首页 > 解决方案 > 无法在 dialogflow(api.ai) 中解析 webhook json 响应

问题描述

我收到以下错误

错误:无法解析 webhook JSON 响应:找不到字段:消息 google.cloud.dialogflow.v2.WebhookResponse 中的语音

<?php 
$method = $_SERVER['REQUEST_METHOD'];
// Process only when method is POST
if($method == 'POST'){
    $requestBody = file_get_contents('php://input');
    $json = json_decode($requestBody);
    $text = $json->result->parameters->text;
    switch ($text) {
        case 'hi':
            $speech = "Hi, Nice to meet you";
            break;
        case 'bye':
            $speech = "Bye, good night";
            break;
        case 'anything':
            $speech = "Yes, you can type anything here.";
            break;

        default:
            $speech = "Sorry";
            break;
    }
    $response = new \stdClass();
    $response->speech = "$speech";
    $response->displayText = "$speech";
    $response->source = 'source-of-the-response';
    $response->return = "$speech";
    $response->fulfillmentText = "$speech";
    echo json_encode($response);
}
else
{
    $speech = "Ok";
    $response->speech = "$speech";
    echo json_encode($response);
}
?>

标签: phpchatbotdialogflow-es

解决方案


推荐阅读