-1

I am using DIalogflow (api.ai) to create chat interfaces. I created a webhook from Dialogflow to a simple app containing a php script deployed on Heroku.

Therefore, I placed in the webhook form of Dialogflow the url of my Heroku app which resembles to this: https://my_heroku_app_name.herokuapp.com.

My ultimate goal is to fetch some data from a database (through the php script) and then feed Dialogflow with them. For now, I am only trying to connect the Heroku app (php script) with Dialogflow through a webhook.

The php script of the Heroku app is the following:

<?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";
}

?>

Keep in mind the following:

  • $method is GET for some reason instead of POST as it is supposed to be from Dialogflow.
  • if you try to echo any of the variables $requestBody, $json or $text then nothing is printed.
  • I have tested that the if branch is executed and that the default branch is executed at switch.

Why my PHP script cannot "see" the webhook from DIaloflow and fetch the data from it so as to respond appropriately?

P.S. My question is not a duplicate of Valid JSON output but still getting error. The former is about the input of the php script whereas the latter is about the output of the php script. These two things do not necessarily constitute identical problems.

Outcast
  • 4,967
  • 5
  • 44
  • 99
  • Possible duplicate of [Valid JSON output but still getting error](https://stackoverflow.com/questions/49394936/valid-json-output-but-still-getting-error) – ChrisGPT was on strike Mar 21 '18 at 11:35
  • "`$method` is `GET` for some reason instead of `POST` as it is supposed to be from Dialogflow." This seems to be the key issue: if you're supposed to be handling a POST request, why are you looking for GET? A GET request is unlikely to contain the payload you expect. How have you tested the branches of your code that get executed? – ChrisGPT was on strike Mar 21 '18 at 11:40
  • In any case, [please don't repost questions](https://meta.stackexchange.com/a/7054/248627). It can take some time to get an answer on SO. – ChrisGPT was on strike Mar 21 '18 at 11:42

2 Answers2

0

try to do something like this with some modification in your code. First, I suggest you to use action instead of using intent name for switch case.

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);
}
?>

get_wardinfo.php

<?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);
    }
}
?>

It seems you know each parameter and all about dialogflow and how it works with PHP arrays and all still if you have confusion in above code or method kindly put a comment.

And I will suggest you don't go for Heroku directly first try it with ngrok it will make your local server live and put the URL as webhook in dialogflow and you can easily debug the errors and all.

Nikhil Savaliya
  • 2,138
  • 4
  • 24
  • 45
  • Thanks very much for your extended answer. Finally, I solve it and I will post (in one of my posts) what I changed even though i am not entirely sure why I was encountering this problem. I did not use `ngrok` but `hurl.it` (not local) to send some requests and see more clearly what is happening. – Outcast Mar 21 '18 at 11:51
  • @Tara Please folllow this => https://github.com/googleapis/google-cloud-php-dialogflow – Nikhil Savaliya Aug 31 '19 at 04:10
0

I managed to connect Dialogflow to my php script on Heroku.

I made the following changes on my php script (on Heroku) and on Dialogflow which led to this result:

  1. I replaced the condition if($method == 'GET') with the condition if($method == 'POST') so as to anticipate the POST request of Dialogflow. Keep in mind that until I solved the whole problem I was not receiving any POST request but I GET request so I thought that the POST request from Dialogflow leads to GET request because of a webpage redirection which I could not really see at that moment.

  2. I replaced $text = $json->metadata->intentName->text; with $text = $json->results->metadata->intentName; which was the right json parsing for retrieving the value of intentName. (I have published here the json request from Dialogflow but nobody noticed my mistake)

  3. I published my bot on Dialogflow through its built-in web demo and on Slack. This may sound quite irrelevant but also one person on the Dialogflow forum stated that: "Maybe it should rementioned somewhere. that api.ai98 is not parsing any parameters/values/data to you service untill you bot is published!!" (See the second post here: https://discuss.api.ai/t/webhook-in-php-example/229).

Outcast
  • 4,967
  • 5
  • 44
  • 99