dialogflow-fulfillment-library and express, what to res on?

走远了吗. 提交于 2019-12-23 03:02:58

问题


I'm trying to use the dialog-fulfillment-library with express, without firebase functions. I'm having trouble finding how to res on the agent though.

    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;
   };

I get a TypeError: Converting circular structure to JSON I've tried decycling the agent but then it doesn't work on the dialogflow side. using:

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

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

Has anyone used it in this way or is it not possible?


回答1:


I have submitted a Pull Request for the same.

Following code works for me.

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('<INTENT_NAME_HERE>', 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!`)
});

Make sure to use both action-on-google and dialogflow-fulfillment modules.



来源:https://stackoverflow.com/questions/51406463/dialogflow-fulfillment-library-and-express-what-to-res-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!