2

I have a Telegram Bot which worked fine for a few months. About 3-4 weeks ago the Telegram Bot sends sometimes (!) the same messages a few times - not just one time!

I have already tried many things to fix that:

  1. I save the update_id in the database. If the new update_id is higher than the saved one, the bot sends the message. If not, an error occurs. (Found out yesterday that every duplicate message has got a new (higher) update_id - so this method is useless)

  2. I tried to add exit(); to every PHP Function - didn't work.

  3. I checked the whole code, if there is a not closed loop. But everything is fine.

  4. I added a ?limit=1 to the function, but also no chance.

Slowly I am at the end of my ideas. Maybe someone has got a good answer for that.

I have always the $update and the chat ID:

define('api', 'https://api.telegram.org/bot'.token.'/');

$data = file_get_contents("php://input");
$update = json_decode($data, true);
$cbid = $update["callback_query"]["from"]["id"];

My function looks like this:

function callback($up){
    return $up["callback_query"];
}

function tg_send_message($id, $text) {

    $params=[
        'chat_id' => $id,
        'text' => $text,
        'parse_mode' => 'Markdown',
    ];

    $ch = curl_init('https://api.telegram.org/bot'.token.'/sendMessage?limit=1');
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3500);
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 3500);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($ch);
    curl_close($ch);

}

If I want to send a message if someone clicks on the inline button Top 10:

if(callback($update) and $cbdata == "top_10"){
    tg_send_message($cbid, "This is my message!");  
}

Everything worked fine and is working now as well - the bot just sends sometime a message multiple times.

The extra messages have always a new update_id! And if there is a variable value from another API (like Bitcoin price), it is also changing. It's like you clicked 22 times on the button (but you just clicked once)

I am very thankful for every helpful answer! Thank you very much.

Best regards.

EDIT: I have also tried the following:

function tg_btn_click_send_message($id, $text) {

    $response = $update["callback_query"];
    $botUrl = "https://api.telegram.org/bot" . BOT_TOKEN . "/answerCallbackQuery";
    $postFields = array('callback_query_id' =>  $callback_query_id, 'text' => $response);
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
    curl_setopt($ch, CURLOPT_URL, $botUrl); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    $output = curl_exec($ch);

    //send Text
    header("Content-Type: application/json");
    $parameters = array('chat_id' => $id, "text" => $text);
    $parameters["method"] = "sendMessage";
    echo json_encode($parameters);

}

Like suggested here: php telegram answercallbackquery sendmessage

But also not working.

Ali Elkhaiat
  • 57
  • 1
  • 9

1 Answers1

0

If someone has the same problem, here the solution:

function tg_answer_callback_query($cbq_id) {

    $params=[
        'callback_query_id' => $cbq_id
    ];

    $ch = curl_init('https://api.telegram.org/bot'.token.'/answerCallbackQuery');
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3500);
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 3500);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, ($params));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $result = curl_exec($ch);
    curl_close($ch);

}

Run this code at the end of if(callback($update)) (not after it!) and it works!

Ali Elkhaiat
  • 57
  • 1
  • 9
  • It's not working bro still it send message twice sometimes if like you have 1000 users try it and let me know if there is a fix – Miki Dec 05 '21 at 09:58
  • This worked 2 years ago. We had more than 1000 active users as well. We don't use Telegram anymore, so I cannot test, if it's still working, sorry! – Ali Elkhaiat Dec 06 '21 at 10:09