boshurik/telegram-bot-bundle

Telegram机器人包

安装数: 66,984

依赖: 0

建议者: 0

安全: 0

星标: 71

关注者: 3

分支: 23

公开问题: 1

类型:symfony-bundle

5.0.2 2023-12-22 12:45 UTC

README

基于 telegram-bot/api 库的 Telegram机器人包

示例

查看 示例项目

安装

Composer

$ composer require boshurik/telegram-bot-bundle

如果你使用 symfony/flex,只需设置 TELEGRAM_BOT_TOKEN 环境变量

注册包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new BoShurik\TelegramBotBundle\BoShurikTelegramBotBundle,
    );
    // ...
}

添加webhook路由

BoShurikTelegramBotBundle:
    resource: "@BoShurikTelegramBotBundle/Resources/config/routing.php"
    prefix: /_telegram/%telegram_bot_route_secret%

或多个机器人

BoShurikTelegramBotBundle:
    resource: "@BoShurikTelegramBotBundle/Resources/config/routing.php"
    prefix: /_telegram/{bot}/%telegram_bot_route_secret%

配置

boshurik_telegram_bot:
    api:
        token: "%telegram_bot_api_token%"
        proxy: "socks5://127.0.0.1:8888"

或多个机器人

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"

使用

API

获取默认机器人API

use TelegramBot\Api\BotApi;
public function __construct(private BotApi $api)

对于多个机器人

use BoShurik\TelegramBotBundle\Telegram\BotLocator;
use TelegramBot\Api\BotApi;

public function foo(BotLocator $botLocator)
{
    /** @var BotApi $api */
    $api = $botLocator->get('first');
}

或使用类型为 TelegramBot\Api\BotApi 且名称模式为 /\${name}(Bot|BotApi|Api)?$/ 的参数

use TelegramBot\Api\BotApi;
public function __construct(private BotApi $firstBotApi)

更多信息请参阅 用法 部分,位于 telegram-bot/api 库中

获取更新

bin/console telegram:updates
bin/console telegram:updates first

更多信息请参阅 官方文档

Webhook

设置
bin/console telegram:webhook:set [url-or-hostname] [<path-to-certificate>]
bin/console telegram:webhook:set [url-or-hostname] [<path-to-certificate>] --bot first

如果未设置 url-or-hostname,则命令将基于 请求上下文 生成URL

取消设置
bin/console telegram:webhook:unset
bin/console telegram:webhook:unset first

更多信息请参阅 官方文档

异步命令处理

为了提高性能,你可以利用 Messenger 通过 Messenger 传输在稍后处理webhooks。

composer req symfony/messenger
# config/packages/messenger.yaml
framework:
    messenger:
        transports:
            async: "%env(MESSENGER_TRANSPORT_DSN)%"

        routing:
            'BoShurik\TelegramBotBundle\Messenger\TelegramMessage': async

添加命令

命令必须实现 \BoShurik\TelegramBotBundle\Telegram\Command\CommandInterface

你可以从 \BoShurik\TelegramBotBundle\Telegram\Command\AbstractCommand 开始

要注册命令:在服务定义中添加标签 boshurik_telegram_bot.command

app.telegram.command:
    class: AppBundle\Telegram\Command\SomeCommand
    tags:
        - { name: boshurik_telegram_bot.command }

如果你使用 autoconfigure 标签将自动添加

对于拥有多个机器人的应用,你需要传递机器人ID

app.telegram.command:
    class: AppBundle\Telegram\Command\SomeCommand
    tags:
        - { name: boshurik_telegram_bot.command, bot: first }

如果你需要为多个机器人使用相同的命令,你必须为每个机器人添加多个标签

app.telegram.command:
    class: AppBundle\Telegram\Command\SomeCommand
    tags:
        - { name: boshurik_telegram_bot.command, bot: first }
        - { name: boshurik_telegram_bot.command, bot: second }

存在预定义的 \BoShurik\TelegramBotBundle\Telegram\Command\HelpCommand。它显示实现 \BoShurik\TelegramBotBundle\Telegram\Command\PublicCommandInterface 的命令

你需要注册它

app.telegram.command.help:
    class: BoShurik\TelegramBotBundle\Telegram\Command\HelpCommand
    arguments:
        - '@boshurik_telegram_bot.command.registry.default'
    tags:
        - { name: boshurik_telegram_bot.command }

或多个机器人

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 }

事件

对于更复杂的应用(例如会话),你可以监听 BoShurik\TelegramBotBundle\Event\UpdateEvent 事件

/**
 * @param UpdateEvent $event
 */
public function onUpdate(UpdateEvent $event)
{
    $update = $event->getUpdate();
    $message = $update->getMessage();
}

使用Telegram登录

此包支持通过Telegram API进行登录

如果你想允许你的机器人的用户登录而无需再次注册,请按照以下 说明 操作。