Incoming Webhook for Private Messages in Microsoft Teams

99封情书 提交于 2019-12-03 16:04:44

The best approach to achieve your goal at this point is to create a Bot and implement it to expose a webhook endpoint which your app or service can post to and for the bot to post those messages into chat with the user.

Start by capturing the information required to successfully post to a conversation of a bot with user based on the incoming activity received by your bot.

var callBackInfo = new CallbackInfo() 
{ 
     ConversationId = activity.Conversation.Id, 
     ServiceUrl = activity.ServiceUrl
};

Then pack the callBackInfo into a token that would later be used as a parameter to your webhook.

 var token = Convert.ToBase64String(
     Encoding.Default.GetBytes(
         JsonConvert.SerializeObject(callBackInfo)));

 var webhookUrl = host + "/v1/hook/" + token;

Finally, implement the webhook handler to unpack the callBackInfo:

var jsonString = Encoding.Default.GetString(Convert.FromBase64String(token));
var callbackInfo = JsonConvert.DeserializeObject<CallbackInfo>(jsonString);

And post to the conversation of the bot with the user:

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);

Take a look my blog post on this topic here. If you have never written a Microsoft Teams bot before, take a look at my other blog post with step-by-step instructions here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!