luzrain/telegram-bot-bundle

Telegram Bot API 的 Symfony 扩展包

v2.0.0 2024-02-29 18:03 UTC

This package is auto-updated.

Last update: 2024-08-30 12:28:44 UTC


README

PHP >=8.2 Symfony ^7.0 Tests Status

luzrain/telegram-bot-api库提供的 symfony 扩展包。

入门

安装 composer 包

$ composer require luzrain/telegram-bot-bundle symfony/http-client nyholm/psr7

启用扩展包

<?php
// config/bundles.php

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

配置扩展包

# config/packages/telegram_bot.yaml

telegram_bot:
  api_token: API_TOKEN
#  webhook:
#    url: https://localhost/tg-webhook

可选。配置 webhook 路由

# config/routes.yaml

# ...
telegram_webhook:
  path: /tg-webhook
  controller: telegram_bot.webhook_controller

注意,symfony/http-clientnyholm/psr7不是必需的。您可以使用任何PSR-18客户端和PSR-17工厂。
telegram_bot.yaml配置文件中设置http_clientrequest_factorystream_factory选项中的自定义服务。
以下是如何使用guzzle http 客户端的示例

# 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:
  http_client: psr18.guzzle_client
  request_factory: psr17.guzzle_factory
  stream_factory: psr17.guzzle_factory
  api_token: API_TOKEN

有关所有可用选项的完整列表和文档,请参阅命令输出。

$ bin/console config:dump-reference telegram_bot

从 Telegram 获取消息

有两种方式可以从 Telegram 获取消息。

Webhook. 推荐方式。

您必须配置 webhook 路由,并使其可以通过互联网访问。
telegram_bot.yaml配置文件中配置webhook.url选项;
使用命令更新 Telegram bot 中的 webhook 配置。

$ bin/console telegram:webhook:update

请注意,每次您在配置文件中更改webhookallowed_updates选项时,都应该运行此命令来更新 Telegram bot 设置。

轮询守护进程。

在开发环境中使用或在无法提供对 webhook url 的公共访问时使用。
使用命令运行轮询守护进程。

$ bin/console telegram:polling:start

示例

命令控制器

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

任何事件控制器

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

发布命令列表作为 bot 按钮

可以将所有命令发布出来,它们将以 bot 菜单按钮中的可用命令列表的形式显示。为此,在 OnCommand 属性中填写description字段和publish标志。

#[OnCommand(command: '/command1', description: 'Test command 1', publish: true)]

运行发布命令。

$ bin/console telegram:button:update

对于按钮删除。

$ bin/console telegram:button:delete