symfony/telegram-notifier
Composer 安装命令:
composer require symfony/telegram-notifier
包简介
Symfony Telegram Notifier Bridge
README 文档
README
Provides Telegram integration for Symfony Notifier.
DSN example
TELEGRAM_DSN=telegram://TOKEN@default?channel=CHAT_ID&sslmode=SSLMODE
where:
TOKENis your Telegram tokenCHAT_IDis your Telegram chat idSSLMODEhttps is used by default. It can be changed by setting value todisable, http will be used
Interacting with local API server instead of official Telegram API
If such a case is needed, you can replace the default keyword in the DSN
with the desired domain/IP address of your local API server. You may also want to
disable the bridge's default behavior of using https protocol as local API servers
can only accept http traffic.
Example:
TELEGRAM_DSN=telegram://TOKEN@localhost:5001?channel=CHAT_ID&sslmode=disable
Caution: Disabling the use of the https protocol can pose a security risk.
You should only do this if your local API server is hosted somehow internally
and the traffic will remain within a secure environment.
Otherwise, you may want to implement a TLS-termination proxy in front of
your server for handling the encryption and decryption of the traffic,
So you can continue using it normally over https protocol.
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, );
Sponsor
This package is looking for a backer.
Help Symfony by sponsoring its development!
Resources
symfony/telegram-notifier 适用场景与选型建议
symfony/telegram-notifier 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 997.24k 次下载、GitHub Stars 达 74, 最近一次更新时间为 2019 年 10 月 05 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「notifier」 「telegram」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 symfony/telegram-notifier 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 symfony/telegram-notifier 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 symfony/telegram-notifier 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Symfony JustCall Notifier Bridge
Laravel 5.6 Monolog custom telegram channel
PHP Wrapper for Telegram Bot API
Extensible library for building notifications and sending them via different delivery channels.
NO LIBRARIES socket per page bridge for your Laravel application.
Symfony SMS Line Notifier Bridge
统计信息
- 总下载量: 997.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 74
- 点击次数: 19
- 依赖项目数: 7
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-10-05