Search code examples
botframeworkchatbotazure-bot-service

How can I use the bot composer to dynamically configure the body part of a HTTP REQUEST


I am using Bot Composer to publish my first chatbot. I need to construct the chatbot to send out an HTTP POST request to fetch external resources from a remote website. As specified by the composer interface, I can embed JSON, form data, or string in the body of the HTTP POST request. Instead of hard-coding the body part of the POST request, I need to pass in one or multiple properties (chatbot's variable) to generate the body of the HTTP POST dynamically. Here are my questions:

(1) can I pass a variable to the body part of the HTTP REQUEST (such as POST)? can I embed a property such as $(user. name) in the HTTP POST body?

For example, can I embed a property such as $(user.name) in a string or form data (such as fname=$(user. name) to construct the body part of the HTTP POST REQUEST?

(2) The document specifies that there is a pre-build function JSON to serialize data. If I understand correctly, I can't pass a variable (such as $(user. name) to the JSON pre-built function. Therefore, I will probably need to embed an expression in the body to pass the variable. Yet, I couldn't find any detailed information. Is there anywhere I can find a good example showing how to write an expression inside the body part of the HTTP REQUEST

Thanks for any information/assistance.


Solution

  • Yes, you can do this. The simplest way is to set the body to Object and then put in your structured json, something similar to:

    {
      "userinfo": {
        "username": "${user.username}",
        "name": "${user.personalname}",
        "favoritecolor": "${user.favcolor}",
        "profileupdated":"${dialog.userprofileuptodate}"
      }
    }
    

    I am trying to figure out how to set it up in an adaptive expression in LG, and then be able to refrence it with something like:

    # APIBodyTemplate()
    -```
    {
      "userinfo": {
        "username": "${user.username}",
        "name": "${user.personalname}",
        "favoritecolor": "${user.favcolor}",
        "profileupdated":"${dialog.userprofileuptodate}"
      }
    }
    ```
    

    And then using something like the following in an expression in the body field: =json(APIBodyTemplate()), but that is not quite working yet. Might be a bug. I will update when I have more info.