0

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?

Outcast
  • 4,967
  • 5
  • 44
  • 99
  • Please refer https://stackoverflow.com/questions/49363518/php-mysql-dialogflow – Nikhil Savaliya Mar 20 '18 at 13:51
  • Thanks but to refer to what? I simply use the value of the `intentName` instead of the value of `action` to determine my response. There is not any significant difference by doing either of these. – Outcast Mar 20 '18 at 14:01
  • Try debuging with `ngrok` so you can find error localy and then deploy it to heroku – Nikhil Savaliya Mar 20 '18 at 18:28

0 Answers0