nicoosh / telegram-bot-bundle
此捆绑包提供了在 Symfony 中使用 longman/telegram-bot 的包装器。
dev-master
2024-07-22 10:20 UTC
Requires
- php: ^8.1
- nicoosh/telegram-bot: *
- symfony/config: ^6.4 || ^7.0
- symfony/dependency-injection: ^6.4 || ^7.0
- symfony/http-foundation: ^6.4 || ^7.0
- symfony/monolog-bundle: ^3.0
This package is not auto-updated.
Last update: 2024-10-01 08:20:02 UTC
README
此捆绑包提供了在 Symfony 中使用 PHP Telegram Bot 的包装器。更多文档可以在官方仓库中找到。
安装
打开命令行,进入您的项目目录,然后执行以下命令以下载此捆绑包的最新稳定版本
symfony composer require m4n50n/telegram-bot-bundle
启用捆绑包
然后,通过将其添加到项目中 config/bundles.php
文件中注册的捆绑包列表中来启用捆绑包
// config/bundles.php return [ // ... M4n50n\TelegramBotBundle\TelegramBotBundle::class => ['all' => true], ];
配置捆绑包
# config/packages/telegram_bot.yaml telegram_bot: bots: first_bot: # Required # key: "%env(BOT_KEY)%" username: "%env(BOT_USERNAME)%" webhook: url: "%env(BOT_HOOK_URL)%" max_connections: "%env(BOT_MAX_CONNECTIONS)%" commands: paths: - "%env(BOT_COMMANDS_PATH)%" # Optional # admins: - 123456789 # Admin user id in Telegram mysql: enabled: true # false by default host: "%env(BOT_MYSQL_HOST)%" user: "%env(BOT_MYSQL_USER)%" password: "%env(BOT_MYSQL_ROOT_PASSWORD)%" database: "%env(BOT_MYSQL_DB)%" log: enabled: true # false by default debug: "%env(LOG_DEBUG_FILE_PATH)%" error: "%env(LOG_ERROR_FILE_PATH)%" update: "%env(LOG_UPDATE_FILE_PATH)%" paths: download: "%env(DOWNLOADS_PATH)%" upload: "%env(UPLOADS_PATH)%" limiter: enabled: true # false by default second_bot: # ...
如果您希望在 /config
文件夹内自动创建配置/环境文件,您可以在 composer.json
中包含我的私有 Symfony Flex 菜单仓库,通过添加以下配置
"extra": { "symfony": { "endpoint": [ "https://api.github.com/repos/m4n50n/symfony_flex_recipes/contents/index.json", "flex://defaults" ] } }
用法
每次您需要获取一个机器人实例时,请使用服务依赖注入。通过提供机器人名称,自动加载机器人配置,该名称应与配置 yaml 文件中的键匹配。例如
telegram_bot: bots: first_bot: # $botName
use M4n50n\TelegramBotBundle\Factory\TelegramBotFactory; final class TelegramBotService { public function __construct(private TelegramBotFactory $botFactory) { } public function initialize(string $botName) { // ... return $this->botFactory->get($botName); } }
方法
此包装器定义了基本的机器人配置方法。
webhookHandler()
方法将通过 outgoing webhook 接收和处理传入的更新。- 使用
setWebhook()
在 Telegram 中注册 webhook。要配置为 webhook 的端点必须位于支持 HTTPS 的服务器上。 - 使用
unsetWebhook()
注销 webhook。
如果您为这些方法创建控制器,您只需做
namespace App\Controller; // ... final class PHPCodeTesterBotController extends AbstractController { private readonly TelegramBot $bot; public function __construct(private TelegramBotService $telegramBotService) { $this->bot = $telegramBotService->initialize("bot_name"); } #[Route('/endpoint', name: 'app_webhook_endpoint')] public function webhookEndpoint(): Response { // ... $webhookHandler = $this->bot->webhookHandler(); // ... or $setWebhook = $this->bot->setWebhook(); // ... or $unsetWebhook = $this->bot->unsetWebhook(); // ... } }
贡献
有关更多信息,请参阅 CONTRIBUTING
安全
有关更多信息,请参阅 SECURITY
许可证
请参阅此仓库中包含的 LICENSE 以获取 MIT 许可证的完整副本,该项目据此许可。