1
votes

Currently I am developing a simple facebook messenger chatbot. I am using localhost url to tunnel via ngrok. I have also created a facebook app and a page on which bot should run. I also created a webhook for it. Everything was done successfully without any issue. But the problem is I can not get the reply from bot to work. I am receiving user's message but I can not get the bot to reply. So the user is not getting anything as a reply. Although in ngrok web interface I can see that the string which I want the bot to reply is there, but somehow it doesn't get send to the user as a reply. Here is the code for it. Can anyone point out the mistake? here is the ngrok inspect

enter image description here

Here is the code of my php file that is being called.

<?php
 if (isset($_GET['hub_verify_token'])) {
if ($_GET['hub_verify_token'] === 'verify_token') {
    echo $_GET['hub_challenge'];
    return;
} else {
    echo 'Invalid Verify Token';
    return;
}}$input = json_decode(file_get_contents('php://input'), true);if (isset($input['entry'][0]['messaging'][0]['sender']['id'])) {

$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];

$jsonData = [
    'recipient' => [ 'id' => $sender],
    'message' => [ 'text' => $message]
];

$url = "https://graph.facebook.com/v2.8/me/messages?access_token=EAAQlAQ9iGz8BABZATCZAN0hd5eyJ2mCFZBR9rDuZARkEmeqh8obC0yZBpiGxFuNbAyi6HHFI2lZCCiILeFNFDuiy2Sb9OHpLfDSIBhCsv7FgglOrzZAqy9yDFlUTZCEHfRfXBYjZCQOj42Vhl4muvyGIqqqsGDP1a0FYcGo9on3QlzgKp5JL8XbZBx";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($jsonData));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_exec($ch);
curl_close($ch);}?>
1
i think you just need to be sure you're grabbing the output of curl_exec. in this example, you're not.WEBjuju
I don't quite understand your suggestion. Can you please explain more? Are you suggesting that I should take curl_exec(); output in a variable and return it?LearningNew
see the solution....$output = curl_exec($ch); echo $output;WEBjuju

1 Answers

0
votes

I finally get it to work. Here is the code for it.

<?php if (isset($_GET['hub_verify_token'])) {
if ($_GET['hub_verify_token'] === 'new_verify_token') {
    echo $_GET['hub_challenge'];
    return;
} else {
    echo 'Invalid Verify Token';
    return;
}}$input = json_decode(file_get_contents('php://input'), true);if(isset($input['entry'][0]['messaging'][0]['sender']['id'])){

$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];

$jsonData = [
    'recipient' => [ 'id' => $sender],
    'message' => [ 'text' => $message]
];

$url = "https://graph.facebook.com/v2.8/me/messages?access_token=EAAQlAQ9iGz8BAI5woul1IjMJFVcLW21ZBoZBbeBNaF80wvaPzdZBuDfEJ8NK7PPozUiVNfEjfhZAoWRJAqYHc7yiTA4J1wFOHZCs6DJYcMoPtEBuz6Icw22gNZCSjunjBcUMssXXnkmPEde4J5nU2AarXTUVxsujYPRS7ew97tCiYPDUY4tJSh";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($jsonData));
curl_exec($ch);
curl_close($ch);}?>