简体   繁体   English

Facebook messenger聊天机器人响应问题

[英]Facebook messenger chatbot response issue

Currently I am developing a simple facebook messenger chatbot. 目前我正在开发一个简单的facebook messenger聊天机器人。 I am using localhost url to tunnel via ngrok. 我使用localhost网址通过ngrok隧道。 I have also created a facebook app and a page on which bot should run. 我还创建了一个Facebook应用程序和一个应该运行bot的页面。 I also created a webhook for it. 我还为它创建了一个webhook。 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. 虽然在ngrok web界面中我可以看到我希望机器人回复的字符串存在,但不知何故它不会作为回复发送给用户。 Here is the code for it. 这是它的代码。 Can anyone point out the mistake? 任何人都可以指出错误吗? here is the ngrok inspect 这是ngrok检查

在此输入图像描述

Here is the code of my php file that is being called. 这是我正在调用的php文件的代码。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM