【问题标题】:Telegram Bot Getupdates API - PHPTelegram Bot Getupdates API - PHP
【发布时间】:2021-11-26 20:50:52
【问题描述】:

我开发了一个电报机器人,它的工作是在用户请求时向群组发送信息/消息。它是这样工作的:假设如果我在组中发布“x 球员得分”文本,它将带来该特定球员的板球得分,并且这种机制发生在无限期 while 循环(长轮询)中

问题在于,除非我也发送其他玩家的请求信息,否则它会继续带来该特定玩家的分数。所以我想要实现的是机器人应该只检查一次该消息并满足请求。

这是我的代码

$token = 'xyz';
$group_name = 'xyz';


while(true){

$get_msg = file_get_contents("https://api.telegram.org/bot%7B$token%7D/getUpdates");
$get_msg_decode = json_decode($get_msg);
$msg_array = $get_msg_decode['result'];
$last_msg_array = end($msg_array); 
$last_msg = $last_msg_array['message']['text']; // this would bring the latest message from telegram group
x_player_score = 50;

// Some actions or logic
        
if($last_msg == x_player_score){
   $bot = "https://api.telegram.org/bot{$token}/sendMessage?chat_id={$group_name}&text={x_player_score}";
  $hit = file_get_contents($bot);      
} 

sleep(5);    

} 

【问题讨论】:

    标签: php telegram telegram-bot php-telegram-bot


    【解决方案1】:

    我建议创建一个发送 API 请求的函数。

    现在,此代码将接收所有更新和可读代码。

    $BOT_TOKEN = 'xyz';
    $GROUP_NAME = 'xyz';
    $SLEEP_TIME = 1;
    
    $curl = curl_init();
    function bot(string $method, array $params = [])
    {
        curl_setopt($curl, CURLOPT_URL, "https://api.telegram.org/bot{$BOT_TOKEN}/{$method}");
        curl_setopt($curl, CURLOPT_POSTFIELDS, $params[0] ?? []);
    
        $result = curl_exec($curl);
        if ($result->ok)
        {
            return $result->result;
        }
        return $result;
    }
    
    $LastUpdateID = 0;
    $x_player_score = 50;
    while (true)
    {
        $updates = bot('getUpdates', [
            'offset' => $LastUpdateID
        ]);
        
        # Loop on all of the updates
        foreach ($updates as $update)
        {
            if ($update->message->text == x_player_score){
                bot('sendMessage', [
                    'chat_id' => $GROUP_NAME,
                    'text' => $x_player_score
                ]);
            }
            $LastUpdateID = $update->update_id;
        }
    
        sleep($SLEEP_TIME);
    }
    
    curl_close($curl);
    

    【讨论】:

      【解决方案2】:

      您需要“确认”收到的消息/更新。否则您的机器人将一遍又一遍地查询相同的更新。

      您实际上可以打开 https://api.telegram.org/botYOUR_TOKEN/getUpdates 并查看发送到您的机器人的所有消息。需要清除此列表。

      See Api Docs.

      只要调用 getUpdates 就认为更新已确认 高于其 update_id 的偏移量。

      要快速修复您的代码,请尝试类似

      $token = 'xyz';
      $group_name = 'xyz';
      $offset = 0;
      
      while(true){
      
      $get_msg = file_get_contents("https://api.telegram.org/bot%7B$token%7D/getUpdates?offset=$offset");
      $get_msg_decode = json_decode($get_msg);
      $msg_array = $get_msg_decode['result'];
      $last_msg_array = end($msg_array);
      $offset= $last_msg_array['update_id']+1; 
      $last_msg = $last_msg_array['message']['text']; // this would bring the latest message from telegram group
      x_player_score = 50;
      
      // Some actions or logic
              
      if($last_msg == x_player_score){
         $bot = "https://api.telegram.org/bot{$token}/sendMessage?chat_id={$group_name}&text={x_player_score}";
        $hit = file_get_contents($bot);      
      } 
      
      sleep(5);    
      
      } 
      

      【讨论】:

      • 工作就像一个魅力!这正是我想要的!太感谢了!你真棒
      猜你喜欢
      • 2017-12-17
      • 2015-12-08
      • 1970-01-01
      • 2015-10-22
      • 2018-04-07
      • 1970-01-01
      • 2017-02-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多