symfony/telegram-notifier

Symfony Telegram Notifier 桥接器

安装次数: 501,445

依赖者: 4

建议者: 0

安全性: 0

星星: 65

关注者: 6

分支: 6

类型:symfony-notifier-bridge


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,
    );

资源