自定义Dialogflow实现响应

时间:2019-09-12 16:30:41

标签: dialogflow dialogflow-fulfillment

我试图返回一个对象作为Dialogflow实现的响应。

我想返回一些对象,以便稍后由前端处理,但出现错误:

  

错误:未知的响应类型:“ {” fruit1“:” apple“,” fruit2“:   “ orange”}“;

我可能做错了。这是我的代码,问题出在 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实现返回对象/自定义json? agent.add()似乎只接受字符串,但我可能错了。

1 个答案:

答案 0 :(得分:1)

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

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