Actions on google 如何通过 php 做出响应?

问题描述

这是我的 PHP 代码响应,但我收到“来自 webhook 的错误无效响应:无法将 JSON 转换为 ExecuteHttpResponse”。

这是生成 JSON 响应的 webhook 代码,但 google 使用此返回 json 抛出无效错误

$method = $_SERVER['REQUEST_METHOD'];

if($method == 'POST')
{
$requestBody = file_get_contents('PHP://input');
$json = json_decode($requestBody,true,512,JSON_BIGINT_AS_STRING);
$customer_name = $json["requestJson"]["intent"]["params"]["customer_name"]["original"];    
$returnText="Customer name is $customer_name"
$response = new \stdClass();
$response->speech = $returnText;               
$response->displayText = $returnText;
$response->source = "webhook";
echo json_encode($response);
}

带有无效错误的 webhook 响应来自 webhook 的无效响应:无法将 JSON 转换为 ExecuteHttpResponse

"responseJson": {
  "session": {
    "id": "1234"
  },"textToSpeech": "bala","displayText": "bala","source": "webhook"
}

我做错了什么?

解决方法

问题是您的响应 JSON 与 Actions on Google 期望的 response body format 不匹配。

你需要一个更像的 JSON 结构

{
  "prompt": {
    "firstSimple": {
      "speech": "bala","text": "bala"
    }
  }
}

你可能可以用 PHP 做这样的事情(未经测试):

$response = array (
  'prompt' => array (
    'firstSimple' => array (
      'speech' => $returnText,'text' => $returnText
    )
  )
);
echo json_encode( $response );

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...