tourze / telegram-bot-bundle
TG机器人
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: ^8.1
- doctrine/data-fixtures: ^2.0
- doctrine/dbal: ^4.0
- doctrine/doctrine-bundle: ^2.13
- doctrine/doctrine-fixtures-bundle: ^4.0
- doctrine/orm: ^3.0
- doctrine/persistence: ^3.1 || ^4
- knplabs/knp-menu: ^3.7
- psr/log: ^3|^2|^1
- symfony/config: ^6.4
- symfony/console: ^6.4
- symfony/dependency-injection: ^6.4
- symfony/event-dispatcher: ^6.4
- symfony/event-dispatcher-contracts: ^2.5 | ^3
- symfony/framework-bundle: ^6.4
- symfony/http-client-contracts: ^2.5 | ^3.0
- symfony/http-foundation: ^6.4
- symfony/http-kernel: ^6.4
- symfony/routing: ^6.4
- symfony/yaml: ^6.4 || ^7.1
- tourze/arrayable: 0.0.*
- tourze/doctrine-async-bundle: ^0.0.6
- tourze/doctrine-snowflake-bundle: 0.0.*
- tourze/doctrine-track-bundle: 0.0.*
- tourze/easy-admin-attribute: 0.0.*
- tourze/easy-admin-extra-bundle: 0.0.*
- tourze/symfony-aop-doctrine-bundle: 0.0.*
- tourze/symfony-routing-auto-loader-bundle: 0.0.*
Requires (Dev)
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^10.0
This package is auto-updated.
Last update: 2025-05-05 08:47:41 UTC
README
这个 Bundle 提供了 Telegram 机器人的管理功能,包括:
- 机器人的添加、编辑和管理
- Webhook 接收消息
- 自动回复规则管理
- 消息处理与发送
- 命令处理系统
使用
- 访问后台管理页面
/admin/telegram-bot/bots
添加机器人 - 使用命令设置 Webhook URL:
# 设置单个机器人的 webhook bin/console telegram:set-webhook --bot-id=123 # 使用自定义基础 URL bin/console telegram:set-webhook --bot-id=123 --base-url=https://custom-domain.com
自动回复规则
- 支持精确匹配和模糊匹配
- 可以设置优先级,优先级高的规则会优先匹配
- 支持 HTML 格式的回复内容
命令处理
Bundle 提供了一个命令处理系统,可以轻松添加新的命令处理器:
- 创建一个命令处理器类:
use TelegramBotBundle\Event\TelegramCommandEvent;use TelegramBotBundle\Handler\AbstractCommandHandler; class YourCommandHandler extends AbstractCommandHandler { protected function getCommand(): string { return 'yourcommand'; // 对应 /yourcommand } protected function handle(TelegramCommandEvent $event): void { $args = $event->getArgs(); // 获取命令参数 $this->reply($event, "你的回复消息"); } }
- 命令处理器会自动注册并处理对应的命令
事件系统
Bundle 使用事件系统来处理 Webhook 消息,你可以创建自己的 EventSubscriber 来处理消息:
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use TelegramBotBundle\Event\TelegramUpdateEvent; class YourSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents(): array { return [ TelegramUpdateEvent::class => ['onTelegramUpdate', -10], // 优先级低于自动回复 ]; } public function onTelegramUpdate(TelegramUpdateEvent $event): void { $update = $event->getUpdate(); $bot = $event->getBot(); // 处理消息... } }
API
发送消息
use TelegramBotBundle\Service\TelegramBotService; class YourService { public function __construct( private readonly TelegramBotService $botService, ) { } public function sendMessage(string $botId, string $chatId, string $message): void { $bot = $this->botRepository->find($botId); if ($bot) { $this->botService->sendMessage($bot, $chatId, $message); } } }