Incoming Webhook for Private Messages in Microsoft Teams

Posted on

Problem :

I can create an incoming webhook from a C# app or PS Script sending a JSON message to channels like MSFT doc explains.

However, I want to use my incoming webhook for send JSON messages from my app to users (as Private Messages) like Slack allows.

As far as I know this is not possible with MSFT Teams:

But maybe you know any workaround or something like that to fix it.

Thanks in advance 🙂

[EDITED] Code used to post messages into MSFT Team by C# App:

//Post a message using simple strings
public void PostMessage(string text, string title)
    Payload payload = new Payload()
        Title = title
        Text = test

//Post a message using a Payload object
public async void PostMessage(Payload payload)
    string payloadJson = JsonConvert.SerializeObject(payload);
    var content = new StringContent(payloadJson);
    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    var client = new HttpClient();
    uri = new Uri(GeneralConstants.TeamsURI);
    await client.PostAsync(uri, content);

Solution :

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(

 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.

Leave a Reply

Your email address will not be published. Required fields are marked *