【问题标题】:Facebook messenger chatbot response issueFacebook Messenger 聊天机器人响应问题
【发布时间】:2016-11-03 05:03:40
【问题描述】:

目前我正在开发一个简单的 facebook messenger 聊天机器人。我正在使用 localhost url 通过 ngrok 进行隧道传输。我还创建了一个 facebook 应用程序和一个应该运行机器人的页面。我还为它创建了一个 webhook。一切都顺利完成,没有任何问题。但问题是我无法得到机器人的回复来工作。我正在接收用户的消息,但我无法让机器人回复。所以用户没有得到任何回复。虽然在 ngrok Web 界面中我可以看到我希望机器人回复的字符串在那里,但不知何故它不会作为回复发送给用户。这是它的代码。谁能指出错误?这是 ngrok 检查

这是我正在调用的 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);}?>

【问题讨论】:

  • 我认为您只需要确保您正在获取 curl_exec 的输出。在这个例子中,你不是。
  • 我不太明白你的建议。你能解释一下吗?你是在建议我应该采取 curl_exec();在变量中输出并返回它?
  • 查看解决方案....$output = curl_exec($ch);回声$输出;

标签: php facebook laravel chatbot


【解决方案1】:

我终于让它工作了。这是它的代码。

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

【讨论】:

    猜你喜欢
    • 2016-08-14
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多