10

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: https://dev.outlook.com/Connectors/Reference

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
    };
    PostMessage(payload);
}

//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);
}
Jose
  • 103
  • 1
  • 1
  • 6

1 Answers1

9

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.

  • Thanks @Sid-Uppal - MSFT, Maybe create a Bot could be better, but I'm not very familiarized with Bots Framework and I only need send messages from my app to users and channels but not backwards. I don't want to edit my code so much but if you need it, I will edit my question adding the code used to post into MSFT Teams with C#. – Jose Nov 03 '16 at 19:43
  • @Jose - I understand how you are posting a webhook message into a channel. Unfortunately, we don't support posting webhook messages into chats right now. The only option you have to notify users 1:1 via your app is to utilize bots. Take a look at the sample Echo bot in the Bot Framework SDK [here](https://github.com/Microsoft/BotBuilder/tree/master/CSharp/Samples/EchoBot). It is very easy to get started. – Sid Uppal - MSFT Nov 03 '16 at 20:29
  • Thanks for your help @Sid Uppal - MSFT, I will try this afternoon and check again with your new info how to achieve my goal :) – Jose Nov 04 '16 at 09:25