[英]Facebook Messenger bot stops working after displaying generic template
該漫游器回復完美,但在顯示通用/列表模板后停止工作。 對於其他消息,例如快速答復,按鈕和文本消息,它可以正常工作。 我的應用程序處於開發模式,因此將其更改為公開模式,但無法正常工作。 我已經附上了我的代碼和屏幕截圖。
我的代碼有什么問題?
API版本:v2.10
面子書頁面: https : //www.facebook.com/yesira.net
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class BotController extends Controller
{
public function bot(Request $request)
{
$data = $request->all();
//get the user’s id
$id = $data["entry"][0]["messaging"][0]["sender"]["id"];
$senderMessage = $data["entry"][0]["messaging"][0]["message"];
$messageText = $data["entry"][0]["messaging"][0]["message"]["text"];
if(!empty($senderMessage) && $messageText == "hi"){
$this->sendTextMessage($id, "Hi buddy");
}
else if($messageText == "blog") {
$answer = ["attachment"=>[
"type"=>"template",
"payload"=>[
"template_type"=>"generic",
"elements"=>[
[
"title"=>"Migrate your symfony application",
"item_url"=>"https://www.cloudways.com/blog/migrate-symfony-from-cpanel-to-cloud-hosting/",
"image_url"=>"https://www.cloudways.com/blog/wp-content/uploads/Migrating-Your-Symfony-Website-To-Cloudways-Banner.jpg",
"subtitle"=>"Migrate your symfony application from Cpanel to Cloud.",
"buttons"=>[
[
"type"=>"web_url",
"url"=>"www.cloudways.com",
"title"=>"View Website"
],
[
"type"=>"postback",
"title"=>"Start Chatting",
"payload"=>"Even want some more? say me!"
]
]
]
]
]
]];
$response = [
'recipient' => [ 'id' => $id ],
'message' => $answer
];
$this->reply($response);
}
else{
$this->sendTextMessage($id, "Hello world!");
}
}
private function reply($jsonData)
{
$ch = curl_init('https://graph.facebook.com/v2.6/me/messages?access_token=' . env("PAGE_ACCESS_TOKEN"));
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($jsonData));
curl_exec($ch);
curl_close($ch);
}
private function sendTextMessage($recepientId, $messageText)
{
$messageData = [
"recipient" => [
"id" => $recepientId,
],
"message" => [
"text" => $messageText,
]
];
$ch = curl_init('https://graph.facebook.com/v2.6/me/messages?access_token=' . env("PAGE_ACCESS_TOKEN"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($messageData));
curl_exec($ch);
curl_close($ch);
}
}
可能您沒有收到新事件,因為平台不斷重試。 您需要以200OK響應webhook事件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.