API.ai如何在同一答案中为webhook添加语音和显示文本字段的不同履行

时间:2017-08-17 02:58:58

标签: javascript json dialogflow

我正在尝试发送回webhook的回复,内容格式化为要阅读,内容格式化为口语。要在屏幕上阅读的文本将在displaytext字段中,以及在语音字段中要说出的文本。使用github.com/api-ai中的天气webhook示例,我尝试在resolve方法中添加第二个对象:

resolve(speech_output, displayText_output);

但是当用于发送响应时,不使用displayText_output:

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}));

displayText未包含在响应的JSON对象中。我认为Promise.resolve只接受一个参数进行处理,一个返回错误,如Promise(解析,拒绝)。但是,当speech_output的值用于两个字段时,displayText将包含在JSON响应中。

我想知道是否有办法实现这两个具有不同信息的字段,以便在同一答案中发送。

感谢。

1 个答案:

答案 0 :(得分:1)

最简单的方法是为变量output创建一个新的Javascript对象,以包含displayTextspeech属性。例如:

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

然后在then区块中:

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));