【问题标题】:Messenger Bot in PHP: No Response BackPHP 中的 Messenger Bot:无响应返回
【发布时间】:2023-10-11 16:46:02
【问题描述】:

我正在尝试用 PHP 构建一个测试信使机器人。我的网络挂钩设置完美,甚至页面订阅也正确完成。但是,我的机器人不响应 Messenger 中的任何文本。我曾尝试更改应用程序 ID、页面 ID,以确保其中是否存在任何问题。我还尝试了各种方法,包括此处概述的基本卷曲: Facebook Chat bot (PHP webhook) sending multiple replies

并尝试了 2 个不同的 php 库: https://github.com/Fritak/messenger-platform https://github.com/pimax/fb-messenger-php

我没有收到任何 PHP 错误,挑战在 Facebook 结束时仍然成功。我的 SSL 证书很好,但我无法让机器人响应。

对此的任何帮助将不胜感激。

【问题讨论】:

  • 这个答案对你的情况有帮助...*.com/a/36616229/2990234
  • 也许是一个愚蠢的问题,但您与机器人交谈的帐户...您是否已将其作为管理员/测试员添加到应用程序中?

标签: php facebook-chatbot


【解决方案1】:

检查 CURL 是否安装正确。试试这个简单的 Gist,https://gist.github.com/visitdigital/58c71acb123870d1ac2ec714d7536587

$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];

// Set this Verify Token Value on your Facebook App 
if ($verify_token === 'YOURVERIFYTOKEN') {
  echo $challenge;
}

$input = json_decode(file_get_contents('php://input'), true);

// Get the Senders Graph ID
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];

// Get the returned message
$message = $input['entry'][0]['messaging'][0]['message']['text'];

//API Url and Access Token, generate this token value on your Facebook App Page

$url = 'https://graph.facebook.com/v2.6/me/messages?access_token=ACCESSTOKEN';
//Initiate cURL.
$ch = curl_init($url);
//The JSON data.
$jsonData = '{
    "recipient":{
        "id":"' . $sender . '"
    }, 
    "message":{
        "text":"The message you want to return"
    }
}';

//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

//Execute the request but first check if the message is not empty.
if(!empty($input['entry'][0]['messaging'][0]['message'])){
  $result = curl_exec($ch);
}

【讨论】:

  • 当我使用您的代码时,出现以下错误。 <b>Notice</b>: Undefined index: hub_challenge in <b>C:\xampp\htdocs\webhook.php</b> on line <b>2</b><br /> <br /> <b>Notice</b>: Undefined index: hub_verify_token in <b>C:\xampp\htdocs\webhook.php</b> on line <b>3</b><br />请帮我解决一下。
【解决方案2】:

您在接收消息时需要自己发送响应(请参阅documentation)。

我不知道你如何为 pimax API 做到这一点,抱歉,但对于 my API,你可以这样做:

// Messenger is calling your URL, someone is sending a message...
$messages = $bot->getMessagesReceived();

// Now you need an ID
$userToSendMessage = $messages[0]->messaging[0]->sender->id;

// Send answer
$bot->sendMessage($userToSendMessage, 'Hi!');

【讨论】:

  • 您好 Fritak,谢谢您的回答。我正在使用 index.php,只更改令牌。您的脚本已经有 sendMessage 代码,但由于某种原因它没有触发。
  • 任何人都对此有任何意见。我真的做了一切。更改了页面、应用程序、服务器。仍然没有发生。
  • 已经订阅了一个页面?没有订阅,您将无法收到任何内容。
  • @fritak,是的。已经这样做了。订阅完美结束。但在那之后就没有ping。我尝试在 api.ai 上构建一个机器人,效果很好,但我想做出动态响应并希望 PHP SDK 工作。 :(
【解决方案3】:

我遇到了同样的问题,答案是我的网络服务器正在重定向请求(在 url 的末尾添加了一个斜杠)。

【讨论】:

    【解决方案4】:

    您能检查以下内容吗?

    1. 您是该页面的管理员,您只能从管理员帐户发送消息。
    2. 您是否收到您在脚本中发送的消息,将这些消息记录在某个文件中以进行检查?
    3. 在您的页面帐户上,fb 是否会给您一些警告,例如您的页面没有收到消息。 如果没有,则 msg 已成功发送给您,问题在于您的回复。
    4. 确保您在创建 webhook 时创建的令牌放置正确。
    5. 您是否复制了生成的令牌。

    还请发送您的代码。

    【讨论】:

      【解决方案5】:

      1-验证 cURL 是否正确安装在您的计算机中
      2-尝试在您的终端中使用以下代码手动发送,确保输入您的访问令牌和收件人的 ID。 我和你有同样的问题。虽然我在我的电脑(windows)中安装了 cURL,但它不会发送请求。当我改为 linux 时它工作得很好。
      试试看。

      curl -X POST -H "Content-Type: application/json" -d '{
        "recipient": {
          "id": "USER_ID"
        },
        "message": {
          "text": "hello, world!"
        }
      }' "https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"
      

      【讨论】: