为什么我的 PHP 脚本没有 "see" 来自 Dialogflow 的 webhook?

Why my PHP script does not "see" the webhook from Dialogflow?

我正在使用 DIalogflow (api.ai) 创建聊天界面。我创建了一个从 Dialogflow 到包含部署在 Heroku 上的 php 脚本的简单应用程序的 webhook。

因此,我在我的 Heroku 应用程序的 url 中放置了 Dialogflow 的 webhook 形式,类似于:https://my_heroku_app_name.herokuapp.com.

我的最终目标是从数据库中获取一些数据(通过 php 脚本),然后将它们提供给 Dialogflow。目前,我只是尝试通过 webhook 将 Heroku 应用程序(php 脚本)与 Dialogflow 连接起来。

Heroku 应用程序的 php 脚本如下:

<?php

$method = $_SERVER['REQUEST_METHOD'];

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

    $text = $json->metadata->intentName->text;

    switch ($text) {
        case 'Name':
            $speech = "This question is too personal";
            break;    
        default:
            $speech = "Sorry, I didnt get that.";
            break;
    }

    $response = new \stdClass();
    $response->speech = $speech;
    $response->displayText = $speech;
    $response->source = "webhook";
    echo json_encode($response);
}
else
{
    echo "Method not allowed";
}

?>

请记住以下几点:

为什么我的 PHP 脚本无法 "see" 来自 DIaloflow 的 webhook 并从中获取数据以做出适当的响应?

P.S。我的问题不是 Valid JSON output but still getting error 的重复问题。前者是关于 php 脚本的输入,而后者是关于 php 脚本的输出。这两件事不一定构成相同的问题。

尝试通过对代码进行一些修改来执行类似的操作。 首先,我建议您使用 action 而不是使用 intent name 作为 switch case。

index.php

<?php

require 'get_wardinfo.php';
function processMessage($input) {
    $action = $input["result"]["action"];
    switch($action){
        case 'wardinfo':
            $param = $input["result"]["parameters"]["number"];
            getWardInfo($param);
            break;
        default :
            sendMessage(array(
                "source" => "RMC",
                "speech" => "I am not able to understand. what do you want ?",
                "displayText" => "I am not able to understand. what do you want ?",
                "contextOut" => array()
            ));
    }
}
function sendMessage($parameters) {
    header('Content-Type: application/json');
    $data = str_replace('\/','/',json_encode($parameters));
    echo $data;
}
$input = json_decode(file_get_contents('php://input'), true);
if (isset($input["result"]["action"])) {
    processMessage($input);
}
?>

get_wardinfo.php

<?php
    require 'config.php';
function getWardInfo($param){
    $wardinfo="";
    $Query="SELECT * FROM public.wardinfo WHERE wardno=$param";
    $Result=pg_query($con,$Query);
    if(isset($Result) && !empty($Result) && pg_num_rows($Result) > 0){
    $row=pg_fetch_assoc($Result);
    $wardinfo= "Here is details that you require:  Name: " . $row["name"]. " --- Address: " . $row["address"]. " --- MobileNo: " . $row["contact"];

        $arr=array(
            "source" => "RMC",
            "speech" => $wardinfo,
            "displayText" => $wardinfo,
        );
        sendMessage($arr);
    }else{
        $arr=array(
            "source" => "RMC",
            "speech" => "Have some problem .",
            "displayText" => "Have some problem .",
        );
        sendMessage($arr);
    }
}
?>

您似乎了解每个参数和所有关于 dialogflow 的信息以及它如何与 PHP 数组一起工作,如果您对上述代码或方法有任何困惑,请发表评论。

我建议你不要直接选择 Heroku 首先尝试使用 ngrok 它将使你的本地服务器生效并将 URL 作为 webhook 放在 dialogflow 中并且您可以轻松调试所有错误。

我设法将 Dialogflow 连接到我在 Heroku 上的 php 脚本。

我对我的 php 脚本(在 Heroku 上)和 Dialogflow 进行了以下更改,导致了这个结果:

  1. 我将条件if($method == 'GET')替换为条件if($method == 'POST'),以便预测Dialogflow的POST请求。 请记住,在我解决整个问题之前,我没有收到任何 POST 请求,但我收到了 GET 请求,所以我认为来自 Dialogflow 的 POST 请求导致 GET 请求,因为网页重定向,当时我真的看不到。

  2. 我将 $text = $json->metadata->intentName->text; 替换为 $text = $json->results->metadata->intentName;,这是用于检索 intentName 值的正确 json 解析。 (我已经发布了 here 来自 Dialogflow 的 json 请求,但没有人注意到我的错误)

  3. 我通过 built-in 网络演示和 Slack 在 Dialogflow 上发布了我的机器人。这听起来可能无关紧要,但 Dialogflow 论坛上的一个人表示:"Maybe it should rementioned somewhere. that api.ai98 is not parsing any parameters/values/data to you service untill you bot is published!!"(请参阅此处的第二个 post:https://discuss.api.ai/t/webhook-in-php-example/229)。