定制 m4n50n/telegram-bot-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

m4n50n/telegram-bot-bundle

最新稳定版本:0.1

Composer 安装命令:

composer require m4n50n/telegram-bot-bundle

包简介

This bundle provides a wrapper for using longman/telegram-bot inside Symfony.

README 文档

README

Latest Stable Version License Total Downloads

This bundle provides a wrapper for using PHP Telegram Bot inside Symfony. Additional documentation can be found in the official repository.

Installation

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

symfony composer require m4n50n/telegram-bot-bundle

Enable the Bundle

Then, enable the bundle by adding it to the list of registered bundles in config/bundles.php file of your project:

// config/bundles.php

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

Configure the 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:
      # ...

If you want the configuration / environment files to be created automatically inside your /config folder, you can include my private Symfony Flex recipes repository in your composer.json by adding the following configuration:

"extra": {
  "symfony": {
      "endpoint": [
          "https://api.github.com/repos/m4n50n/symfony_flex_recipes/contents/index.json",
          "flex://defaults"
      ]
  }
}

Usage

Whenever you need to get a bot instance, use dependency injection for your service. The bot configuration is automatically loaded by providing the bot name, which should match the key in the configuration yaml file. For example:

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);        
    }
}

Methods

This wrapper defines the basic bot configuration methods.

  • webhookHandler() Method that will receive and handle incoming updates via an outgoing webhook.
  • setWebhook() to register the webhook with Telegram. The endpoint to be configured as a webhook must be on a server with HTTPS support.
  • unsetWebhook() to unregister the webhook.

If you create controllers for these methods, you will only have to do:

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

See CONTRIBUTING for more information.

Security

See SECURITY for more information.

License

Please see the LICENSE included in this repository for a full copy of the MIT license, which this project is licensed under.

统计信息

  • 总下载量: 60
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 20
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 18
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-02-09