为什么我的PHP脚本没有;见;Dialogflow中的webhook?
我正在使用DIalogflow(api.ai)创建聊天界面。我创建了一个从Dialogflow到包含部署在Heroku上的php脚本的简单应用程序的webhook 因此,我在Dialogflow的webhook表单中放置了我的Heroku应用程序的url,类似于:为什么我的PHP脚本没有;见;Dialogflow中的webhook?,php,heroku,webhooks,dialogflow-es,Php,Heroku,Webhooks,Dialogflow Es,我正在使用DIalogflow(api.ai)创建聊天界面。我创建了一个从Dialogflow到包含部署在Heroku上的php脚本的简单应用程序的webhook 因此,我在Dialogflow的webhook表单中放置了我的Heroku应用程序的url,类似于:https://my_heroku_app_name.herokuapp.com 我的最终目标是(通过php脚本)从数据库中获取一些数据,然后向Dialogflow提供这些数据。现在,我只想通过webhook将Heroku应用程序(ph
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
$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";
}
?>
- 由于某种原因,
是$method
,而不是GET
,因为它应该来自DialogflowPOST
- 如果尝试回显任何变量
、$requestBody
或$json
,则不会打印任何内容$text
- 我已经测试了
分支是否被执行,以及if
分支是否在default
开关处执行
另外,我的问题不是重复的。前者是关于php脚本的输入,而后者是关于php脚本的输出。这两件事不一定构成相同的问题。尝试在代码中做一些类似的修改。 首先,我建议您使用
action
而不是使用intent name
作为开关盒
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);
}
?>
<?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);
}
}
?>
get_wardinfo.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);
}
?>
<?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中,你可以很容易地调试错误和所有错误。我在Heroku上成功地将dialogflow连接到我的php脚本 我对我的php脚本(在Heroku上)和Dialogflow进行了以下更改,这导致了这个结果:
if($method=='GET')
替换为条件if($method=='POST')
,以便预测Dialogflow的POST
请求。
请记住,在解决整个问题之前,我没有收到任何POST请求,但我收到了请求,因此我认为来自Dialogflow的POST请求导致了GET请求,因为当时我无法真正看到网页重定向$text=$json->metadata->intentName->text代码>带有$text=$json->results->metadata->intentName
这是用于检索intentName
值的正确json解析。(我已经发布了Dialogflow的json请求,但没有人注意到我的错误)
由于某种原因,“
$method
的可能重复项是GET
,而不是像Dialogflow那样的POST
。”这似乎是一个关键问题:如果你应该处理POST请求,为什么要寻找GET?GET请求不太可能包含您期望的有效负载。您是如何测试代码中执行的分支的?在任何情况下。可能需要一些时间才能得到答案。非常感谢您的扩展答案。最后,我解决了这个问题,我将(在我的一篇帖子中)发布我所做的更改,尽管我不完全确定我为什么会遇到这个问题。我没有使用ngrok
而是使用hurl。它
(非本地)发送一些请求,并更清楚地看到发生了什么。@Tara请遵循此=>