I'm developing a bot (Facebook Messenger) and it is not responding, the 'file_get_contents' values returns null/empty.
$update_response = file_get_contents("php://input");
First I added an application to an existing page, it did not work. So I created a new page and a new application, but the error remained. I thought it might be a setting on the page or in the application.
What can be wrong?
<?php
require('parser.php');
define('BOT_TOKEN', 'FACEBOOK TOKEN');
define('VERIFY_TOKEN', 'MY TOKEN');
define('API_URL', 'https://graph.facebook.com/v2.6/me/messages?access_token='.BOT_TOKEN);
$hub_verify_token = null;
function processMessage($message) {
// processa a mensagem recebida
$sender = $message['sender']['id'];
$text = $message['message']['text'];//texto recebido na mensagem
if (isset($text)) {
if ($text === "Mega-Sena") {
sendMessage(array('recipient' => array('id' => $sender), 'message' => array("text" => getResult('megasena', $text))));
} else if ($text === "Quina") {
sendMessage(array('recipient' => array('id' => $sender), 'message' => array("text" => getResult('quina', $text))));
} else if ($text === "Lotomania") {
sendMessage(array('recipient' => array('id' => $sender), 'message' => array("text" => getResult('lotomania', $text))));
} else if ($text === "Lotofácil" || $text === "Lotofacil") {
sendMessage(array('recipient' => array('id' => $sender), 'message' => array("text" => getResult('lotofacil', $text))));
} else {
sendMessage(array('recipient' => array('id' => $sender), 'message' => array('text' => 'Olá! Eu sou um bot que informa os resultados das loterias da Caixa. Será que você ganhou dessa vez? Para começar, digite o nome do jogo (Lotomania, Quina, Mega ou Lotofácil) para o qual deseja ver o resultado.')));
}
}
}
function sendMessage($parameters) {
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode($parameters),
'header'=> "Content-Type: application/json
" .
"Accept: application/json
"
)
);
$context = stream_context_create( $options );
file_get_contents(API_URL, false, $context );
}
if(isset($_REQUEST['hub_challenge'])) {
$challenge = $_REQUEST['hub_challenge'];
$hub_verify_token = $_REQUEST['hub_verify_token'];
}
if ($hub_verify_token === VERIFY_TOKEN) {
echo $challenge;
}
$update_response = file_get_contents("php://input");
//-----VERIFY LOG-----//
$fh = fopen("log.txt", "w+");
fwrite($fh, $update_response);
fclose($fh);
$update = json_decode($update_response, true);
if (isset($update['entry'][0]['messaging'][0])) {
processMessage($update['entry'][0]['messaging'][0]);
}
?>
</div>