0

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"
}
Anthony
  • 1,760
  • 1
  • 23
  • 43
  • I've seen questions like [this](https://stackoverflow.com/questions/63030966/microsoft-teams-cant-start-conversation-receiving-status-code-forbidden) one, which unfortunately do not explain the details of the underlying request. Using the SDK is not an option for me. – Anthony Feb 06 '23 at 16:58

2 Answers2

1

What you're trying to do is called "Proactive Messaging" - basically, it's having your bot create the first message in a series, rather than being the 'recipient' in the usual case. You definitely will need a bot for this, but it sounds like you have one already so that's good. In essence, in a Teams context, you're never really starting a 'new' conversation with the bot, it's just 'continuing' an existing one, so you're wanting to post to the endpoint of that converation. I've covered (I hope well) in some other answers, so please refer here for more: Sending proactive messages from an outside process to organizational users via Teams chat bot . If you're still stuck, let me know and I'll try help further.

Hilton Giesenow
  • 9,809
  • 2
  • 10
  • 24
  • Thanks for the information. I have been successful with proactive messaging (it has transformed my business, actually), but never to a `channel`. In essence, I need to start a new conversation in a channel with the bot. I can reply to conversations, messages, group chats, etc., but specifically starting a new conversation in a channel has never worked for me. The documentation for `/conversations` does not explain how to do this. – Anthony Feb 06 '23 at 17:50
  • By the way, I read your link and much of the referenced documentation. Thanks. The disconnect is the [official documentation says it is possible to start a conversation](https://learn.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-connector-send-and-receive-messages?view=azure-bot-service-4.0#start-a-conversation) in a channel: `There may be times when your bot needs to initiate a conversation with one or more users. To start a conversation with a user on a channel, your bot must know its account information and the user's account information on that channel.` – Anthony Feb 06 '23 at 17:58
  • For the purposes of documentation, I'm adding a link to the [ConversationParameters](https://learn.microsoft.com/en-us/azure/bot-service/rest-api/bot-framework-rest-connector-api-reference?view=azure-bot-service-4.0#conversationparameters-object) details here as well. I've tried creating a request with the specified properties, but it never works. – Anthony Feb 06 '23 at 18:40
  • 1
    So you need to be careful, when reading the bot framework docs, to differentiate between a BOT channel and a TEAMS channel. The sample I linked to (https://github.com/pnp/teams-dev-samples/blob/main/samples/bot-proactive-messaging/src/nodejs-backend/botActivityHandler.js) works for 1-1 as well as TEAMS channels no problem. The link you've posted above is talking about starting a conversation with a user on a BOT channel. In a BOT context, 'channel' means Web chat/facebook/email/slack/Teams as being 'channels' of communication. – Hilton Giesenow Feb 06 '23 at 19:14
  • Thankfully Microsoft made this very straightforward and not all confusing by reusing the word channel everywhere without an explanation. So annoying. Unfortunately, the link you sent doesn't show the JSON object, so it isn't as helpful to me. I will use it as a reference, though. – Anthony Feb 06 '23 at 20:00
  • 1
    confusing indeed! 100000 words in the English language and they had to use the SAME ONE! – Hilton Giesenow Feb 06 '23 at 20:08
  • FYI - see my answer for the JSON structure to start a new conversation. – Anthony Feb 06 '23 at 20:26
0

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:c25ae532345659b67313c54e1310fdb6@thread.tacv2;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.

Anthony
  • 1,760
  • 1
  • 23
  • 43
  • See [here](https://github.com/microsoft/botbuilder-dotnet/issues/5041) for discussion about setting the subject of a conversation. – Anthony Feb 06 '23 at 20:50
  • 1
    Glad you got it sorted out. Note that having `;messageid=2456564642569` at the end indicates you're replying to an existing message. Leaving that off will start a new "Thread". You could also consider using a "Heading" in markdown to give a "title" - see https://support.microsoft.com/en-us/office/use-markdown-formatting-in-teams-4d10bd65-55e2-4b2d-a1f3-2bebdcd2c772 – Hilton Giesenow Feb 07 '23 at 07:48