node.js - Google Assistant 的履行响应带有转义字符“\”
问题描述
我使用Google Client Library 上的 Actions创建了一个简单的 webhook 来实现 Google Action 意图。此 webhook 使用以下代码托管在 AWS Lambda 函数上:
'use strict';
// Import the Dialogflow module from the Actions on Google client library.
const {dialogflow} = require('actions-on-google');
// Instantiate the Dialogflow client.
const app = dialogflow({debug: true});
// Handle the Dialogflow intent named 'favorite color'.
// The intent collects a parameter named 'color'.
app.intent('favorite color', (conv, {color}) => {
const luckyNumber = color.length;
// Respond with the user's lucky number and end the conversation.
conv.close('Your lucky number is ' + luckyNumber);
});
// Set the DialogflowApp object to handle the HTTPS POST request.
exports.fulfillment = app;
我的问题是响应以这种形式返回给助手:
{
"statusCode": 200,
"body": "{\"payload\":{\"google\":{\"expectUserResponse\":false,\"richResponse\":{\"items\":[{\"simpleResponse\":{\"textToSpeech\":\"Your lucky number is 3\"}}]}}},\"fulfillmentText\":\"Your lucky number is 3\"}",
"headers": {
"content-type": "application/json;charset=utf-8"
}
}
如您所见,正文带有插入的转义字母,导致履行失败。
我尝试了以下方法:
JSON.stringify(conv.close('Your lucky number is ' + luckyNumber));
JSON.parse(conv.close('Your lucky number is ' + luckyNumber));
JSON.parse(conv.close('Your lucky number is ' + luckyNumber).body);
什么都没有改变,因为我认为我需要到达有效载荷部分。
解决方案
推荐阅读
- django - 从嵌套循环访问变量
- r - 如何重新排列在 dplyr 中易于查看的汇总统计信息?
- typescript - TypeScript:任何包含字段并返回转换对象的接口的类型
- r - 从 colnames 子集日期
- bash - 为什么在我的 PS1 末尾添加 \n 会导致替换错误?
- kdb - 创建一个空表,从另一个表中获取列名
- linux - 需要用一个变量比较两个字符串
- java - 从recycleview解析Android Html
- javascript - 未捕获的 SyntaxError:无效的正则表达式:/@|#|$|&|*/:没有可重复的内容
- c++ - 12:00:01 AM 是有效时间吗?