首页 文章

如何从POST请求“看到”json响应(webhook)

提问于
浏览
2

我在Dialogflow中创建了一个聊天机器人,它告诉用户我(扩展)家庭的成员以及他们住在哪里 . 我已经创建了一个包含MySQL的小型数据库,其中存储了这些数据,并且只要这是合适的,我就会使用PHP脚本(在Heroku上托管)来获取它们,具体取决于用户与聊天机器人的交互 . 我的PHP脚本从Dialogflow接收POST请求(webhook)如下:

<?php

$dbServername = '******************';
$dbUsername = '******************';
$dbPassword = '******************';
$dbName = '******************';
$conn = mysqli_connect($dbServername, $dbUsername, $dbPassword, $dbName);

header('Content-Type: application/json');
$method = $_SERVER['REQUEST_METHOD'];

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

    $action = $json->result->action;
    $first_name = $json->result->contexts[0]->parameters->{'given-name'};
    $last_name = $json->result->contexts[0]->parameters->{'last-name'};
    $lifespan = $json->result->contexts[0]->lifespan;

    $sql = "SELECT * FROM family WHERE name LIKE '%$first_name%$last_name%';";
    $result = mysqli_query($conn, $sql);
    $resultCheck = mysqli_num_rows($result);
    if ($resultCheck > 0) {
       while ($row = mysqli_fetch_assoc($result)) {
            $person = $row;
       }

       switch ($action) {
           case 'Name':
               $speech= "$first_name is my" . $person["name"] . ".";
               break;  
           case 'Location':
               $speech = "$first_name is living in {$person["location"]}.";
               break;
           default:
               $speech = "Please ask me something more relevant to my family";
               break;
       } 
    }
    else {

        $speech = "Sorry, $first_name $last_name is not a member of my family.";

    }

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

I can instantly see on Dialogflow the json response that I am receiving from it in my PHP script. However, Google Assistant does not provide this option. The problem also is that the json response when using Google Assistant is considerably different than the one when using only Dialogflow.

My question is: how can I "see" what JSON is being sent to my PHP script when using the Google Assistant? In other words, how can I "see" the whole of $requestBody variable at once?

例如,我尝试使用https://webhook.site/并填写以下信息以创建新的URL / endpoints :

Default status code ->  200
Content Type -> application/json
Timeout before response -> 0
Response body -> {
 "speech": "WEBHOOK",
 "displayText": "WEBHOOK",
 "source": "agent"
}

响应主体具有与PHP脚本相同的结构 . 但是,出于某种原因,Google智能助理不会从此自定义 endpoints 收到json响应(而Dialogflow确实会收到它完美) . 因此,我不能完全继续前进,看看Dialogflow和Google智能助理发送的是什么,意图由上下文进一步触发...

1 回答

  • 0

    Easy solution: add logging

    在代码中添加一些错误记录,打印出格式化的JSON . 在您创建 $json 之后,这样的内容会将其记录到您的普通HTTP日志文件中:

    error_log( json_encode( $json, JSON_PRETTY_PRINT ) );
    

    然后,您可以在每次请求后检查HTTP错误日志,以查看发送的内容 . (正如您在评论中指出的那样,您可以在项目目录的终端中的heroku上使用 heroku logs 执行此操作 . )

    如果要将其发送到其他位置,可以查看error_log()的文档,以获取有关如何将其发送到电子邮件地址(如果您的配置支持)或其他文件的详细信息 . 例如,这会将事物记录到名为 /tmp/json.txt 的文件中:

    error_log( json_encode( $json, JSON_PRETTY_PRINT ), 3, "/tmp/json.txt" );
    

    More complicated solution: use a proxy

    您还可以使用允许请求检查的代理,例如ngrok . 这将为您提供一个公共主机名,您将设置该主机名转发到运行服务的主机名 . 然后,您可以将此公共主机名用于Dialogflow中的实现URL以及webhook的路径 . 当Dialogflow发送请求时,它将转到此代理,该代理会将其转发给您的服务 . 您的服务回复代理,代理将其转发回Dialogflow . 您可以检查请求和响应 . (ngrok在与服务相同的机器上运行,并允许通过使用另一个可以查看请求和响应的URL进行检查 . 其他代理可能有不同的工作.webhook.site看起来像它做了类似的事情,但我还没有测试过它的代理工作 . )

相关问题