Facebook 聊天机器人(PHP webhook)发送多个回复
Facebook Chat bot (PHP webhook) sending multiple replies
我的 Facebook 聊天机器人正在运行,但它在我向它发送初始消息后发回多条消息。这是我的 webhook 脚本(我很欣赏这是一个非常粗糙的工作示例):
$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];
if ($verify_token === 'MY_VERIFICATION_TOKEN') {
echo $challenge;
}
$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
//API Url
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token=<my-token>';
//Initiate cURL.
$ch = curl_init($url);
//The JSON data.
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"Hey Lee!"
}
}';
//Encode the array into JSON.
$jsonDataEncoded = $jsonData;
//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, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request
$result = curl_exec($ch);
FB 使用原始传入消息访问您的 webhook url,然后您对其进行处理。然后,您将向用户发回响应,脚本结束。然后,一旦消息传递给用户,FB 就会向 webhook url 发送传递确认。由于您的脚本总是设置为在任何时候调用它时发送 "Hey Lee!" ,因此传递回调实际上触发了另一条消息的发送,然后另一个传递确认出现,然后该过程自行重复。要解决此问题,请在您的代码周围放置一个 if 语句以发送消息。这是一个例子。
$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];
if ($verify_token === 'MY_VERIFICATION_TOKEN') {
echo $challenge;
}
$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
//API Url
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token=<my-token>';
//Initiate cURL.
$ch = curl_init($url);
if($message=="hello")
{
//The JSON data.
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"Hey Lee!"
}
}';
}
//Encode the array into JSON.
$jsonDataEncoded = $jsonData;
//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, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request
$result = curl_exec($ch);
希望对您有所帮助。
我认为这是因为你没有验证发送的消息是否为空:
试试这个:
$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];
if ($verify_token === 'MY_VERIFICATION_TOKEN') {
echo $challenge;
}
$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
//API Url
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token=<my-token>';
//Initiate cURL.
$ch = curl_init($url);
//The JSON data.
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"Hey Lee!"
}
}';
//Encode the array into JSON.
$jsonDataEncoded = $jsonData;
//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, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request
if(!empty($input['entry'][0]['messaging'][0]['message'])){
$result = curl_exec($ch);
}
试过一样,第一个请求包含实际用户消息,其他请求没有。如果
我只是发送一个答案
$message = $input['entry'][0]['messaging'][0]['message']['text'];
不为空:
if ($message){
//send your message here
}
我的 Facebook 聊天机器人正在运行,但它在我向它发送初始消息后发回多条消息。这是我的 webhook 脚本(我很欣赏这是一个非常粗糙的工作示例):
$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];
if ($verify_token === 'MY_VERIFICATION_TOKEN') {
echo $challenge;
}
$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
//API Url
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token=<my-token>';
//Initiate cURL.
$ch = curl_init($url);
//The JSON data.
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"Hey Lee!"
}
}';
//Encode the array into JSON.
$jsonDataEncoded = $jsonData;
//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, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request
$result = curl_exec($ch);
FB 使用原始传入消息访问您的 webhook url,然后您对其进行处理。然后,您将向用户发回响应,脚本结束。然后,一旦消息传递给用户,FB 就会向 webhook url 发送传递确认。由于您的脚本总是设置为在任何时候调用它时发送 "Hey Lee!" ,因此传递回调实际上触发了另一条消息的发送,然后另一个传递确认出现,然后该过程自行重复。要解决此问题,请在您的代码周围放置一个 if 语句以发送消息。这是一个例子。
$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];
if ($verify_token === 'MY_VERIFICATION_TOKEN') {
echo $challenge;
}
$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
//API Url
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token=<my-token>';
//Initiate cURL.
$ch = curl_init($url);
if($message=="hello")
{
//The JSON data.
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"Hey Lee!"
}
}';
}
//Encode the array into JSON.
$jsonDataEncoded = $jsonData;
//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, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request
$result = curl_exec($ch);
希望对您有所帮助。
我认为这是因为你没有验证发送的消息是否为空:
试试这个:
$challenge = $_REQUEST['hub_challenge'];
$verify_token = $_REQUEST['hub_verify_token'];
if ($verify_token === 'MY_VERIFICATION_TOKEN') {
echo $challenge;
}
$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
//API Url
$url = 'https://graph.facebook.com/v2.6/me/messages?access_token=<my-token>';
//Initiate cURL.
$ch = curl_init($url);
//The JSON data.
$jsonData = '{
"recipient":{
"id":"'.$sender.'"
},
"message":{
"text":"Hey Lee!"
}
}';
//Encode the array into JSON.
$jsonDataEncoded = $jsonData;
//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, $jsonDataEncoded);
//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//Execute the request
if(!empty($input['entry'][0]['messaging'][0]['message'])){
$result = curl_exec($ch);
}
试过一样,第一个请求包含实际用户消息,其他请求没有。如果
我只是发送一个答案
$message = $input['entry'][0]['messaging'][0]['message']['text'];
不为空:
if ($message){
//send your message here
}