aymericcucherousset/telegram-bot
A simple PHP Telegram Bot library for sending messages, handling updates, and interacting with Telegram’s Bot API. Includes helpers for requests, webhooks/long polling, keyboards, and common methods to build bots quickly and cleanly.
Callback queries are triggered when a user interacts with an inline keyboard button. The SDK uses attribute-based handler classes for clean, extensible callback query processing.
Implement a handler class with the AsTelegramCallbackQuery attribute:
use Aymericcucherousset\TelegramBot\Api\HttpTelegramClient;
use Aymericcucherousset\TelegramBot\Handler\HandlerInterface;
use Aymericcucherousset\TelegramBot\Attribute\AsTelegramCallbackQuery;
use Aymericcucherousset\TelegramBot\Method\Message\EditMessageText;
use Aymericcucherousset\TelegramBot\Update\Update;
use Aymericcucherousset\TelegramBot\Value\ParseMode;
#[AsTelegramCallbackQuery(name: 'products', description: 'Replies with the list of products')]
final class ProductsCallbackQuery implements HandlerInterface
{
public function handle(Update $update): void
{
$callbackQuery = $update->callbackQuery;
if ($callbackQuery === null) {
return;
}
$editMessage = new EditMessageText(
chatId: $callbackQuery->chatId,
messageId: $update->message->id,
text: 'List of products : ...',
mode: ParseMode::Markdown,
);
$update->bot->getClient()->send($editMessage);
}
}
To create an Update object from incoming JSON, you must now pass a Bot instance as the second argument:
use Aymericcucherousset\TelegramBot\Update\UpdateFactory;
use Aymericcucherousset\TelegramBot\Bot\Bot;
$bot = new Bot($client); // $client is your TelegramClientInterface implementation
$update = UpdateFactory::fromJson($json, $bot);
InlineKeyboardButton::callback() defines a callback_data payload.See also: API Reference, Testing
How can I help you explore Laravel packages today?