symfony / telegram-notifier
Symfony Telegram Notifier 桥接器
v7.1.5
2024-09-20 13:35 UTC
Requires
- php: >=8.2
- symfony/http-client: ^6.4|^7.0
- symfony/mime: ^6.4|^7.0
- symfony/notifier: ^6.4|^7.0
- 7.2.x-dev
- 7.1.x-dev
- 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.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.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-09-21 06:12:40 UTC
README
为 Symfony Notifier 提供 Telegram 集成。
DSN 示例
TELEGRAM_DSN=telegram://TOKEN@default?channel=CHAT_ID
其中
TOKEN
是您的 Telegram 令牌CHAT_ID
是您的 Telegram 聊天 ID
向消息添加交互
使用 Telegram 消息,您可以使用 TelegramOptions
类来添加 消息选项。
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.ac.cn/'), ]) ); // Add the custom options to the chat message and send the message $chatMessage->options($telegramOptions); $chatter->send($chatMessage);
向消息添加文件
使用 Telegram 消息,您可以使用 TelegramOptions
类来添加 消息选项。
⚠️ 警告 在一条消息中,您只能发送一个文件
Telegram 支持 3 种 传递文件的方式
- 您可以通过传递公共 HTTP URL 到选项来发送文件
- 照片
$telegramOptions = (new TelegramOptions()) ->photo('https://localhost/photo.mp4');
- 视频
$telegramOptions = (new TelegramOptions()) ->video('https://localhost/video.mp4');
- 动画
$telegramOptions = (new TelegramOptions()) ->animation('https://localhost/animation.gif');
- 音频
$telegramOptions = (new TelegramOptions()) ->audio('https://localhost/audio.ogg');
- 文档
$telegramOptions = (new TelegramOptions()) ->document('https://localhost/document.odt');
- 贴纸
$telegramOptions = (new TelegramOptions()) ->sticker('https://localhost/sticker.webp', '🤖');
- 照片
- 您可以通过传递本地路径到选项来发送文件,在这种情况下,文件将通过 multipart/form-data 发送
- 照片
$telegramOptions = (new TelegramOptions()) ->uploadPhoto('files/photo.png');
- 视频
$telegramOptions = (new TelegramOptions()) ->uploadVideo('files/video.mp4');
- 动画
$telegramOptions = (new TelegramOptions()) ->uploadAnimation('files/animation.gif');
- 音频
$telegramOptions = (new TelegramOptions()) ->uploadAudio('files/audio.ogg');
- 文档
$telegramOptions = (new TelegramOptions()) ->uploadDocument('files/document.odt');
- 贴纸
$telegramOptions = (new TelegramOptions()) ->uploadSticker('files/sticker.webp', '🤖');
- 照片
- 您可以通过传递 file_id 到选项来发送文件
- 照片
$telegramOptions = (new TelegramOptions()) ->photo('ABCDEF');
- 视频
$telegramOptions = (new TelegramOptions()) ->video('ABCDEF');
- 动画
$telegramOptions = (new TelegramOptions()) ->animation('ABCDEF');
- 音频
$telegramOptions = (new TelegramOptions()) ->audio('ABCDEF');
- 文档
$telegramOptions = (new TelegramOptions()) ->document('ABCDEF');
- 贴纸 - 不能使用 file_id 发送
- 照片
完整示例
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.ac.cn/favicons/android-chrome-192x192.png'); // Add the custom options to the chat message and send the message $chatMessage->options($telegramOptions); $chatter->send($chatMessage);
向消息添加位置
使用 Telegram 消息,您可以使用 TelegramOptions
类来添加 消息选项。
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);
向消息添加地点
使用 Telegram 消息,您可以使用 TelegramOptions
类来添加 消息选项。
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);
向消息添加联系人
使用 Telegram 消息,您可以使用 TelegramOptions
类来添加 消息选项。
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);
更新消息
TelegramOptions::edit()
方法是在 Symfony 6.2 中引入的。
当处理交互式回调按钮时,您可以使用 TelegramOptions
来引用先前消息以进行编辑。
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'), ]) );
响应回调查询
TelegramOptions::answerCallbackQuery()
方法是在 Symfony 6.3 中引入的。
当发送带有回调数据的内联键盘按钮的消息时,您可以使用 TelegramOptions
来 响应回调查询。
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, );
资源
- 贡献
- 报告问题 和 发送拉取请求 在 主要 Symfony 仓库