J
J
jadam2022-01-18 19:21:03
PHP
jadam, 2022-01-18 19:21:03

Editing Telegram Bot messages?

The bottom line is this: The
bot sends a message to the user.
We save the ID of the last message.
Then we enter a similar loop:

for ($i=0; $i<60; $i++) {
  sleep(1);
  //edit last message
}


The first 10-15 seconds the message is edited with a bang, but then (although there were cases that almost immediately, i.e. randomly) the bot starts sending a new message (with edited data), and not editing the old one.
Also at this moment, the cart api returns the ID of the edited message, but the value is already different from what it was originally.

Just as I noticed, sometimes, for example, at 15 seconds, the counter is reset and the bot sends a new message, and does not edit the old one.

Below is a piece of code in its entirety:

$callback_balancecheck = function () use ($Bot, $BotController, $mySql, $Numbers) {

    $editmessage = $BotController->removeLastMarkup($Bot->editMessage(\TelegramBot\Bot::MESSAGE_EDIT_REPLY_MARKUP), $Bot->Request->id, $BotController->getLastMessageID($Bot->Request->id));
    $Bot->send($editmessage);



    $message = $Bot->createMessage();
    $message->chat_id($Bot->Request->id);
    $message->text("*Начало проверки баланса\.*\n\r\n\rОжидайте ответного сообщения через некоторое время\.\n\r\n\r");
    $message->parse_mode(\TelegramBot\Message::PARSEMODE_MarkdownV2);

    $result = $Bot->send($message);

    $BotController->saveLastMessageID($Bot->Request->id, $result->message_id);

    $last_message_id = $result->message_id;
    //file_put_contents("ids.txt", $last_message_id."\n", FILE_APPEND);



    $user_id = $BotController->getRegisteredUserID($Bot->Request->id);

    $userName = $mySql->query("SELECT login FROM users WHERE id = '".$user_id."'")->fetchAll();
    $userName = $userName[0]['login'];

    $activeNumbers = $Numbers->getActiveNumbers($user_id);
    $activeNumbers = $Numbers->getOperatorNumbers($activeNumbers);

    $customNumbers = $BotController->getCustomNumber($Bot->Request->id);

    $i = 1;

    foreach ($activeNumbers as $key => $value) {

        $number = array();
        $number[0] = $value[0];
        $number[1] = $Numbers->getNumberPassword($value[0]);
        $number[2] = $value[1];

        $balance = $Numbers->getBalance($number); 

        $editmessage = $Bot->editMessage(\TelegramBot\Bot::MESSAGE_EDIT_FULL);
        $editmessage->chat_id($Bot->Request->id);
        $editmessage->message_id($last_message_id);

        $editmessage->text("*Начало проверки баланса\.*\n\r\n\rОжидайте ответного сообщения через некоторое время\.\n\r\n\rПроверено *".$i."* из *".(count($activeNumbers) + count($customNumbers))."* номеров\.");
        $editmessage->parse_mode(\TelegramBot\Message::PARSEMODE_MarkdownV2);

        $result = $Bot->send($editmessage);
        $last_message_id = $result->message_id;

        //file_put_contents("ids.txt", $last_message_id."\n", FILE_APPEND);


        if ($balance != \Numbers\Numbers::BALANCE_ERROR) { 

            $activeNumbers[$key][2] = $balance;

            $lastBalance = $Numbers->getLastBalance($number);

            $activeNumbers[$key][3] = $lastBalance['balance'];

            if (floatval($lastBalance['balance']) != floatval($balance)) {
                $Numbers->saveBalance($number[0], $balance);
            }

        }
        else {
            $activeNumbers[$key][2] = \Numbers\Numbers::BALANCE_ERROR;
        }

        $i++;      

    }

    uasort($activeNumbers, 'cmp');


    foreach ($customNumbers as $key => $value) {

        $number = array();
        $number[0] = $value[0];
        $number[1] = $value[1];
        $number[2] = $Numbers->getOperatorNumbers([$value[0]])[0][1];

        $balance = $Numbers->getBalance($number);


        $editmessage = $Bot->editMessage(\TelegramBot\Bot::MESSAGE_EDIT_FULL);
        $editmessage->chat_id($Bot->Request->id);
        $editmessage->message_id($last_message_id);

        $editmessage->text("*Начало проверки баланса\.*\n\r\n\rОжидайте ответного сообщения через некоторое время\.\n\r\n\rПроверено *".$i."* из *".(count($activeNumbers) + count($customNumbers))."* номеров\.");
        $editmessage->parse_mode(\TelegramBot\Message::PARSEMODE_MarkdownV2);

        $result = $Bot->send($editmessage);
        $last_message_id = $result->message_id;

        //file_put_contents("ids.txt", $last_message_id."\n", FILE_APPEND);

        if ($balance != \Numbers\Numbers::BALANCE_ERROR) { 

            $customNumbers[$key][2] = $balance;

            $lastBalance = $Numbers->getLastBalance($number);

            $customNumbers[$key][3] = $lastBalance['balance'];

            if (floatval($lastBalance['balance']) != floatval($balance)) {
                $Numbers->saveBalance($number[0], $balance);
            }

        }
        else {
            $customNumbers[$key][2] = \Numbers\Numbers::BALANCE_ERROR;
        }

        $i++;
    }

    uasort($customNumbers, 'cmp');


    $messageText = "*Список активных номеров \(". (count($activeNumbers) + count($customNumbers)) ."\) для пользователя _".$userName."_*\n\r\n\r";
    $messageText .= $Bot->escape(date("d.m.y H:i:s"))."\n\r\n\r";

    foreach ($activeNumbers as $key => $value) {        

        
        if ($value[2] != \Numbers\Numbers::BALANCE_ERROR) {
            $delta = floatval($value[2]) - floatval($value[3]);

            if ($delta < 0) {
                $delta_msg = "  *".$Bot->escape(sprintf("%01.2f", $delta))."*р\.";
            }
            else if ($delta > 0) {
                $delta_msg = "  *".$Bot->escape("+".sprintf("%01.2f", $delta))."*р\.";
            }
            else {
                $delta_msg = "";
            }


            $messageText .= $BotController->formatMinBalance($value[2], $Bot->escape($Numbers->format($value[0])))."  ".$BotController->formatMinBalance($value[2], $Bot->escape(sprintf("%01.2f", $value[2]))."р\.").$delta_msg."\n\r";
        }
        else {
            $messageText .= $Bot->escape($Numbers->format($value[0]))."   Ошибка\n\r";
        }
        

    }
    

    $messageText .= "\n\r*Дополнительные номера*\n\r\n\r";

    foreach ($customNumbers as $key => $value) {        

        
        if ($value[2] != \Numbers\Numbers::BALANCE_ERROR) {
            $delta = floatval($value[2]) - floatval($value[3]);

            if ($delta < 0) {
                $delta_msg = "  *".$Bot->escape(sprintf("%01.2f", $delta))."*р\.";
            }
            else if ($delta > 0) {
                $delta_msg = "  *".$Bot->escape("+".sprintf("%01.2f", $delta))."*р\.";
            }
            else {
                $delta_msg = "";
            }


            $messageText .= $BotController->formatMinBalance($value[2], $Bot->escape($Numbers->format($value[0])))."  ".$BotController->formatMinBalance($value[2], $Bot->escape(sprintf("%01.2f", $value[2]))."р\.").$delta_msg."\n\r";
        }
        else {
            $messageText .= $Bot->escape($Numbers->format($value[0]))."   Ошибка\n\r";
        }
        

    }




    $message = $Bot->createMessage();
    $message->chat_id($Bot->Request->id);
    $message->text($messageText);
    $message->parse_mode(\TelegramBot\Message::PARSEMODE_MarkdownV2);



    $keyboard = $BotController->createKeyboard($message->createKeyboard(), [BotController::KEYBOARD_ACCOUNTINFO, BotController::KEYBOARD_CHECKBALANCE, BotController::KEYBOARD_NUMBERINFO, BotController::KEYBOARD_SETTINGS]);
    $message->reply_markup($keyboard);


    $result = $Bot->send($message);
    $BotController->saveLastMessageID($Bot->Request->id, $result->message_id);

};


What's wrong?
There is a suspicion that the cart is waiting too long for a response from the server and starts sending a second request, and there is also an assumption that the same message cannot be edited many times.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question