简体   繁体   中英

Actions on google how to make response via php?

This is my php code response but I'm getting "error Invalid response from webhook: Failed to translate JSON to ExecuteHttpResponse".

Here is the webhook code to generate the JSON response but google throws invalid error with this return 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 response with invalid error Invalid response from webhook: Failed to translate JSON to ExecuteHttpResponse

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

What am I doing wrong?

The problem is that your response JSON doesn't match the response body format that Actions on Google is expecting.

You need a JSON structure something more like

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

which you can probably do with PHP something like this (untested):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM