Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MS Bot, Direct line API, cannot send an activity, how to fix it?

I been trying to set up MS Bot within an app. So far, I did following.

  1. set a REST endpoint for Bot, that connector listens to it.

     app.post("/botapi/messages", connector);
    
  2. get APP_ID and APP_PASSWORD, emulator successfully connected to with following,

     http://localhost:4000/botapi/messages
     APP_ID
     APP_PASSWORD
    

    this is successful, bot replied as expected.

  3. Tried to talk to the Bot using Direct Line API. Successfully started conversations. One of the response is as follows.

     {
       "conversationId": "3JYZyAn5VYB3HNcO3tcgtn",
       token: ....
        .....
      }
    

    I used "node-fetch" packages to issue POST request, as the documentations said.

However, I cannot send an activity using Direct Line API, received

    internal server error 500

The documentation said the POST request should be like this below.

    POST 
    https://directline.botframework.com/v3/directline/conversations
    Authorization: Bearer my_secret

This worked perfectly for starting conversations but not for sending activities.

The activity I sent is:

      {
       "type": "message",
       "from": {
              "id": "user1"
               },
            "text": "hello Bot, say something"
       }

I don't think "id" is something important, so that is what I posted to

          https://directline.botframework.com/v3/directline/conversations/{conversationId}/activities

I used the conversationId received from starting a conversation. I googled around, but find no answer to my problem. Besides, I have few questions, maybe their answers would help me.

Q1: The url "https://directline.botframework.com/v3/directline/" is the same for everybody using Direct Line API? When I replace it with the endpoint of the bot, "http://localhost:3000/botapi/messages/conversations", I even cannot start conversations, nothing works.

Q2: How does the Direct Line API work? I issue POST with my secret to the API, then how the API finds my bot? How the Bot and API communicate? am I missing something here?

Q3: When I issue POST to send an activity, I followed the documentation. In the Authorization, I tried both my secret and the token I got from starting conversation, both did not work. I believe both should work. am I wrong?

Q4: Do I need to do something with Bot Connector Service? I read the article, but I don't know what it is for. am I wrong?

So, what I am missing here? How can I send activities?

Note: My bot is not deployed to azuri or aws, it is on my Mac only. But, I got APP_ID, APP_PASSWORD and SECRET for DirectLine as documents explained.

like image 729
arslan Avatar asked Oct 18 '22 04:10

arslan


1 Answers

The documentation specifies that this is the endpoint for sending an activity (message) to a bot via Direct Line:

https://directline.botframework.com/v3/directline/conversations/{conversationId}/activities

In this URI, {conversationId} is the conversation ID (conversationId value) that you received in the response body when you started the conversation. And the body of the request should specify information about the activity you're sending, for example:

{
    "type": "message",
    "from": {
        "id": "user1"
    },
    "text": "hello"
}

Finally, answers to your questions:

  • Q1: the base URI is the same for all Direct Line API requests.

  • Q2: the Direct Line secret or token that you specify in the Authorization header of the request is used to identify the bot that the request should be directed to

  • Q3: yes, you should be able to specify either the secret or the token value that you receive in the Start Conversation response in the Authorization header of the Send Activity request. Note, however, that the token you receive in a Start Conversation response can only be used to interact with that specific conversation.

  • Q4: The article that you've linked to isn't directly relevant to using the Direct Line API. (But as @EzequielJadib mentioned in his comment, you do need to enable the Direct Line channel for your bot.)

like image 186
Kim Brandl Avatar answered Dec 31 '22 00:12

Kim Brandl