m4n50n/telegram-bot-bundle

此包为在Symfony中使用longman/telegram-bot提供了封装。

安装: 43

依赖者: 0

建议者: 0

安全: 0

星标: 16

关注者: 1

分支: 1

开放问题: 0

类型:symfony-bundle

0.1 2024-02-12 14:47 UTC

This package is auto-updated.

Last update: 2024-09-26 10:31:18 UTC


README

Latest Stable Version License Total Downloads

此包为在Symfony中使用PHP Telegram Bot提供了封装。更多文档可以在官方仓库中找到。

安装

打开命令行,进入您的项目目录,并执行以下命令以下载此包的最新稳定版本

symfony composer require m4n50n/telegram-bot-bundle

启用Bundle

然后,通过将其添加到项目中config/bundles.php文件中注册的包列表中,来启用此包

// config/bundles.php

return [
    // ...
    M4n50n\TelegramBotBundle\TelegramBotBundle::class => ['all' => true],
];

配置Bundle

# 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() 方法将通过外发钩子接收和处理传入的更新。
  • 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许可证的完整副本,该项目受此许可证的许可。