0

I am creating a chatbot using Dialogflow. I need to use the webhook service. I have been able to successfully read the webhook request generated at my server using PHP. I am able to do use the data from webhook to do necessary stuff. But, I am not able to figure out the way to send JSON respone back to Dialogflow from PHP.

Code that I have tried:

<?php
    $webhookContent = "";
    $webhook = fopen('php://input' , 'rb');
    while (!feof($webhook)) {
        $webhookContent .= fread($webhook, 4096);
    }
    $data = json_decode($webhookContent);
    $json = '{"fulfillmentMessages": [{"text": {"text": ["Text response from webhook"]}}]}';
    $myfile = fopen("chatbot.txt", "a") or die("Unable to open file!");
    fwrite($myfile, $webhookContent."\r\n");
    fclose($myfile);
    echo json_encode($json);
    http_response_code(200);
?>

Above is a dummy of code that I am using. To send response I have tried the following options but nothing is working.

echo json_encode($json);
return json_encode($json);
return $json;

On Dialogflow it shows [empty response]

Parminder
  • 161
  • 2
  • 3
  • 14
  • Be sure absolutely nothing else is being output before or after your json echo. This includes even php warnings and notices. Also, that service may be wanting you to formulate the `header` as json response. This may help: https://stackoverflow.com/a/4064468/2960971 – IncredibleHat Jul 28 '20 at 12:27
  • Oh, and move that `http_response_code` above your echo. That may be tossing a warning about 'headers already output blah blah blah'. – IncredibleHat Jul 28 '20 at 12:29
  • I was using ```http_response_code(200)``` and I've removed that added the header response before echo. Still [empty response]. I was just wondering that how is the response directed to webhook url. Do we also require the url from where the webhook originated? – Parminder Jul 28 '20 at 12:31
  • A webhook is a process where an external server is hitting YOU (your url, your php) with data. They are informing your server of some action. You then deal with that input data. If their call requires you to respond back with something, then what you respond back is based on their docs and such. Looking at your code, I'm kind of curious why they would want back what they sent you. – IncredibleHat Jul 28 '20 at 12:33
  • Okay. So whatever I respond back will be directed to where the original request came from. I am trying to get user input from chatbot, do some processing on the received input and send the output to be shown on bot. Dialogflow requires the output in JSON. – Parminder Jul 28 '20 at 12:36
  • I see, I'm looking at the docs. Right, they want a json object returned with something that you are to display back. I see the problem in your test case... you are trying to `json_encode($json);` a string that is already json. (along with the header code after the echo, which can create a php notice text addition to the output). – IncredibleHat Jul 28 '20 at 12:39
  • Yeah, right. I removed ```json_encode``` and everything is working fine. Thanks! Cheers! – Parminder Jul 28 '20 at 12:40

0 Answers0