How to create a local server for dialogflow chatbot?

Viewed 1348

I am trying to built a local server for dialogflow bot using a node.js framework but ,not able to establish one .

I am using serveo.net as tunneling as ngrok doesn't work as it is blocked by my institute.

I am able to launch a server but unable to get a response from it back to the dialogflow agent.

'use strict';

const {WebhookClient} = require('dialogflow-fulfillment');

const express = require("express"); //express

const bodyParser = require("body-parser"); //body-parser

const app = express(); //app

app.use(bodyParser.json);

app.use(bodyParser.urlencoded({
    extended: true
  })
);

const WEBHOOK = 'webhook';

app.get('/', (req, res) => res.send('online'));

app.post('/webhook', express.json(), (request, respond) => {
  const agent = new WebhookClient({
    request,
    response
  });

  function webhookprocessing(request, response) {
    const agent = new WebhookClient(request, response);
    const action = agent.intent;
    if (action == WEBHOOK) {
      agent.add("My name is karthik");
    } else {
      agent.add("karthik");
    }
  }

  function welcome() {
    agent.add('Welcome to my agent!')
  }

  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set("webhook", webhookprocessing);

  agent.handleRequest(intentMap)
  //const agentPath =  agent.entitiesClient.projectAgentPath("master-bot-53dee");
  //console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  //console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
  //console.log("Server Hit");
});

app.listen(process.env.PORT || 5000);

edit1: I am getting a request from google dialogflow but my local server isn't sending a response.

edit2: The response payload shown received by dialogflow from my node is

{
   "responseId":"efaf7898-74de-4727-bf2a-8eeb32ba570a-baaf0c1f",

   "queryResult":{

      "queryText":"1",

      "parameters":{

         "number":1

      },

      "allRequiredParamsPresent":true,

      "fulfillmentMessages":[
         {

            "text":{

               "text":[

                  ""

               ]
            }

         }

      ],

      "intent":{

         "name":"projects/master-bot-53dee/agent/intents/15b96d92-4adb-4657-8b15-ebdf7df180b4",

         "displayName":"webhook"
      },
      "intentDetectionConfidence":1,

      "diagnosticInfo":{

         "webhook_latency_ms":4991
      },

      "languageCode":"en"

   },

   "webhookStatus":{
      "code":4,
      "message":"Webhook call failed. Error: Request timeout."
   }
}

and the request payload send by dialogflow is

{
   "responseId":"efaf7898-74de-4727-bf2a-8eeb32ba570a-baaf0c1f",
   "queryResult":{
      "queryText":"1",
      "parameters":{
         "number":1
      },
      "allRequiredParamsPresent":true,
      "fulfillmentMessages":[
         {
            "text":{
               "text":[
                  ""
               ]
            }
         }
      ],
      "intent":{
         "name":"projects/master-bot-53dee/agent/intents/15b96d92-4adb-4657-8b15-ebdf7df180b4",
         "displayName":"webhook"
      },
      "intentDetectionConfidence":1,
      "languageCode":"en"
   },
   "originalDetectIntentRequest":{
      "payload":{

      }
   },
   "session":"projects/master-bot-53dee/agent/sessions/d1205a66-9eda-d79c-7677-75eeb402e7e5"
}

The request sent by dialogflow reaches my public url created by my tunneling software but there isn't any response from the localhost .

enter image description here

This image is a screenshot of my console where I appear to be getting a post request but there isn't a response appearing on dialogflow.

I have used this url to refer webhook url https://excedo.serveo.net/.

enter image description here

2 Answers
app.post('/webhook', express.json(), (request, respond) => { // error name of the param doesn't match its usage

This line you are using respond as the parameter and passing and using response instead .Please change line to -

app.post('/webhook', express.json(), (request, response) => {

The webhook isn't being called at the /webhook path because the configuration in Dialogflow is telling it the webhook is at /. If you change this to https://excedo.serveo.net/webhook it will be routed correctly.

Related