symfony / telegram-notifier
Symfony Telegram Notifier Bridge
Fund package maintenance!
fabpot
Tidelift
symfony.com/sponsor
Installs: 529 299
Dependents: 4
Suggesters: 0
Security: 0
Stars: 67
Watchers: 6
Forks: 6
Type:symfony-notifier-bridge
Requires
- php: >=8.2
- symfony/http-client: ^6.4|^7.0
- symfony/mime: ^6.4|^7.0
- symfony/notifier: ^7.2
- 7.2.x-dev
- v7.2.0-BETA1
- 7.1.x-dev
- v7.1.6
- v7.1.5
- v7.1.1
- v7.1.0
- v7.1.0-RC1
- v7.1.0-BETA1
- 7.0.x-dev
- v7.0.8
- v7.0.7
- v7.0.3
- v7.0.0
- v7.0.0-RC1
- v7.0.0-BETA1
- 6.4.x-dev
- v6.4.13
- v6.4.12
- v6.4.8
- v6.4.7
- v6.4.3
- v6.4.0
- v6.4.0-RC1
- v6.4.0-BETA1
- 6.3.x-dev
- v6.3.12
- v6.3.5
- v6.3.0
- v6.3.0-RC1
- v6.3.0-BETA1
- 6.2.x-dev
- v6.2.8
- v6.2.7
- v6.2.5
- v6.2.2
- v6.2.0
- v6.2.0-RC1
- v6.2.0-BETA1
- 6.1.x-dev
- v6.1.11
- v6.1.9
- v6.1.0
- v6.1.0-RC1
- v6.1.0-BETA1
- 6.0.x-dev
- v6.0.19
- v6.0.17
- v6.0.3
- v6.0.0
- v6.0.0-RC1
- v6.0.0-BETA2
- v6.0.0-BETA1
- 5.4.x-dev
- v5.4.45
- v5.4.40
- v5.4.39
- v5.4.35
- v5.4.31
- v5.4.22
- v5.4.21
- v5.4.19
- v5.4.17
- v5.4.3
- v5.4.0
- v5.4.0-RC1
- v5.4.0-BETA2
- v5.4.0-BETA1
- 5.3.x-dev
- v5.3.14
- v5.3.7
- v5.3.4
- v5.3.2
- v5.3.0
- v5.3.0-RC1
- v5.3.0-BETA4
- v5.3.0-BETA1
- 5.2.x-dev
- v5.2.12
- v5.2.10
- v5.2.7
- v5.2.4
- v5.2.3
- v5.2.2
- v5.2.1
- v5.2.0
- v5.2.0-RC2
- v5.2.0-RC1
- v5.2.0-BETA3
- v5.2.0-BETA2
- v5.2.0-BETA1
- 5.1.x-dev
- v5.1.11
- v5.1.10
- v5.1.9
- v5.1.8
- v5.1.7
- v5.1.6
- v5.1.5
- v5.1.4
- v5.1.3
- v5.1.2
- v5.1.1
- v5.1.0
- v5.1.0-RC2
- v5.1.0-RC1
- v5.1.0-BETA1
- 5.0.x-dev
- v5.0.11
- v5.0.10
- v5.0.9
- v5.0.8
- v5.0.7
- v5.0.6
- v5.0.5
- v5.0.4
- v5.0.3
- v5.0.2
- v5.0.1
- v5.0.0
- v5.0.0-RC1
- v5.0.0-BETA2
- v5.0.0-BETA1
This package is auto-updated.
Last update: 2024-10-27 15:24:54 UTC
README
Provides Telegram integration for Symfony Notifier.
DSN example
TELEGRAM_DSN=telegram://TOKEN@default?channel=CHAT_ID
where:
TOKEN
is your Telegram tokenCHAT_ID
is your Telegram chat id
Adding Interactions to a Message
With a Telegram message, you can use the TelegramOptions
class to add
message options.
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);
Adding files to a Message
With a Telegram message, you can use the TelegramOptions
class to add
message options.
⚠️ WARNING In one message you can send only one file
Telegram supports 3 ways for passing files:
- You can send files by passing public http url to option:
- Photo
$telegramOptions = (new TelegramOptions()) ->photo('https://localhost/photo.mp4');
- Video
$telegramOptions = (new TelegramOptions()) ->video('https://localhost/video.mp4');
- Animation
$telegramOptions = (new TelegramOptions()) ->animation('https://localhost/animation.gif');
- Audio
$telegramOptions = (new TelegramOptions()) ->audio('https://localhost/audio.ogg');
- Document
$telegramOptions = (new TelegramOptions()) ->document('https://localhost/document.odt');
- Sticker
$telegramOptions = (new TelegramOptions()) ->sticker('https://localhost/sticker.webp', '🤖');
- Photo
- You can send files by passing local path to option, in this case file will be sent via multipart/form-data:
- Photo
$telegramOptions = (new TelegramOptions()) ->uploadPhoto('files/photo.png');
- Video
$telegramOptions = (new TelegramOptions()) ->uploadVideo('files/video.mp4');
- Animation
$telegramOptions = (new TelegramOptions()) ->uploadAnimation('files/animation.gif');
- Audio
$telegramOptions = (new TelegramOptions()) ->uploadAudio('files/audio.ogg');
- Document
$telegramOptions = (new TelegramOptions()) ->uploadDocument('files/document.odt');
- Sticker
$telegramOptions = (new TelegramOptions()) ->uploadSticker('files/sticker.webp', '🤖');
- Photo
- You can send files by passing file_id to option:
- Photo
$telegramOptions = (new TelegramOptions()) ->photo('ABCDEF');
- Video
$telegramOptions = (new TelegramOptions()) ->video('ABCDEF');
- Animation
$telegramOptions = (new TelegramOptions()) ->animation('ABCDEF');
- Audio
$telegramOptions = (new TelegramOptions()) ->audio('ABCDEF');
- Document
$telegramOptions = (new TelegramOptions()) ->document('ABCDEF');
- Sticker - Can't be sent using file_id
- Photo
Full example:
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions; use Symfony\Component\Notifier\Message\ChatMessage; $chatMessage = new ChatMessage('Photo Caption'); // Create Telegram options $telegramOptions = (new TelegramOptions()) ->chatId('@symfonynotifierdev') ->parseMode('MarkdownV2') ->disableWebPagePreview(true) ->hasSpoiler(true) ->protectContent(true) ->photo('https://symfony.com/favicons/android-chrome-192x192.png'); // Add the custom options to the chat message and send the message $chatMessage->options($telegramOptions); $chatter->send($chatMessage);
Adding Location to a Message
With a Telegram message, you can use the TelegramOptions
class to add
message options.
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') ->location(48.8566, 2.3522); // Add the custom options to the chat message and send the message $chatMessage->options($telegramOptions); $chatter->send($chatMessage);
Adding Venue to a Message
With a Telegram message, you can use the TelegramOptions
class to add
message options.
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') ->venue(48.8566, 2.3522, 'Center of Paris', 'France, Paris'); // Add the custom options to the chat message and send the message $chatMessage->options($telegramOptions); $chatter->send($chatMessage);
Adding Contact to a Message
With a Telegram message, you can use the TelegramOptions
class to add
message options.
use Symfony\Component\Notifier\Bridge\Telegram\TelegramOptions; use Symfony\Component\Notifier\Message\ChatMessage; $chatMessage = new ChatMessage(''); $vCard = 'BEGIN:VCARD VERSION:3.0 N:Doe;John;;; FN:John Doe EMAIL;type=INTERNET;type=WORK;type=pref:johnDoe@example.org TEL;type=WORK;type=pref:+330186657200 END:VCARD'; // Create Telegram options $telegramOptions = (new TelegramOptions()) ->chatId('@symfonynotifierdev') ->parseMode('MarkdownV2') ->contact('+330186657200', 'John', 'Doe', $vCard); // Add the custom options to the chat message and send the message $chatMessage->options($telegramOptions); $chatter->send($chatMessage);
Updating Messages
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.
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
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.
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, );