此时实现您的目标的最佳方法是创建一个机器人并实现它以公开您的应用程序或服务可以发布到的 Webhook 端点,并让机器人将这些消息发布到与用户的聊天中。
首先,根据机器人收到的传入活动,捕获成功发布到机器人与用户的对话所需的信息。
var callBackInfo = new CallbackInfo()
{
ConversationId = activity.Conversation.Id,
ServiceUrl = activity.ServiceUrl
};
然后将 callBackInfo 打包到令牌中,该令牌稍后将用作 Webhook 的参数。
var token = Convert.ToBase64String(
Encoding.Default.GetBytes(
JsonConvert.SerializeObject(callBackInfo)));
var webhookUrl = host + "/v1/hook/" + token;
最后,实现 webhook 处理程序来解压 callBackInfo:
var jsonString = Encoding.Default.GetString(Convert.FromBase64String(token));
var callbackInfo = JsonConvert.DeserializeObject<CallbackInfo>(jsonString);
并发布到机器人与用户的对话中:
ConnectorClient connector = new ConnectorClient(new Uri(callbackInfo.ServiceUrl));
var newMessage = Activity.CreateMessageActivity();
newMessage.Type = ActivityTypes.Message;
newMessage.Conversation = new ConversationAccount(id: callbackInfo.ConversationId);
newMessage.TextFormat = "xml";
newMessage.Text = message.Text;
await connector.Conversations.SendToConversationAsync(newMessage as Activity);
看看我关于这个主题的博客文章here https://blog.somecreativity.com/2016/11/04/send-a-notification-to-a-microsoft-teams-user-from-an-app/。如果您以前从未编写过 Microsoft Teams 机器人,请查看我的另一篇博文,其中包含分步说明here https://blog.somecreativity.com/2016/11/03/creating-a-bot-for-microsoft-teams/.