M
M
maximw2021-07-07 18:22:42
API
maximw, 2021-07-07 18:22:42

Why does the Telegram bot API method answerCallbackQuery not show an alert?

I'm trying to show an alert like this in telegram. 0lSAk.png

My test code is something like this:

$url = "https://api.telegram.org/botTOKEN/answerCallbackQuery";
$response = file_get_contents($url."?callback_query_id=". $callback_query_id ."&text=SUCCESS&show_alert=true");

The response is $response { "ok": true, "result": true }, so I think the $callback_query_id parameter is correct.

But the alert does not appear! This is the problem.

I tried other bots (not mine) and other devices, and they show alerts fine. Therefore, I am almost sure that the problems are not in my device or telegram application.

In addition, I tried to make a request using CURL or using php libraries for telegram bots. In any of the options it was not possible to cause an alert.
"Smoking" official documentation also did not clarify anything.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
maximw, 2021-07-10
@maximw

The problem was in the library that I used https://github.com/php-telegram-bot/core
If you receive updates from telegram using the handleGetUpdates() method of this library, then the library itself sends empty responses to the CallbackQuery.
It turns out that I tried to answer the same CallbackQuery again, the answer from Telegram comes - OK, but in reality Telegram processed only the first answer, which was automatically sent by the library.

A
Anastasia, 2021-07-07
@nastya97core

try this:

$tg = json_decode(file_get_contents('php://input'), true);
function tlgrm($method, $data, $client) {
  $data["parse_mode"] = "html";
  $ch = curl_init("https://api.telegram.org/---токен---/$method");
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  curl_close($ch);
}

$client = $tg['callback_query']['from']['id'];
$cb_id = $tg['callback_query']['id'];
tlgrm('answerCallbackQuery', ['callback_query_id'=> $cb_id, 'text' => 'СООБЩЕНИЕ', 'show_alert' => true],$client);

create a php file, put this code in there and install a webhook on it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question