实现响应PHP webhook未显示在Google的“操作”中

时间:2018-12-16 19:23:28

标签: php dialogflow actions-on-google

履行响应在Dialogflow环境中的诊断信息中可见。但是当我在Google的“操作”中对其进行测试时,它并未显示。有谁知道如何让它工作?这是我的webhook代码:

<?php

$method = $_SERVER['REQUEST_METHOD'];

if($method == 'POST'){
        $requestBody = file_get_contents('php://input');
        $json = json_decode($requestBody);

        $text = $json->queryResult->queryText;

        $date = (!empty($json->queryResult->parameters->date)) ? $json->queryResult->parameters->date : '';
        $environment  = (!empty($json->queryResult->parameters->environment)) ? $json->queryResult->parameters->environment : '';
        $intent   = (!empty($json->queryResult->intent->displayName)) ? $json->queryResult->intent->displayName : '';

        $responseText = prepareResponse($intent, $text, $date, $environment);

        $response = new \stdClass();
        $response->speech = $responseText;
        $response->displayText = $responseText;
        $response->source = "webhook";
        header("Content-type:application/json");
        echo json_encode($response);
}
else
{
        echo "Method not allowed";
}

function prepareResponse($intent, $text, $date, $environment)
    {
    return "You said: " . $text . " | I found Intent: " . $intent . " | with parameters: date=" . $date . " environment=" . $environment;    
    }
?>

1 个答案:

答案 0 :(得分:1)

Google上的操作响应应该位于payload属性下的对象中,该对象包含带有AoG response format的单个属性google

我还没有测试过,这可能不是构建它的最佳方法,但是类似以下的内容应该可以工作:

$response->payload = array(
  "google" => array(
    "expectUserResponse" => TRUE,
    "richResponse" => array(
      "items" => array(
        array(
          "simpleResponse" => array(
            "textToSpeech" => $responseText
          )
        )
      )
    )
  )
);