Botframework MS团队:如何从c#后端发起与用户的私人聊天/对话
我当前尝试向用户发送私人通知。 我目前在MS Team环境中拥有一个已连接的机器人和连接器 在我的c#后端中,我有一个bot,它继承自ActivityHandler、租户id和ms团队的用户id 现在的情况是: 有人正在我的后端通过API Post调用创建对象(例如任务),我想通知ms团队中的用户。 我现在的想法是在我的API控制器(包括iTunesContext)中实例化Bot。然后使用bot找到具有租户id和用户id的正确ms团队环境,创建新的聊天/对话并发送消息。但我想这不是正确的方法,或者我做错了什么。因为我认为没有办法从我的代码中初始化iTunesContext,或者 这是我的代码,我的想法是在API控制器中使用CreatePrivateConversation方法Botframework MS团队:如何从c#后端发起与用户的私人聊天/对话,botframework,microsoft-teams,office365connectors,Botframework,Microsoft Teams,Office365connectors,我当前尝试向用户发送私人通知。 我目前在MS Team环境中拥有一个已连接的机器人和连接器 在我的c#后端中,我有一个bot,它继承自ActivityHandler、租户id和ms团队的用户id 现在的情况是: 有人正在我的后端通过API Post调用创建对象(例如任务),我想通知ms团队中的用户。 我现在的想法是在我的API控制器(包括iTunesContext)中实例化Bot。然后使用bot找到具有租户id和用户id的正确ms团队环境,创建新的聊天/对话并发送消息。但我想这不是正确的方法,或
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Teams;
using Microsoft.Bot.Connector;
using Microsoft.Bot.Connector.Authentication;
using Microsoft.Bot.Schema;
using Microsoft.Bot.Schema.Teams;
namespace IntegrationsService.Bots
{
public class ProactiveBot : ActivityHandler
{
double _secondsToReply = 3;
ICredentialProvider _credentialProvider;
public ProactiveBot(ICredentialProvider credentialProvider)
{
_credentialProvider = credentialProvider;
}
public async Task<ConversationResourceResponse> CreatePrivateConversation(string message, ITurnContext turnContext)
{
ConnectorClient _client = new ConnectorClient(
new Uri(turnContext.Activity.ServiceUrl),
await GetMicrosoftAppCredentialsAsync(turnContext),
new HttpClient());
var channelData = turnContext.Activity.GetChannelData<TeamsChannelData>();
var conversationParameter = new ConversationParameters
{
Bot = turnContext.Activity.Recipient,
Members = new[] { new ChannelAccount("userid") },
// IsGroup = true,
ChannelData = channelData,
TenantId = channelData.Tenant.Id,
Activity = MessageFactory.Text(message)
};
var response = await _client.Conversations.CreateConversationAsync(conversationParameter);
return response;
}
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
try
{
await turnContext.SendActivityAsync(MessageFactory.Text($"I'll reply to you in {_secondsToReply} seconds."));
QueueReplyAndSendItProactively(turnContext).Wait();
}
catch (Exception e)
{
Debug.WriteLine(e.Message);
throw e;
}
}
public async Task QueueReplyAndSendItProactively(ITurnContext turnContext)
{
string conversationMessage = "I created my own conversation.";
string replyMessage = "I proactively replied to this conversation.";
var task = Task.Run(async () =>
{
await Task.Delay(TimeSpan.FromSeconds(_secondsToReply));
// Let the Bot Proactively create a a conversation.
var response = await CreateConversation(conversationMessage, turnContext);
// Reply to the conversation which the bot created.
await ProactivelyReplyToConversation(response.Id, replyMessage, turnContext);
return Task.CompletedTask;
});
await task;
}
public async Task<ConversationResourceResponse> CreateConversation(string message, ITurnContext turnContext)
{
ConnectorClient _client = new ConnectorClient(new Uri(turnContext.Activity.ServiceUrl), await GetMicrosoftAppCredentialsAsync(turnContext), new HttpClient());
var channelData = turnContext.Activity.GetChannelData<TeamsChannelData>();
var conversationParameter = new ConversationParameters
{
Bot = turnContext.Activity.Recipient,
IsGroup = true,
ChannelData = channelData,
TenantId = channelData.Tenant.Id,
Activity = MessageFactory.Text(message)
};
var response = await _client.Conversations.CreateConversationAsync(conversationParameter);
return response;
}
public async Task ProactivelyReplyToConversation(string conversationId, string message, ITurnContext turnContext)
{
ConnectorClient _client = new ConnectorClient(new Uri(turnContext.Activity.ServiceUrl), await GetMicrosoftAppCredentialsAsync(turnContext), new HttpClient());
var reply = MessageFactory.Text(message);
reply.Conversation = new ConversationAccount(isGroup: true, id: conversationId);
await _client.Conversations.SendToConversationAsync(reply);
}
private async Task<MicrosoftAppCredentials> GetMicrosoftAppCredentialsAsync(ITurnContext turnContext)
{
ClaimsIdentity claimsIdentity = turnContext.TurnState.Get<ClaimsIdentity>("BotIdentity");
Claim botAppIdClaim = claimsIdentity.Claims?.SingleOrDefault(claim => claim.Type == AuthenticationConstants.AudienceClaim)
??
claimsIdentity.Claims?.SingleOrDefault(claim => claim.Type == AuthenticationConstants.AppIdClaim);
string appPassword = await _credentialProvider.GetAppPasswordAsync(botAppIdClaim.Value).ConfigureAwait(false);
return new MicrosoftAppCredentials(botAppIdClaim.Value, appPassword);
}
protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
foreach (var member in membersAdded)
{
if (member.Id != turnContext.Activity.Recipient.Id)
{
await turnContext.SendActivityAsync(MessageFactory.Text($"Hello and Welcome!"), cancellationToken);
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用System.Linq;
使用System.Net.Http;
使用System.Security.Claims;
使用系统线程;
使用System.Threading.Tasks;
使用Microsoft.Bot.Builder;
使用Microsoft.Bot.Builder.Teams;
使用Microsoft.Bot.Connector;
使用Microsoft.Bot.Connector.Authentication;
使用Microsoft.Bot.Schema;
使用Microsoft.Bot.Schema.Teams;
命名空间集成服务.Bots
{
公共类ProactiveBot:ActivityHandler
{
双二次存储层=3;
ICredentialProvider_credentialProvider;
公共ProactiveBot(ICredentialProvider credentialProvider)
{
_credentialProvider=credentialProvider;
}
公共异步任务CreatePrivateConversation(字符串消息,iTunesContext turnContext)
{
ConnectorClient _client=新的ConnectorClient(
新Uri(turnContext.Activity.ServiceUrl),
等待GetMicrosoftAppCredentialAsync(turnContext),
新的HttpClient());
var channelData=turnContext.Activity.GetChannelData();
var conversationParameter=新ConversationParameters
{
Bot=turnContext.Activity.Recipient,
Members=new[]{new ChannelAccount(“userid”)},
//IsGroup=true,
ChannelData=ChannelData,
TenantId=channelData.Tenant.Id,
活动=MessageFactory.Text(消息)
};
var response=wait_client.Conversations.CreateConversationAsync(conversationParameter);
返回响应;
}
受保护的重写异步任务OnMessageActivityAsync(ITurnContext turnContext,CancellationToken CancellationToken)
{
尝试
{
wait turnContext.SendActivityAsync(MessageFactory.Text($“我将在{u secondsToReply}秒内答复您”);
QueueReplyAndSendItProactively(turnContext).Wait();
}
捕获(例外e)
{
Debug.WriteLine(e.Message);
投掷e;
}
}
公共异步任务队列ReplyandSenditProactive(ITurnContext turnContext)
{
string conversationMessage=“我创建了自己的对话。”;
string replyMessage=“我主动回复了此对话。”;
var task=task.Run(异步()=>
{
等待任务延迟(时间跨度从秒(_secondsToReply));
//让机器人主动创建对话。
var响应=等待CreateConversation(conversationMessage,turnContext);
//回复bot创建的对话。
等待主动的replytoconversation(response.Id、replyMessage、turnContext);
返回Task.CompletedTask;
});
等待任务;
}
公共异步任务CreateConversation(字符串消息,iTunesContext turnContext)
{
ConnectorClient _client=新的ConnectorClient(新Uri(turnContext.Activity.ServiceUrl),等待GetMicrosoftAppCredentialAsync(turnContext),新的HttpClient());
var channelData=turnContext.Activity.GetChannelData();
var conversationParameter=新ConversationParameters
{
Bot=turnContext.Activity.Recipient,
IsGroup=true,
ChannelData=ChannelData,
TenantId=channelData.Tenant.Id,
活动=MessageFactory.Text(消息)
};
var response=wait_client.Conversations.CreateConversationAsync(conversationParameter);
返回响应;
}
公共异步任务ProactivelyReplyToConversation(字符串会话ID、字符串消息、iTunesContext turnContext)
{
ConnectorClient _client=新的ConnectorClient(新Uri(turnContext.Activity.ServiceUrl),等待GetMicrosoftAppCredentialAsync(turnContext),新的HttpClient());
var reply=MessageFactory.Text(消息);
reply.Conversation=newconversationaccount(isGroup:true,id:conversationId);
wait_client.Conversations.SendToConversationAsync(reply);
}
专用异步任务GetMicrosoftAppCredentialAsync(iTurContext turnContext)
{
ClaimsIdentity ClaimsIdentity=turnContext.TurnState.Get(“BotIdentity”);
Claim botAppIdClaim=ClaimSideEntity.Claims?.SingleOrDefault(Claim=>Claim.Type==AuthenticationConstants.AudienceClaim)
??
ClaimSideEntity.Claims?.SingleOrDefault(claim=>claim.Type==AuthenticationConstants.AppIdClaim);
字符串appPassword=await\u credentialProvider.GetAppPasswordAsync(botAppIdClaim.Value).ConfigureAwait(false);
返回新的MicrosoftAppCredentials(botAppIdClaim.Value、appPassword);
}
受保护的重写异步任务