在nodejs中发送来自webhook的不同响应(Dialogflow)

时间:2018-05-10 18:15:38

标签: node.js dialogflow

我是使用Dialogflow的新手。我在nodejs中有一个webhook,当他们要求提供信息时会向用户发送响应。如果用户使用电报,脸谱,线路,我想发送不同的响应...现在我总是发送相同的信息。 这是我的webhook:

"use strict";

const express = require("express");
const bodyParser = require("body-parser");
const restService = express();

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

restService.use(bodyParser.json());

restService.post("/webhook", function(req, res) {
var speech =
req.body.result &&
req.body.result.parameters &&
req.body.result.parameters.tipo
  ? response(req.body.result.parameters.tipo.toLowerCase())
  : "Ups... ha habido algún problema con nuestra comunicación, sorry!";
return res.json({
    speech: speech,
    displayText: speech,
    source: "webhook-echo-sample"
 });
});

let response = function(tipo){
   let response;
   switch(tipo){
       case 'daw':  response = 'http://moodle.iesgrancapitan.org/course/index.php?categoryid=7'; break;
       case 'asir':  response = 'http://moodle.iesgrancapitan.org/course/index.php?categoryid=4'; break;
       default: response = 'Vaya... Creo que algo fue mal'; break;
  }
  return response;
}

restService.listen(process.env.PORT || 8000, function() {
   console.log("Server up and listening");
});

1 个答案:

答案 0 :(得分:2)

有两种方法可以添加特定于平台的响应:

  1. 根据您的意图,一直向下滚动到回复。在默认旁边,有一个加号按钮,您可以在其中选择要添加丰富消息的平台。如果您转到“添加回复”,您可以从下拉菜单中选择您的丰富回复(madia,建议芯片等),然后添加所需的所有信息。
  2. 从您的履行中,添加自定义有效负载。我已在下方附上了该片段,但有关详细信息,请访问此链接https://dialogflow.com/docs/fulfillment。当然,不要忘记实现该意图:)
  3. {
    "fulfillmentText": "This is a text response",
    "fulfillmentMessages": [
      {
        "card": {
          "title": "card title",
          "subtitle": "card text",
          "imageUri": "https://assistant.google.com/static/images/molecule/Molecule-Formation-stop.png",
          "buttons": [
            {
              "text": "button text",
              "postback": "https://assistant.google.com/"
            }
          ]
        }
      }
    ],
    "source": "example.com",
    "payload": {
      "google": {
        "expectUserResponse": true,
        "richResponse": {
          "items": [
            {
              "simpleResponse": {
                "textToSpeech": "this is a simple response"
              }
            }
          ]
        }
      },
      "facebook": {
        "text": "Hello, Facebook!"
      },
      "slack": {
        "text": "This is a text response for Slack."
      }
    },
    "outputContexts": [
      {
        "name": "projects/${PROJECT_ID}/agent/sessions/${SESSION_ID}/contexts/context name",
        "lifespanCount": 5,
        "parameters": {
          "param": "param value"
        }
      }
    ],
    "followupEventInput": {
      "name": "event name",
      "languageCode": "en-US",
      "parameters": {
        "param": "param value"
      }
    }