Forum Discussion

Vladimir_Anokhin's avatar
Vladimir_Anokhin
Copper Contributor
Jun 11, 2021

I can't show message in Teams after success API request

I made new conversation as API with my bot in Teams with DirectLine chanel

"https://directline.botframework.com/v3/directline/conversations" 

and made sucsses request with message

"https://directline.botframework.com/v3/directline/conversations/Bl1AgRQB1zz7xx8NrdFNe2-m/activities" 

And have correct answer 200OK with ID :

 

"id": "Bl1AgRQB1zz7xx8NrdFNe2-m|0000030"

 

But in Teams nothing events, not any messages.

Why probably reason of this behaviour?

  • I found right way:

    1 step - take information from bot's index.js about the participants in the conversation with the bot and save it to yourself - this can be done by sending a request from the bot to your script
    const axios = require('axios');
    axios.get('https://My_server/My_script.php', {
    params: {
    'context': context
    }
    })
    .then(function (response) {
    console.log(response);
    })

    2 step - get Bearer token via request
    https://login.microsoftonline.com/common/oauth2/v2.0/token
    Use Application ID and secret key in parameters
    Note - token's life is 1 day, will get new if need

    3 step - with token to create new conversation with user send request
    https://smba.trafficmanager.net/emea/v3/conversations
    WARNING - look at the exact url in the data that you received from the bot in step 1 - it may differ!

    with body (all parameters you should save in yours base in step 1) :
    {
    "bot": {
    "id": "BotID",
    "name": "NameBot"

    },
    "isGroup": false,
    "members": [
    {
    "id": "UserID",
    "name": "User Name"
    }
    ],
    "tenantId":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
    "topicName": "News Alert"
    }
    In answer you must get ID of new conversation - save it too

    And finally - step 4 - send message to user with request
    https://smba.trafficmanager.net/amer/v3/conversations/[CONFERSATION_ID]/activities

    WARNING - look at the exact url in the data that you received from the bot in step 1 - it may differ!

    and body like this or others variants of fields (don't forget use token which you got in step 2)


    {
    "type": "message",
    "channelId": "msteams",
    "conversation": {"id": "CONVERSATION_ID"},

    "from":{"id": "BotID","name": "NameBot"},
    "recipient":{"id": "UserID",
    "name": "UserName"
    },
    "text": "Hi userName! Welcome from API.",
    "channelData": {
    "notification": {
    "alert": true
    }
    },
    "textFormat":"plain"}

    I hope this helps someone lose a little less time than it does me 🙂 .
    Chetana-MSFT , thank you for yours attention.
    • Vladimir_Anokhin's avatar
      Vladimir_Anokhin
      Copper Contributor
      Sorry, I didn't quite understand what documents you mean. In general, my problem is that I need to send a private message to a user in Teams via a bot via an https request. And although all requests seem to be executed correctly, messages do not come.

Resources