【问题标题】:Actions on google how to make response via php?谷歌上的操作如何通过 php 做出响应?
【发布时间】:2021-04-05 16:07:01
【问题描述】:

这是我的 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"
}

我做错了什么?

【问题讨论】:

    标签: php webhooks actions-on-google google-assistant actions-builder


    【解决方案1】:

    问题在于您的响应 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 );
    

    【讨论】:

    • 仍然得到没有错误的空输出,谷歌说“对不起,我没有得到任何回应”。有什么要做的更多步骤吗?还是我错过了什么?
    • 将代码放入 cmets 对 StackOverflow 来说是个坏主意。更好的办法是更新问题,或者,既然您已经接受了这个答案,请继续创建一个新问题。
    猜你喜欢
    • 2021-06-28
    • 2018-12-15
    • 2017-11-30
    • 1970-01-01
    • 2017-03-27
    • 2012-01-18
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多