自定义 Dialogflow Fulfillment 响应

Custom Dialogflow Fulfillment response

我正在尝试 return 一个对象作为来自 Dialogflow fulfillment 的响应。

我想 return 一些对象稍后由我的前端处理,但我收到一个错误:

Error: Unknown response type: "{"fruit1": "apple", "fruit2": "orange"}";

很可能我做错了。这是我的代码,问题出在 function intent1f.

use strict';

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

process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));

  function welcome(agent) {
    agent.add(`Welcome to my agent!`);
  }

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

  function intent1f(agent) {
    var someObject = {"fruit1": "apple",
                      "fruit2": "orange"};
    agent.add(someObject);
  }

  let intentMap = new Map();
  intentMap.set('Default Welcome Intent', welcome);
  intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('intent1', intent1f);
  // intentMap.set('your intent name here', googleAssistantHandler);
  agent.handleRequest(intentMap);
});

有没有一种方法可以通过 Dialogflow Fulfillment return 对象/自定义 json? agent.add() 似乎只接受字符串,但我可能错了。

function intent1f(agent) {
    var someObject = {
        "fruit1": "apple",
        "fruit2": "orange"
    };
    agent.add(JSON.stringify(someObject));
}

在您的客户端,您可以使用 JSON.parse() 来拥有相同的对象。