PHP 中的 Messenger Bot:没有回复
Messenger Bot in PHP: No Response Back
我正在尝试在 PHP 中构建一个测试 Messenger 机器人。我的网络挂钩设置完美,甚至页面订阅也正确完成。但是,我的机器人不响应 Messenger 中的任何文本。我曾尝试更改应用程序 ID、页面 ID,只是为了确保是否存在任何问题。我还尝试了各种方法,包括此处概述的基本卷曲:
并尝试了 2 个不同的 php 库:
https://github.com/Fritak/messenger-platform
https://github.com/pimax/fb-messenger-php
我没有收到 PHP 错误,在 Facebook 端挑战仍然成功。我的 SSL 证书没问题,但我无法让机器人响应。
如有任何帮助,我们将不胜感激。
您在接收消息时需要自己发送回复(参见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!');
检查 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);
}
你能检查一下下面的东西吗
- 您是该页面的管理员,您只能从管理员帐户发送消息。
- 您是否收到您在脚本上发送的消息,将这些消息记录在某个文件中以供检查?
- 在你的页面帐户上,fb 是否给你一些警告,比如你的页面没有收到消息。
如果没有,那么msg发送成功给你问题出在你的回复上。
- 确保您在创建 webhook 时创建的令牌放置正确。
- 你复制生成的token了吗
另外请发送您的代码。
我遇到了同样的问题,答案是我的网络服务器正在重定向请求(在 url 的末尾添加了一个斜线)。
1-验证您的计算机中是否正确安装了 cURL
2-尝试在您的终端中使用下面的代码手动发送,确保输入您的访问令牌和收件人的 ID。
我遇到了和你一样的问题。虽然我的电脑上安装了 cURL(windows),但它不会发送请求。当我更改为 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"
我正在尝试在 PHP 中构建一个测试 Messenger 机器人。我的网络挂钩设置完美,甚至页面订阅也正确完成。但是,我的机器人不响应 Messenger 中的任何文本。我曾尝试更改应用程序 ID、页面 ID,只是为了确保是否存在任何问题。我还尝试了各种方法,包括此处概述的基本卷曲:
并尝试了 2 个不同的 php 库: https://github.com/Fritak/messenger-platform https://github.com/pimax/fb-messenger-php
我没有收到 PHP 错误,在 Facebook 端挑战仍然成功。我的 SSL 证书没问题,但我无法让机器人响应。
如有任何帮助,我们将不胜感激。
您在接收消息时需要自己发送回复(参见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!');
检查 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);
}
你能检查一下下面的东西吗
- 您是该页面的管理员,您只能从管理员帐户发送消息。
- 您是否收到您在脚本上发送的消息,将这些消息记录在某个文件中以供检查?
- 在你的页面帐户上,fb 是否给你一些警告,比如你的页面没有收到消息。 如果没有,那么msg发送成功给你问题出在你的回复上。
- 确保您在创建 webhook 时创建的令牌放置正确。
- 你复制生成的token了吗
另外请发送您的代码。
我遇到了同样的问题,答案是我的网络服务器正在重定向请求(在 url 的末尾添加了一个斜线)。
1-验证您的计算机中是否正确安装了 cURL
2-尝试在您的终端中使用下面的代码手动发送,确保输入您的访问令牌和收件人的 ID。
我遇到了和你一样的问题。虽然我的电脑上安装了 cURL(windows),但它不会发送请求。当我更改为 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"