phpwebhooksactions-on-googlegoogle-assistantactions-builder

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?


Solution

  • 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 );