5

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.

arslan
  • 2,034
  • 7
  • 34
  • 61
  • Have you seen https://github.com/Microsoft/BotBuilder-Samples/tree/master/Node/core-DirectLine? Also, did you enable DirectLine as a channel for your bot? – Ezequiel Jadib Oct 11 '17 at 10:46
  • @EzequielJadib Yes, I created a "Direct Line" Channel in my bot. I also checked that example in this morning. I saw it uses "swagger-client", but our app already using "node-fetch" for issue request, so I used "node-fetch". The rest of the story as write above: can start conversations but cannot send activity :( – arslan Oct 11 '17 at 10:50
  • I would first see if you can make the sample I provided works against your bot; after that, I will review the specific parts (besides which client you use to issue a request) to see if there is any outstanding difference. – Ezequiel Jadib Oct 11 '17 at 10:55
  • @EzequielJadib The Direct Line API is just issuing HTTP request. That is what I did using "node-fetch", and worked for starting conversations. But did not work for sending activities. The sample you mentioned, did not even start, it has some errors, tried to fix, but could not. – arslan Oct 12 '17 at 01:13

1 Answers1

3

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.)

Kim Brandl
  • 13,125
  • 2
  • 16
  • 21
  • Thanks for clarifying these. It turned out that what you said is exactly what I did. But still, I can start a conversation, but cannot send an activity using the token received in the response of start conversation. :( What is the possible problem here, hmm... – arslan Oct 12 '17 at 10:57
  • I'm not sure what to tell you, as the procedure that I've outlined should work. To troubleshoot further (and get help here on SO), you might consider using a tool like Fiddler (or something similar) to capture the API request / response that you're sending and receiving for both the **Start Conversation** operation and subsequent **Send Activity** operation and update your question above with that info (obfuscating any sensitive information of course). – Kim Brandl Oct 12 '17 at 13:38
  • My bot is restified locally, I did not deploy it to Azuri Or Aws. Is there possibility that it is the cause ? – arslan Oct 17 '17 at 03:08