I am using DIalogflow (api.ai) to create chat interfaces. For now, I only have a simple intent called 'Name'regarding the question 'Who is name?' which responds 'name is a great person'. The json output regarding this question from Dialoglow is the following:
{
"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": {}
Notice that the json output states an error regarding the 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 $."
I want to create a webhook to (a simple app containing) a php script deployed on Heroku to give another answer. An example of this php script would be:
$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";
}
Also notice that $method
is GET
for some reason instead of POST
as it is supposed to be from Dialogflow.
Keep also in mind that if you try to echo
any of the variables $requestBody
, $json
or $text
then nothing is printed.
This is printed to the webpage where the app is deployed by 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"}
How can I fix these errors and connect dialogflow with my php script on Heroku?