boshurik/telegram-bot-bundle
Composer 安装命令:
composer require boshurik/telegram-bot-bundle
包简介
Telegram bot bundle
README 文档
README
Telegram bot bundle on top of telegram-bot/api library
Examples
See example project
Installation
Composer
$ composer require boshurik/telegram-bot-bundle
If you are using symfony/flex all you need is to set TELEGRAM_BOT_TOKEN environment variable
Register the bundle
<?php // app/AppKernel.php public function registerBundles() { $bundles = array( // ... new BoShurik\TelegramBotBundle\BoShurikTelegramBotBundle, ); // ... }
Add routing for webhook
BoShurikTelegramBotBundle: resource: "@BoShurikTelegramBotBundle/Resources/config/routing.php" prefix: /_telegram/%telegram_bot_route_secret%
or for multiple bots:
BoShurikTelegramBotBundle: resource: "@BoShurikTelegramBotBundle/Resources/config/routing.php" prefix: /_telegram/{bot}/%telegram_bot_route_secret%
Configuration
boshurik_telegram_bot: api: token: "%telegram_bot_api_token%" proxy: "socks5://127.0.0.1:8888"
or for multiple bots:
boshurik_telegram_bot: api: default_bot: first bots: first: "%first_telegram_bot_api_token%" second: "%second_telegram_bot_api_token%" proxy: "socks5://127.0.0.1:8888"
Usage
API
To get default bot api:
use TelegramBot\Api\BotApi; public function __construct(private BotApi $api)
For multiple bots:
use BoShurik\TelegramBotBundle\Telegram\BotLocator; use TelegramBot\Api\BotApi; public function foo(BotLocator $botLocator) { /** @var BotApi $api */ $api = $botLocator->get('first'); }
or use argument with type TelegramBot\Api\BotApi and name pattern /\${name}(Bot|BotApi|Api)?$/
use TelegramBot\Api\BotApi; public function __construct(private BotApi $firstBotApi)
For more info see Usage section in telegram-bot/api library
Getting updates
bin/console telegram:updates bin/console telegram:updates first
For more information see official documentation
Webhook
Set
bin/console telegram:webhook:set [url-or-hostname] [<path-to-certificate>] bin/console telegram:webhook:set [url-or-hostname] [<path-to-certificate>] --bot first
If url-or-hostname is not set command will generate url based on request context
Unset
bin/console telegram:webhook:unset bin/console telegram:webhook:unset first
For more information see official documentation
Async command processing
To improve performance, you can leverage Messenger to process webhooks later via a Messenger transport.
composer req symfony/messenger
# config/packages/messenger.yaml framework: messenger: transports: async: "%env(MESSENGER_TRANSPORT_DSN)%" routing: 'BoShurik\TelegramBotBundle\Messenger\TelegramMessage': async
Adding commands
Commands must implement \BoShurik\TelegramBotBundle\Telegram\Command\CommandInterface
There is \BoShurik\TelegramBotBundle\Telegram\Command\AbstractCommand you can start with
To register command: add tag boshurik_telegram_bot.command to service definition
app.telegram.command: class: AppBundle\Telegram\Command\SomeCommand tags: - { name: boshurik_telegram_bot.command }
If you use autoconfigure tag will be added automatically
For application with multiple bots you need to pass bot id:
app.telegram.command: class: AppBundle\Telegram\Command\SomeCommand tags: - { name: boshurik_telegram_bot.command, bot: first }
If you need to use same command for multiple bots you must add multiple tags for each bot:
app.telegram.command: class: AppBundle\Telegram\Command\SomeCommand tags: - { name: boshurik_telegram_bot.command, bot: first } - { name: boshurik_telegram_bot.command, bot: second }
There is predefined \BoShurik\TelegramBotBundle\Telegram\Command\HelpCommand.
It displays commands which additionally implement \BoShurik\TelegramBotBundle\Telegram\Command\PublicCommandInterface
You need to register it:
app.telegram.command.help: class: BoShurik\TelegramBotBundle\Telegram\Command\HelpCommand arguments: - '@boshurik_telegram_bot.command.registry.default' tags: - { name: boshurik_telegram_bot.command }
or for multiple bots:
app.telegram.command.help: class: BoShurik\TelegramBotBundle\Telegram\Command\HelpCommand arguments: - '@boshurik_telegram_bot.command.registry.first' tags: - { name: boshurik_telegram_bot.command, bot: first }
Events
For more complex application (e.g. conversations) you can listen for BoShurik\TelegramBotBundle\Event\UpdateEvent event
/** * @param UpdateEvent $event */ public function onUpdate(UpdateEvent $event) { $update = $event->getUpdate(); $message = $update->getMessage(); }
Login with Telegram
This bundle supports login through Telegram Api
If you want to allow your Bot's users to login without requiring them to register again follow these instructions.
boshurik/telegram-bot-bundle 适用场景与选型建议
boshurik/telegram-bot-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 119.36k 次下载、GitHub Stars 达 80, 最近一次更新时间为 2016 年 04 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 boshurik/telegram-bot-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 boshurik/telegram-bot-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 119.36k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 81
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-04-25