Facebook Messenger Webhook JSON file_get_contents为空

时间:2017-06-27 22:10:40

标签: php facebook webhooks messenger

我正在开发一个机器人(Facebook Messenger)并且它没有响应,' file_get_contents' values返回null / empty。

$ update_response = file_get_contents(" php:// input");

首先,我将一个应用程序添加到现有页面,但它不起作用。 所以我创建了一个新页面和一个新的应用程序,但错误仍然存​​在。 我认为它可能是页面上或应用程序中的设置。

有什么不对?



<?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\r\n" .
                "Accept: application/json\r\n"
    )
);

$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]);
}

?>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可以尝试使用精简代码并检查以下内容:

  1. 如果您的网络服务器访问日志显示来自Facebook的点击

  2. 如果有任何错误(您也可以使用简单的curl cli命令进行检查。

  3. 如果logs.txt显示任何内容。

  4. &#13;
    &#13;
    WebSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
    WebSettings.setAppCacheEnabled(true);
    WebSettings.setDomStorageEnabled(true);
    
    &#13;
    &#13;
    &#13;