首页 > 解决方案 > dialogflow Webhookclient“request_”属性

问题描述

我正在尝试使用 Dialogflow 构建一个 facebook messenger 聊天机器人。在 dialogflow 实现内联编辑器中,我发现可以使用 agent.request_.body 来获取请求的正文。我假设“request_”是 WebhoodClient 对象的属性?但是我找不到任何详细说明的文档,您能否告知我的理解是否正确以及在哪里可以找到参考或文档?

const agent = new WebhookClient({ request, response });
console.log(JSON.stringify(agent.request_.body));

谢谢

标签: dialogflow-esfacebook-chatbot

解决方案


Google在此处提供 Dialogflow webhook 的文档,其中包括此示例 webhook 以检查参数并动态创建槽填充提示:

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {

  const agent = new WebhookClient({ request, response });

  function flight(agent) {
    const city = agent.parameters['geo-city'];
    const time = agent.parameters['time'];
    const gotCity = city.length > 0;
    const gotTime = time.length > 0;

    if(gotCity && gotTime) {
        agent.add(`Nice, you want to fly to ${city} at ${time}.`);
    } else if (gotCity && !gotTime) {
        agent.add('Let me know which time you want to fly');
    } else if (gotTime && !gotCity) {
        agent.add('Let me know which city you want to fly to');
    } else {
        agent.add('Let me know which city and time you want to fly');
    }
  }

  let intentMap = new Map();
  intentMap.set('flight', flight);
  agent.handleRequest(intentMap);
});

我的猜测是添加

console.log(agent);

在定义飞行函数之前,然后检查日志以查看代理包含哪些对象,然后添加 console.log(agent.fakeObjectName) 的迭代,直到找到所需的信息。

如果您遵循Google 的 Codelabs 级别 2 中的操作中推荐的部署流程,您的日志将显示在 Firebase 控制台中,如下所示:

Firebase 控制台 - 功能 - 日志

希望有帮助!


推荐阅读