skip to Main Content

I developed a telegram bot whose job is to send the information/message to the group if the user requests it. This is how it works: Suppose If I post the "x player score" text in the group, it would bring the cricket score of that particular player and this mechanism is happening inside the indefinite while loop (long polling)

the problem is that it would keep bringing the score of that particular player unless I send request information for some other player too. So what I’m trying to achieve is that the bot should go over that message only once and fulfill the request.

Here is my code

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

} 

2

Answers


  1. You need to "confirm" a received message/update. Or else your bot will query the same updates over and over again.

    You can actually open https://api.telegram.org/botYOUR_TOKEN/getUpdates and see all messages sent to your bot. This list needs to be cleared.

    See Api Docs.

    An update is considered confirmed as soon as getUpdates is called with
    an offset higher than its update_id.

    For a quick fix to your code try something like

    $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);    
    
    } 
    
    Login or Signup to reply.
  2. I suggest creating a function that will send API Requests.

    Now, This code will receive all updates, And readable code.

    $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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search