dialogflow-cx

DialogFlow CX webhook basics


I want to give my chatbot some calculation capabilities but I have no experience with webhooks at all. Unfortunately there seems to be close to zero documentation on this topic for DialogFlow CX (except some generic description of what webhooks are).

Does anybody know of a concrete tutorial showing how to do this? Or could anybody point me in the right direction?

My starting point: I have a DialogFlow CX bot with a couple of session parameters which I want to use for some calculations and the calculation result should then be included in a bot text utterance.

What are the different steps I need to do now? Does anybody have a concrete sample incl. sample code that I could reuse? (I basically just know that I need to setup a webhook resource in DialogFlow CX and that the easiest way for such a webhook is probably using Cloud Functions (which I have never done before))

Thanks a lot!!!


Solution

  • Note that simple calculations like addition and subtraction can be done in Dialogflow CX without webhook with the help of System functions.

    For more complex calculations, here are general steps you can follow for setting up and using a webhook for Dialogflow CX:

    1. Create a web service on your preferred hosting site(e.g. Cloud functions) and refer to Webhook service requirements and Authentication.

    2. Once your webhook service is set up, you can add it to your agent as a webhook resource via the Dialogflow CX console or API.

    3. After creating the webhook resource for your agent, you can add it in any section that supports using webhook(e.g. Fulfillment, Routes, Event handlers, etc.).

    4. When the agent calls a webhook, it sends a webhook request json. See the WebhookRequest reference documentation for details.

    5. You can get Session parameters under the webhookRequest.sessionInfo.parameters json body:

      {
        "sessionInfo" : {
          "session":<session string>,
          "parameters": {
             "param1": {
                 "value": "sample1"
             }
         }
      
      }
      

      Here’s a sample webhook code using Node.js extracting the value of the “param1” parameter:

      let yourParameter = request.body.sessionInfo.parameters.param1;

    6. Your webhook service must then return a valid format of the webhook response json. See the WebhookResponse reference documentation for details. Here’s an example code using Node.js on how to send a text response and pass a session parameter from the webhook response:

      let jsonResponse = {
         fulfillment_response: {
           messages: [
             {
               text: {
                 text: ["This is a sample response."]
               }
             }
           ]
         },
         sessionInfo: {
           parameters: {
                "sample-parameter": {
                    value: "sample1"
                }
           }
         }
       };
      
       response.json(jsonResponse);
      

    Note that the exact code to receive, process, and send json data would vary depending on the programming language you are using.

    You can check JavaScript webhook examples for calculating values in this Stackoverflow post.