首页 > 解决方案 > 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);

什么都没有改变,因为我认为我需要到达有效载荷部分。

标签: node.jsjsonaws-lambdaactions-on-google

解决方案


原来 AWS API Gateway 中有一个名为:Use Lambda Proxy Integration的复选框选项。

选择后,它会从我的代码中按原样返回 JSON ,而不需要额外的格式。

使用 Lambda 代理集成


推荐阅读