热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

dialogflow-fulfillment-library和express,该如何处理?

如何解决《dialogflow-fulfillment-library和express,该如何处理?》经验,为你挑选了1个好方法。

我正在尝试使用带有express的dialog-fulfillment-library库,而没有firebase函数。我在寻找如何重新处理代理方面遇到麻烦。

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

    module.exports = function () {

      let self = {};

      self.create = function (req, res, next) {
        const agent = new WebhookClient({request: req, response: res});

        agent.add(new Suggestion(`Quick Reply`));
        agent.add(new Card({
            title: `Title: this is a card title`,
            imageUrl: 'https://developers.google.com/actions/images/badges/XPM_BADGING_GoogleAssistant_VER.png',
            text: `This is the body text of a card.  You can even use line\n  breaks and emoji! `,
            buttonText: 'This is a button',
            buttonUrl: 'https://assistant.google.com/'
          })
        );

       res.json(agent);
      };

     return self;
   };

我收到TypeError:将圆形结构转换为JSON我曾尝试回收代理,但是在dialogflow端它不起作用。使用:

  res.send(JSON.stringify(agent, decycle())); 

返回: Webhook call failed. Error: Failed to parse webhook JSON response: Cannot find field: request_ in message google.cloud.dialogflow.v2.WebhookResponse.

有没有人以这种方式使用过它?



1> Abhinav Tyag..:

我已经提交了相同的请求请求。

以下代码对我有用。

package.json

{
  "name": "Test_Agent",
  "version": "0.0.1",
  "description": "Test Agent webhook",
  "main": "server.js",
  "author": "Abhinav Tyagi, New Delhi, India",
  "dependencies": {
    "dialogflow-fulfillment": "^0.4.1",
    "body-parser": "^1.18.3",
    "express": "^4.16.3",
    "actions-on-google": "^2.2.0"
  }
}

server.js

'use strict';

const {WebhookClient} = require('dialogflow-fulfillment');
const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));



function welcome (agent) {
    agent.add(`Welcome to Express.JS webhook!`);
}

function fallback (agent) {
    agent.add(`I didn't understand`);
    agent.add(`I'm sorry, can you try again?`);
}

function WebhookProcessing(req, res) {
    const agent = new WebhookClient({request: req, response: res});
    console.info(`agent set`);

    let intentMap = new Map();
    intentMap.set('Default Welcome Intent', welcome);
    intentMap.set('Default Fallback Intent', fallback);
// intentMap.set('', yourFunctionHandler);
    agent.handleRequest(intentMap);
}


// Webhook
app.post('/', function (req, res) {
    console.info(`\n\n>>>>>>> S E R V E R   H I T <<<<<<<`);
    WebhookProcessing(req, res);
});

app.listen(8080, function () {
    console.info(`Webhook listening on port 8080!`)
});

确保同时使用“谷歌操作”模块和“对话流”实现模块。


推荐阅读
author-avatar
开口就笑i
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有