Telegram Notifier
Edit this pageTelegram Notifier
The Telegram Notifier package allows to use Telegram via the Symfony Notifier component. Read the main Notifier docs to learn about installing and configuring that component.
Adding Interactions to a Message
With a Telegram message, you can use the TelegramOptions class to add message options:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\Button\InlineKeyboardButton;
use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\InlineKeyboardMarkup;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
use Symfony\Component\Notifier\Message\ChatMessage;
$chatMessage = new ChatMessage('');
// Create Telegram options
$telegramOptions = (new TelegramOptions())
->chatId('@symfonynotifierdev')
->parseMode('MarkdownV2')
->disableWebPagePreview(true)
->disableNotification(true)
->replyMarkup((new InlineKeyboardMarkup())
->inlineKeyboard([
(new InlineKeyboardButton('Visit symfony.com'))
->url('https://symfony.com/'),
])
);
// Add the custom options to the chat message and send the message
$chatMessage->options($telegramOptions);
$chatter->send($chatMessage);
Updating Messages
6.2
The TelegramOptions::edit()
method was introduced in Symfony 6.2.
When working with interactive callback buttons, you can use the TelegramOptions to reference a previous message to edit:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\Button\InlineKeyboardButton;
use Symfony\Component\Notifier\Bridge\Telegram\Reply\Markup\InlineKeyboardMarkup;
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
use Symfony\Component\Notifier\Message\ChatMessage;
$chatMessage = new ChatMessage('Are you really sure?');
$telegramOptions = (new TelegramOptions())
->chatId($chatId)
->edit($messageId) // extracted from callback payload or SentMessage
->replyMarkup((new InlineKeyboardMarkup())
->inlineKeyboard([
(new InlineKeyboardButton('Absolutely'))->callbackData('yes'),
])
);
Answering Callback Queries
6.3
The TelegramOptions::answerCallbackQuery()
method was introduced in Symfony 6.3.
When sending message with inline keyboard buttons with callback data, you can use TelegramOptions to answer callback queries:
1 2 3 4 5 6 7 8 9 10 11
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions;
use Symfony\Component\Notifier\Message\ChatMessage;
$chatMessage = new ChatMessage('Thank you!');
$telegramOptions = (new TelegramOptions())
->chatId($chatId)
->answerCallbackQuery(
callbackQueryId: '12345', // extracted from callback
showAlert: true,
cacheTime: 1,
);
This work, including the code samples, is licensed under a
Creative Commons BY-SA 3.0
license.
TOC
Version
Version: