我正在使用DIalogflow(api.ai)来创建聊天界面。现在,我只有一个名为'Name'的简单意图,问题是'谁是名称?'响应'名称是一个伟大的人'。关于Dialoglow这个问题的json输出如下:
{
"id": "...",
"timestamp": "2018-03-20T12:38:27.972Z",
"lang": "en",
"result": {
"source": "agent",
"resolvedQuery": "Who is John",
"action": "",
"actionIncomplete": false,
"parameters": {
"given-name": "John"
},
"contexts": [],
"metadata": {
"intentId": "...",
"webhookUsed": "true",
"webhookForSlotFillingUsed": "false",
"webhookResponseTime": 242,
"intentName": "Name"
},
"fulfillment": {
"speech": "John is a",
"messages": [
{
"type": 0,
"speech": "John is a"
}
]
},
"score": 1
},
"status": {
"code": 206,
"errorType": "partial_content",
"errorDetails": "Webhook call failed. Error: Failed to parse webhook JSON response: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $.",
"webhookTimedOut": false
},
"sessionId": "491d57cb-0af2-45ac-a658-9e47ec6658ce",
"alternativeResultsFromKnowledgeService": {}
请注意,json输出表明有关webhook的错误:"Webhook call failed. Error: Failed to parse webhook JSON response: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $."
我想创建一个webhook(包含一个简单的应用程序)部署在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";
}
另请注意,$method
由于某种原因GET
而不是POST
,因为它应该来自Dialogflow。
另请注意,如果您尝试echo
任何变量$requestBody
,$json
或$text
,则不会打印任何内容。
这将打印到Heroku部署应用程序的网页上:
{"speech":"Sorry, I didnt get that. Please ask me something else.","displayText":"Sorry, I didnt get that. Please ask me something else.","source":"webhook"}
如何修复这些错误并使用Heroku上的php脚本连接对话框?