Search code examples
botframeworkmicrosoft-teams

Starting a new conversation in a Teams channel via REST


As far as I know, there are two APIs that are required to interact with bots

  • Graph - Access to Microsoft Graph endpoints, which do not provide access to conversations in Teams.
  • Bot Framework - The endpoints in this API provide access to conversations.

Moving past the difficulty of finding bot-user IDs, the problem becomes the creation of a new conversation in a channel by the bot. This link says it should be possible, but I have not been successful after hours of attempts.

The service URL in the documentation does not match my requests. It says https://smba.trafficmanager.net/teams/, but I see https://smba.trafficmanager.net/amer/ and https://smba.trafficmanager.net/apis/ in other places. Which one is it?

The crux of the issue is the required fields in the JSON data. This link is meant to help with the creation of channel conversations, but it does not explain the data. In that section is the following comment:

Alternatively, you can use the REST API and issue a POST request to /conversations resource.

which leads to here. Unfortunately, that page does not explain how this works because the sample JSON data is incomplete and not targeted at channels. It appears to be related only to new group chats.

Ultimately, I am requesting an example JSON request to the POST /v3/conversations endpoint, which will create a new conversation in a Team (conversationType = channel, to be clear). This is how it is supposed to look for a new group chat, which does not work with a channel:

{
    "bot": {
        "id": "12345678",
        "name": "bot's name"
    },
    "isGroup": false,
    "members": [
        {
            "id": "1234abcd",
            "name": "recipient's name"
        }
    ],
    "topicName": "News Alert"
}

Solution

  • I managed to get a JSON object to create a conversation. I have been at this for days, by the way.

    The following JSON object will create a new conversation in the General channel of a Team:

    {
        "activity": {
            "type": "message",
            "text": "got here"
        },
        "bot": {
            "id": "{{botID}}",
            "name": "{{botName}}"
        },
        "channelData":{
          "teamsChannelId":"{{teamID}}",
          "teamsTeamId":"{{teamID}}",
    
          // The channel ID of the General channel is the team ID
          // Use another channel ID here if you want the message to go elsewhere
          "channel":{"id":"{{teamID}}"},
    
          "team":{"id":"{{teamID}}"},
          "tenant":{"id": "{{tenantID}}"}
        },
        "isGroup": true,
        "tenantId": "{{tenantID}}"
    }
    

    Here is the way to reply:

    POST {{urlBase}}/v3/conversations/{{conversationId}}/activities
    
    {
        "bot": {
            "id": "{{botID}}",
            "name": "{{botName}}"
        },
        "text": "reply test",
        "type": "message"
    }
    

    where urlBase is something like https://smba.trafficmanager.net/amer and conversationId is something like 19:[email protected];messageid=2456564642569 .

    A ton of helpful information can be found here. That link is buried and completely obscure, but it has most of what you'll want for low-level calls.

    Unfortunately, adding a title/subject to the conversation seems to be impossible, which completely breaks my use case. All that work yielded nothing for me because the easiest part is missing, but at least I figured it out. I hope this helps someone.