andrew-gos/telegram-bot-bundle
Composer 安装命令:
composer require andrew-gos/telegram-bot-bundle
包简介
Advanced strictly typed Telegram Bot Library
README 文档
README
An advanced, strictly typed, and feature-rich Symfony bundle for creating Telegram bots, built on top of the powerful andrew-gos/telegram-bot library. This bundle fully leverages the Symfony ecosystem to provide a seamless and highly configurable development experience.
✨ Key Features
- 🐘 Multi-Bot Support: Effortlessly configure and manage multiple bots within a single Symfony application.
- ⚙️ Flexible Configuration: Powerful and intuitive YAML configuration for bots, update handlers, plugins, and middlewares.
- 🕹️ Console Commands: A rich set of
bin/consolecommands to manage webhooks, listen for updates (long-polling), and inspect bot status. - 🧩 Extensible Architecture: Easily create your own update handlers, checkers, middlewares, and plugins as standard Symfony services.
- 🛡️ Strictly Typed: Benefit from a fully typed API that provides excellent autocompletion and reduces runtime errors.
- 🔗 Symfony Integration: Built from the ground up to work with Symfony's Dependency Injection, Event Dispatcher, and other core components.
🚀 Installation
Install the bundle into your Symfony project using Composer.
composer require andrew-gos/telegram-bot-bundle
The bundle will be automatically enabled by Symfony Flex.
⚙️ Configuration
This is where the magic happens. The bundle's configuration is powerful and allows you to define every aspect of your bot's behavior.
1. Create the Configuration File
When you install the bundle, our Symfony Flex recipe will automatically generate an initial, well-commented configuration file for you at the following location:
config/packages/andrew_gos_telegram_bot.yaml
This file will serve as a great starting point for your own configuration.
If you have chosen not to use Flex recipes, or if the file was not created for any reason, you can create it manually. Simply run the following command from your project's root directory:
touch config/packages/andrew_gos_telegram_bot.yaml
🧠 Why this is better:
- Sets correct expectations: It immediately informs the 99% of users with Flex enabled that the work is already done for them.
- Provides a clear fallback: It gives a simple, direct instruction for those who need to create the file manually.
- Enhances professionalism: It shows that the bundle is modern and fully integrated with the Symfony ecosystem's best practices.
2. Minimal Configuration (Quick Start)
Here is the minimum configuration required to get a single bot running. This bot will use long-polling.
# config/packages/andrew_gos_telegram_bot.yaml andrew_gos_telegram_bot: bots: # A unique name for your bot my_first_bot: factory: # 'getUpdates' is a shortcut for the long-polling factory method: 'getUpdates' arguments: # Best practice: use environment variables for your token! $token: '%env(string:TELEGRAM_BOT_TOKEN)%' # Define at least one handler to process messages handlers: - handler: App\Telegram\Handler\DefaultMessageHandler
Now, add your token to the .env file:
# .env TELEGRAM_BOT_TOKEN="123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11"
3. Full Configuration Guide
The bundle's configuration is split into three main sections for each bot: factory, plugins, and handlers.
The factory section (Required)
This defines how the Telegram service instance is created. You can use one of two shortcuts:
'default': The standard factory, best for webhooks.'getUpdates': The factory for long-polling via thelistencommand.
The plugins section (Optional)
Plugins are services that run on every incoming update before the main handlers. They are perfect for logging, analytics, or initializing session data.
plugins: - class: App\Telegram\Plugin\RequestLoggerPlugin arguments: $logger: '@monolog.logger.telegram'
The handlers section (Required for bot logic)
This is the core of your bot. It's a list of handler groups, executed based on priority. A handler group has four parts:
checker(Required): A service that decides if this handler should run for the current update.handler(Required): The service that contains the actual business logic.middlewares(Optional): A chain of services that run after thecheckerand before thehandler.priority(Optional, default0): A number to control the execution order. Higher priority runs first.
Execution Flow: For any update, the bundle checks each handler group from highest to lowest priority. The first group whose
checkerreturns true gets itsmiddlewaresandhandlerexecuted, and the process stops.
Complete Example
This example shows a complex setup with two bots, demonstrating most features.
# config/packages/andrew_gos_telegram_bot.yaml andrew_gos_telegram_bot: bots: # A powerful main bot configured for webhooks. my_main_bot: factory: method: 'default' arguments: $token: '%env(string:TELEGRAM_MAIN_BOT_TOKEN)%' plugins: - class: App\Telegram\Plugin\RequestLoggerPlugin arguments: $logger: '@monolog.logger.telegram' handlers: # Handles the '/start' command with high priority. - checker: class: App\Telegram\Checker\CommandChecker arguments: { $command: '/start' } handler: App\Telegram\Handler\StartCommandHandler middlewares: - App\Telegram\Middleware\UserAuthenticationMiddleware priority: 100 # A fallback handler for any other text message. # If 'checker' is omitted, it defaults to AnyChecker (always runs). - handler: App\Telegram\Handler\DefaultMessageHandler priority: -100 # A simple second bot for sending notifications. my_notification_bot: factory: method: 'getUpdates' arguments: $token: '%env(string:TELEGRAM_NOTIFY_BOT_TOKEN)%'
🕹️ Usage
Console Commands
The bundle provides a set of commands to manage your bots. You must always specify the bot's name from your configuration file.
-
Listen for Updates (Long-Polling)
php bin/console andrew-gos:telegram-bot:listen my_main_bot
-
Set a Webhook
# The URL must be HTTPS php bin/console andrew-gos:telegram-bot:set-webhook my_main_bot https://my-domain.com/telegram/webhook -
Get Webhook Info
php bin/console andrew-gos:telegram-bot:webhook-info my_main_bot
-
Delete a Webhook
php bin/console andrew-gos:telegram-bot:delete-webhook my_main_bot
-
Get Bot Info (useful for checking if the token is correct)
php bin/console andrew-gos:telegram-bot:bot-info my_main_bot
Creating Your Services
Your bot's logic lives in services. Here are skeletons for the main types.
-
Checker (
App\Telegram\Checker\CommandChecker.php) A checker must implementCheckerInterface.<?php namespace App\Telegram\Checker; use AndrewGos\TelegramBot\Kernel\Checker\CheckerInterface; use AndrewGos\TelegramBot\Entity\Update; readonly class CommandChecker implements CheckerInterface { public function __construct( private string $command, ) {} public function check(Update $update): bool { $message = $update->getMessage(); return $message && $message->getText() === $this->command; } }
-
Handler (
App\Telegram\Handler\StartCommandHandler.php) A handler must implementHandlerInterface.<?php namespace App\Telegram\Handler; use AndrewGos\TelegramBot\Kernel\Handler\HandlerInterface; use AndrewGos\TelegramBot\Entity\Update; use AndrewGos\TelegramBot\Telegram; use AndrewGos\TelegramBot\Request\SendMessageRequest; class StartCommandHandler implements HandlerInterface { public function handle(Update $update, Telegram $telegram): void { $chatId = $update->getMessage()->getChat()->getId(); $telegram->getApi()->sendMessage( new SendMessageRequest($chatId, 'Hello! Welcome to the bot.') ); } }
🤝 Contributing
Contributions are welcome! If you've found a bug or have an idea for a new feature, please open an issue on GitHub. If you'd like to contribute code, please open a pull request.
📜 License
This bundle is released under the MIT license. See the bundled LICENSE file for details.
andrew-gos/telegram-bot-bundle 适用场景与选型建议
andrew-gos/telegram-bot-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 163 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「symfony」 「php」 「api」 「bundle」 「bot」 「symfony-bundle」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 andrew-gos/telegram-bot-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 andrew-gos/telegram-bot-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 andrew-gos/telegram-bot-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The bundle for easy using json-rpc api on your project
A PSR-7 compatible library for making CRUD API endpoints
Bundle Symfony DaplosBundle
Alfabank REST API integration
Biblioteca PHP pura para pagamentos SISP/Vinti4 de Cabo Verde.
Zero-dependency raw PHP DNS resolver, domain-ownership verification, and intoDNS/MxToolbox-style diagnostics. Queries authoritative nameservers directly over sockets — never trusts the recursive cache for ownership checks.
统计信息
- 总下载量: 163
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-02