php-telegram-bot / core

PHP Telegram Bot based on the official Telegram Bot API
MIT License
3.9k stars 955 forks source link

answerCallbackQuery not working #1204

Closed temirovgroup closed 3 years ago

temirovgroup commented 3 years ago

PHP 7.4 (Shared hosting beget.com) Other commands are working successfully.

answerCallbackQuery not working. Everything is done correctly, returns a successful result. But there is no window in the chat (alert)

My code:

Request::answerCallbackQuery([ 'callback_query_id' => $callback_query_id, 'text' => 'OK', 'show_alert' => true, ]);

Please, help me :)

noplanman commented 3 years ago

@temirovgroup Have you tried on different devices and Telegram apps? Various Telegram apps display the alerts differently.

Also, be sure to return the answer using return Request::answerCallbackQuery...!

temirovgroup commented 3 years ago

I tried it on different devices. Returns this:

{ "ok": true, "result": true, "raw_data": { "ok": true, "result": true }, "bot_username": "@chickenstore_bot" }

P.S. return Request::answerCallbackQuery...! - It's done. Not working

noplanman commented 3 years ago

Please try this instead, inside your CallbackqueryCommand::execute command, and let me know if that works. Maybe the callback query ID you're passing is invalid?

public function execute(): ServerResponse
{
    $callback_query = $this->getCallbackQuery();

    // your code...

    return $callback_query->answer([
        'text'       => 'OK',
        'show_alert' => true,
    ]);
}
temirovgroup commented 3 years ago

Excuse please. The problem was this: $telegram = new Telegram(Yii::$app->params['telegram']['api_key'], Yii::$app->params['telegram']['bot_username']); $telegram->handle(); ($telegram->handle();)

I don't know why I called him at all.. :))

temirovgroup commented 3 years ago

Thank you for your answers. And a special thank you for your work!

DavidLeeForWin commented 1 year ago

Excuse please. The problem was this: $telegram = new Telegram(Yii::$app->params['telegram']['api_key'], Yii::$app->params['telegram']['bot_username']); $telegram->handle(); ($telegram->handle();)

I don't know why I called him at all.. :))

This problem has plagued me 10H.. Thank you Soooooomuch

underwear commented 1 year ago

I encountered the same issue where I'd send an answerCallbackQuery and Telegram responded with 200 OK, but no notification appeared for the user.

I found this answer on StackOverflow mentioning:

The problem was with the library I was using and how I used it. Using the https://github.com/php-telegram-bot/core and the handleGetUpdates() method, the library automatically sends an empty response to answerCallbackQuery. I attempted to answer the CallbackQuery again after the library already did. Even though Telegram says OK, it only handles the first call.

So, I solved it by not using $telegram->handleGetUpdates().

Instead, I'm now using the \Longman\TelegramBot\Request::getUpdates($data) method.

To use it directly, you still need to set up and create a Telegram instance and then pass it to Request::initialize().

Like this:

use \Longman\TelegramBot\Request;
use \Longman\TelegramBot\Telegram;

$apiKey = "place_your_api_key_here";
$botUsername = "place_your_bot_username_here";

$telegram = new Telegram($apiKey, $botUsername);

Request::initialize($telegram);

$updates = Request::getUpdates([
    // here is a place for your long polling request params
]);

I hope this helps!

noplanman commented 1 year ago

@underwear How curious, I don't understand why it's not working for you, as the $telegram->handleGetUpdates() "should" be doing the same thing.

Clearly the few extras it does, seem to be breaking something, but I've not encountered that yet.

FYI, no need to do Request::initialize as the Telegram constructor does that already for you.

What version of PHP are you on, and which version of the bot are you using?