1
votes

I am trying to return an object as a response from Dialogflow fulfillment.

I wanted to return some object to be handled later by my front end but I am getting an error:

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

It's likely that I may be doing it wrong. Here's the code that I have, problem is on 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);
});

Is there a way where I can return an object / self defined json via Dialogflow Fulfillment? agent.add() seems to only accept strings but I may be wrong.

1

1 Answers

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

On your client side you can use JSON.parse() to have same object.