So, I want to create a messanger bot using php. I've successfully set up webhooks (I'm using XAMPP + ngrok) and when I send a message to bot I see that webhook is triggered (in ngrok inspector panel I see POST request with headers and payload). Then I try to send a message using curl and I can't do it. I think I have problem directly in the link itslef, but don't know how to fix it. Here's the code:
<?php
if (isset($_GET['hub_mode']) && isset($_GET['hub_challenge']) && isset($_GET['hub_verify_token'])) {
if ($_GET['hub_verify_token'] == '1234')
echo $_GET['hub_challenge'];
}else{
$access_token = 'MY_TOKEN';
$response = file_get_contents('php://input');
$response = json_decode($response, true);
$message = $response['entry'][0]['messaging'][0]['message']['text'];
$id_recepient = $response['entry'][0]['messaging'][0]['sender']['id'];
$reply_message = '{
"messaging_type":"RESPONSE",
"recipient": {"id": "'.$id_recepient.'"},
"message":{
"text": "Hiii"
}
}';
function send_reply($reply, $page_id, $access_token){
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "message=$reply&access_token=$access_token");
curl_setopt($ch, CURLOPT_URL, "https://graph.facebook.com/v16.0/$page_id/messages");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
curl_setopt($ch, CURLOPT_POSTFIELDS, $reply);
$response = curl_exec($ch);
file_put_contents('ch.txt', $ch);
if (curl_errno($ch)) {
file_put_contents('error.txt', curl_error($ch).curl_errno($ch));
}
curl_close($ch);
$result = json_decode($response, TRUE);
file_put_contents('encoded.txt', json_encode($result));
return $result;
}
send_reply($reply_message, $page_id, $access_token);
}
?>
As you can see, I tried to write in file 'encoded.txt' response and I have this in it:
{"error":{"message":"(#2) Service temporarily unavailable","type":"OAuthException","is_transient":true,"code":2,"fbtrace_id":"AZ3OkaRaJr4YJ5s4p8hJxV3"}}