【问题标题】:Facebook - Messaging webhook keeps infinitely looping the callback php fileFacebook - 消息传递 webhook 不断循环回调 php 文件
【发布时间】:2020-04-22 02:34:37
【问题描述】:

当我的页面收到任何通知时 .. 回调文件无限地自行运行而不会停止 这是代码:

require_once 'vendor/autoload.php';   
$fb = new \Facebook\Facebook([
  'app_id' => '{app-id}',           
  'app_secret' => '{app_secret}',   
  'graph_api_version' => 'v5.0',
]);
$feedData = file_get_contents('php://input');
$data = json_decode($feedData);
if ($data->object == "page") {   
  try {
    $response = $fb->post(
      '/me/messages',
      array (
        'recipient' => '{
          "id": "{id}"
        }',
        'message' => '{
          "text": "{text}"
        }'
      ),
     $accesstoken
    );
  } catch(FacebookExceptionsFacebookResponseException $e) {
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
  } catch(FacebookExceptionsFacebookSDKException $e) {
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
  }
  $graphNode = $response->getGraphNode();  
  }

因此用户将无限地接收相同的消息而不会停止

我在这里尝试了解决方案 infinite loop in a WebHook

任何人都可以帮我解决这个问题,让它只运行一次吗? 谢谢

【问题讨论】:

    标签: php facebook bots messenger


    【解决方案1】:
    try {
      // Returns a `FacebookFacebookResponse` object
      $response = $fb->get(
        '/{to_id}?fields=conversations{messages{message}}',
        $accesstoken
      );
    } catch(FacebookExceptionsFacebookResponseException $e) {
      echo 'Graph returned an error: ' . $e->getMessage();
      exit;
    } catch(FacebookExceptionsFacebookSDKException $e) {
      echo 'Facebook SDK returned an error: ' . $e->getMessage();
      exit;
    }
    $idchk = $response->getGraphNode();
    $datachk = json_decode($idchk);
    if (($data->object == "page")&&(strpos($idchk,'hey')=="")) {   
      try {
        $response = $fb->post(
          '/me/messages',
          array (
            'recipient' => '{
              "id": "{to_id}"
            }',
            'message' => '{
              "text": "hey"
            }'
          ),
         $accesstoken
        );
    
        exit;
      } catch(FacebookExceptionsFacebookResponseException $e) {
        echo 'Graph returned an error: ' . $e->getMessage();
    
        exit;
    
      } catch(FacebookExceptionsFacebookSDKException $e) {
    
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
    
    }}
    
        exit;
    

    【讨论】:

      【解决方案2】:

      这可能不是您要寻找的答案,但我在开发自己的聊天机器人时遇到了同样的问题。有两种解决方案的组合可以解决此问题:

      解决方案 1: 如果没有输入,或者没有消息文本和有效负载,则代码退出。这是我的代码的摘录:

      $input = json_decode(file_get_contents('php://input'), true);
      if(empty($input))
          exit();
      
      $messageText = $input['entry'][0]['messaging'][0]['message']['text'];
      $messagePayload = $input['entry'][0]['messaging'][0]['postback']['payload'];
      
      if(empty($messageText) && empty($messagePayload)) {
          exit();
      }
      

      这是必要的,因为聊天机器人似乎只是一遍又一遍地向我的入口点发送空请求。不过……

      解决方案 2: 我将所有消息 id-s 保存在数据库中,如果一个已经在处理中,则退出。这是必要的,因为我在 API 中不断收到某些消息 2 或 3 次,而且我不得不避免两次回答相同的消息。这是一个简短的摘录:

      $mid = $input['entry'][0]['messaging'][0]['message']['mid'];
      $received = $this->user->isReceived($mid); // To avoid reacting to the same message twice
      if($received) {
          exit();
      } else {
          $this->user->logMsg($mid);
      }
      

      isReceived 函数中,我检查消息id-s 的自定义表是否已经包含此消息。在 logMsg 函数中,我存储了 id。然而,它们有点复杂且依赖于框架,因此您必须自己编写这些函数,因为您认为它们适合您的项目。

      祝你好运,我希望有人提出更好的解决方案。

      【讨论】:

        猜你喜欢
        • 2016-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-21
        相关资源
        最近更新 更多