简体   繁体   English

如何为对话流聊天机器人创建本地服务器?

[英]How to create a local server for dialogflow chatbot?

I am trying to built a local server for dialogflow bot using a node.js framework but ,not able to establish one .我正在尝试使用node.js框架为 dialogflow bot 构建本地服务器,但无法建立一个。

I am using serveo.net as tunneling as ngrok doesn't work as it is blocked by my institute.我正在使用serveo.net作为隧道,因为ngrok不起作用,因为它被我的研究所阻止了。

I am able to launch a server but unable to get a response from it back to the dialogflow agent.我能够启动服务器,但无法从它获得对 dialogflow 代理的响应。

'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.编辑 1:我收到来自 google dialogflow 的请求,但我的本地服务器没有发送响应。

edit2: The response payload shown received by dialogflow from my node is edit2:dialogflow 从我的节点接收到的响应负载是

{
   "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并且由 dialogflow 发送的请求有效负载是

{
   "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 . dialogflow 发送的请求到达了我的隧道软件创建的公共 URL,但没有来自 localhost 的任何响应。

在此处输入图片说明

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.此图像是我的控制台的屏幕截图,其中我似乎收到了一个发布请求,但在 dialogflow 上没有出现响应。

I have used this url to refer webhook url https://excedo.serveo.net/ .我已经使用这个 url 来引用 webhook url https://excedo.serveo.net/

在此处输入图片说明

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

您正在使用响应作为参数并传递和使用响应的这一行。请将行更改为-

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 / .未在/webhook路径调用/webhook因为 Dialogflow 中的配置告诉它 webhook 位于/ If you change this to https://excedo.serveo.net/webhook it will be routed correctly.如果您将其更改为https://excedo.serveo.net/webhook ,它将被正确路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM