Facebook – Messaging webhook keeps infinitely looping the callback php file

huangapple go评论74阅读模式
英文:

Facebook - Messaging webhook keeps infinitely looping the callback php file

问题

当我的页面收到任何通知时,回调文件会无限地自行运行而不停止。

以下是代码部分:

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();
}

因此,用户将无限地收到相同的消息而不停止。

我尝试了这里的解决方案:
https://stackoverflow.com/questions/39377533/infinite-loop-in-a-webhook

请问有人可以帮助我,让它只运行一次吗?谢谢。

英文:

while my page recive any notification .. the callback file infinitely running itself without stopping
here is the code:

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();  
  }

so the user will infinitely recive the same message without stopping
<br>

i tried the solution here
https://stackoverflow.com/questions/39377533/infinite-loop-in-a-webhook
<br>

anyone can help me with this please to make it run just once?
thank you

答案1

得分: 0

这可能不是你要寻找的答案,但我在开发自己的聊天机器人时也曾遇到过类似的问题。有两种解决方案的组合可以解决这个问题:

解决方案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,并在已处理一条消息时退出。这是必要的,因为我在API中一直会收到某些消息2或3次,我必须避免重复回答相同的消息。这是一个简短的摘录:

$mid = $input['entry'][0]['messaging'][0]['message']['mid'];
$received = $this->user->isReceived($mid); // 为了避免重复对同一条消息做出反应
if($received) {
    exit();
} else {
    $this->user->logMsg($mid);
}

isReceived函数中,我检查自定义消息ID表是否已包含此消息。在logMsg函数中,我存储了该ID。然而,它们有点复杂且依赖于框架,因此你将不得不根据你的项目情况自行编写这些函数。

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

英文:

This is probably not the answer you're looking for, but i've struggled with this same problem while developing a chatbot of my own. There is a combination of 2 solutions that fixed this issue:

Solution 1: If there is no input, or no message text and payload, the code exits. Here is an excerpt from my code:

$input = json_decode(file_get_contents(&#39;php://input&#39;), true);
if(empty($input))
	exit();

$messageText = $input[&#39;entry&#39;][0][&#39;messaging&#39;][0][&#39;message&#39;][&#39;text&#39;];
$messagePayload = $input[&#39;entry&#39;][0][&#39;messaging&#39;][0][&#39;postback&#39;][&#39;payload&#39;];

if(empty($messageText) &amp;&amp; empty($messagePayload)) {
	exit();
}

This was necessary because the chatbot just seemed to keep spamming my entry-point with empty requests over and over again. However...

Solution 2: I saved all message id-s in the database, and exit out if one is already being processed. This was necessary because I kept receiving certain messages 2 or 3 times in the API, and I had to avoid answering the same message twice. Here is a short excerpt:

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

In the isReceived function, I check if the custom table of message id-s already contains this message. In the logMsg function, I store the id. They are a bit more complicated and framework-dependant however, so you will have to write these functions on your own, as you see them fit for your project.

Good luck, I hope someone chimes in with a better solution.

答案2

得分: 0

try {
  // 返回一个 `FacebookFacebookResponse` 对象
  $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;
英文:
try {
  // Returns a `FacebookFacebookResponse` object
  $response = $fb-&gt;get(
    &#39;/{to_id}?fields=conversations{messages{message}}&#39;,
    $accesstoken
  );
} catch(FacebookExceptionsFacebookResponseException $e) {
  echo &#39;Graph returned an error: &#39; . $e-&gt;getMessage();
  exit;
} catch(FacebookExceptionsFacebookSDKException $e) {
  echo &#39;Facebook SDK returned an error: &#39; . $e-&gt;getMessage();
  exit;
}
$idchk = $response-&gt;getGraphNode();
$datachk = json_decode($idchk);
if (($data-&gt;object == &quot;page&quot;)&amp;&amp;(strpos($idchk,&#39;hey&#39;)==&quot;&quot;)) {   
  try {
    $response = $fb-&gt;post(
      &#39;/me/messages&#39;,
      array (
        &#39;recipient&#39; =&gt; &#39;{
          &quot;id&quot;: &quot;{to_id}&quot;
        }&#39;,
        &#39;message&#39; =&gt; &#39;{
          &quot;text&quot;: &quot;hey&quot;
        }&#39;
      ),
     $accesstoken
    );
    
    exit;
  } catch(FacebookExceptionsFacebookResponseException $e) {
    echo &#39;Graph returned an error: &#39; . $e-&gt;getMessage();
    
    exit;

  } catch(FacebookExceptionsFacebookSDKException $e) {

    echo &#39;Facebook SDK returned an error: &#39; . $e-&gt;getMessage();

}}

    exit;

huangapple
  • 本文由 发表于 2020年1月3日 22:15:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定