luzrain/telegram-bot-bundle
Composer 安装命令:
composer require luzrain/telegram-bot-bundle
包简介
Symfony bundle for Telegram Bot API
README 文档
README
A symfony bundle for luzrain/telegram-bot-api library.
Getting Started
Install Composer Packages
$ composer require luzrain/telegram-bot-bundle symfony/http-client nyholm/psr7
Enable the Bundle
<?php // config/bundles.php return [ // ... Luzrain\TelegramBotBundle\TelegramBotBundle::class => ['all' => true], ];
Configure the Bundle
# config/packages/telegram_bot.yaml telegram_bot: api_token: API_TOKEN # webhook: # url: https://localhost/tg-webhook
Note that symfony/http-client and nyholm/psr7 are not required. You may use any PSR-18 http client and PSR-17 factory implementations.
To use your own services, set the http_client, request_factory, and stream_factory options in the telegram_bot.yaml configuration file.
Here is an example how to use Guzzle http client:
# config/services.yaml psr18.guzzle_client: class: GuzzleHttp\Client arguments: - http_errors: false psr17.guzzle_factory: class: GuzzleHttp\Psr7\HttpFactory
# config/packages/telegram_bot.yaml telegram_bot: api_token: API_TOKEN http_client: psr18.guzzle_client request_factory: psr17.guzzle_factory stream_factory: psr17.guzzle_factory
For a full list of available configuration options and their documentation, run:
$ bin/console config:dump-reference telegram_bot
Receiving Messages from Telegram
There are two ways to receive updates from Telegram:
1. Webhook (recommended)
- Configure a webhook and make sure the endpoint is publicly accessible.
# config/routes.yaml # ... telegram_webhook: path: /tg-webhook controller: telegram_bot.webhook_controller
- Set the
webhook.urloption in yourtelegram_bot.yamlconfiguration file. - Apply the webhook settings to your bot:
$ bin/console telegram:webhook:update
Whenever you change the webhook or allowed_updates configuration options, run this command again to update the bot's webhook settings.
2. Polling Daemon
Useful during development or when you cannot expose a public URL.
Start the polling daemon:
$ bin/console telegram:polling:start
Examples
Command Controller
use Luzrain\TelegramBotApi\Method; use Luzrain\TelegramBotApi\Type; use Luzrain\TelegramBotBundle\Attribute\OnCommand; use Luzrain\TelegramBotBundle\TelegramCommand; final class StartCommandController extends TelegramCommand { // You can pass command arguments next to $message. // Be aware to set default values for arguments as they won't necessarily will be passed #[OnCommand('/start')] public function __invoke(Type\Message $message, string $arg1 = '', string $arg2 = ''): Method { return $this->reply('Hello from symfony bot'); } }
Any Event Controller
use Luzrain\TelegramBotApi\Event; use Luzrain\TelegramBotApi\Method; use Luzrain\TelegramBotApi\Type; use Luzrain\TelegramBotBundle\Attribute\OnEvent; // It's not necessary to extend TelegramCommand final class OnMessageController { // Listen any available event from Event namespace #[OnEvent(Event\Message::class)] public function __invoke(Type\Message $message): Method { return new Method\SendMessage( chatId: $message->chat->id, text: 'You wrote: ' . $message->text, ); } }
Publishing Commands to the Bot Menu Button
You can publish your bot commands so they appear in the bot’s menu button.
To do this, set both the description and publish arguments in the OnCommand attribute:
#[OnCommand(command: '/command1', description: 'Test command 1', publish: true)]
Publish commands list to the menu button:
$ bin/console telegram:button:update
To remove menu button:
$ bin/console telegram:button:delete
luzrain/telegram-bot-bundle 适用场景与选型建议
luzrain/telegram-bot-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 32.14k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2023 年 06 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「telegram」 「telegram-bot」 「Telegram-Bot-API」 「telegram-bundle」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 luzrain/telegram-bot-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 luzrain/telegram-bot-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 luzrain/telegram-bot-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Clear and simple Telegram bot API
A simple wrapper for telegram-bot-api.
Tools set for messenger managing. Allows you to send any content via Telegram, Viber, WhatsApp, VK and so on
Simple and convenient object-oriented implementation Telegram bot API with php version ^7.4 support. You'll like it)
Laravel 5.6 Monolog custom telegram channel
Telegram Bot PHP API
统计信息
- 总下载量: 32.14k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-06-28