1
votes

I am trying to send back a response from a webhook with content formatted to be read, and content formatted to be spoken. The text to be read on screen would be in the displaytext field, and the text to be spoken in the speech field. Using the weather webhook example from github.com/api-ai, I tried to add a second object to the resolve method:

resolve(speech_output, displayText_output);

but when used to send the response, the displayText_output is not used:

callWeatherApi(city, date).then((speech_output, displayText_output) => {
            // Return the results of the weather API to API.AI
            res.setHeader('Content-Type', 'application/json');
            res.send(JSON.stringify({'speech': speech_output, 'displayText': displayText_output}));

The displayText is not included in the JSON object for the response. I think the Promise.resolve only takes one argument to process, and one to return error like Promise(resolve, reject). However, when the value of speech_output is used for both fields, the displayText is included in the JSON response.

I would like to know if there is a way to fulfill those two fields with different information to be send in the same answer.

Thanks.

1

1 Answers

1
votes

The easiest way would be to create a new Javascript object for the variable output to include both the displayText and speech attributes. For example:

output = { 'displayText': 'YOUR DISPLAY TEXT STRING HERE',
           'speech': 'YOUR SPEECH STRING HERE' };
resolve(output);

Then in your then block:

callWeatherApi(city, date).then((output) => {
            // Return the results of the weather API to API.AI
            res.setHeader('Content-Type', 'application/json');
            res.send(JSON.stringify(output));