第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Facebook - 消息傳遞 webhook 不斷循環(huán)回調(diào) php 文件

Facebook - 消息傳遞 webhook 不斷循環(huán)回調(diào) php 文件

PHP
蕪湖不蕪 2022-07-16 09:41:45
當(dāng)我的頁面收到任何通知時..回調(diào)文件無限運(yùn)行而不會在這里停止是代碼: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();    }因此用戶將無限地接收相同的消息而不會停止我 在 WebHook 中嘗試了無限循環(huán)的解決方案任何人都可以幫我解決這個問題,讓它只運(yùn)行一次嗎?謝謝你
查看完整描述

2 回答

?
寶慕林4294392

TA貢獻(xiàn)2021條經(jīng)驗 獲得超8個贊

這可能不是您正在尋找的答案,但我在開發(fā)自己的聊天機(jī)器人時也遇到了同樣的問題。有兩種解決方案的組合可以解決此問題:


解決方案 1:如果沒有輸入,或者沒有消息文本和有效負(fù)載,則代碼退出。這是我的代碼的摘錄:


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

}

這是必要的,因為聊天機(jī)器人似乎只是一遍又一遍地用空請求向我的入口點(diǎn)發(fā)送垃圾郵件。然而...


解決方案 2:我將所有消息 id-s 保存在數(shù)據(jù)庫中,如果一個已經(jīng)在處理中,則退出。這是必要的,因為我在 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函數(shù)中,我檢查消息 id-s 的自定義表是否已包含此消息。在logMsg函數(shù)中,我存儲了 id。然而,它們有點(diǎn)復(fù)雜并且依賴于框架,因此您必須自己編寫這些函數(shù),因為您認(rèn)為它們適合您的項目。


祝你好運(yùn),我希望有人提出更好的解決方案。


查看完整回答
反對 回復(fù) 2022-07-16
?
aluckdog

TA貢獻(xiàn)1847條經(jīng)驗 獲得超7個贊

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;


查看完整回答
反對 回復(fù) 2022-07-16
  • 2 回答
  • 0 關(guān)注
  • 139 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號