1

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.

brian3415
  • 65
  • 1
  • 7

1 Answers1

2
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.

Nikhil Savaliya
  • 2,138
  • 4
  • 24
  • 45
  • This is what I've been thinking of doing too. I thought a customized object within the Dialogflow Fulfillment's response would be better but after digging through the documentation, there doesn't seem to be any way to do it. Another option might be the custom payload response but unfortunately it is not available through Fulfillment. – brian3415 Sep 13 '19 at 16:16
  • you can use simplly https://www.npmjs.com/package/dialogflow where you will be more free to customize – Nikhil Savaliya Sep 14 '19 at 04:46