borsaco/telegram-bot-api-bundle

telegram-bot-api的一个简单包装器。

安装次数: 27,224

依赖项: 0

建议者: 0

安全性: 0

星标: 55

关注者: 3

分支: 17

开放问题: 0

类型:symfony-bundle

v1.1.2 2022-01-29 19:50 UTC

This package is auto-updated.

Last update: 2024-09-17 21:16:25 UTC


README

Telegram Bot API提供的symfony包装器包。

安装

通过Composer

composer require borsaco/telegram-bot-api-bundle

配置包

此包设计为开箱即用。要使此包运行,您只需配置bot的token即可。

# config/packages/telegram.yaml

telegram_bot_api:
    # Proxy (optional) :
    #proxy: 'socks5h://127.0.0.1:5090' # this is example you have to change this
    #async_requests: false

    # Development section:
    development:
        # implement in next version
        # Telegram user_id of developers accounts
        developers_id: [1234567, 87654321]
        # If this mode is enabled, the robot only responds to the developers
        maintenance:
            text: "The robot is being repaired! Please come back later."
  
    # Bots:
    bots:
        # The bot name
        first:
            # Your bot token: (required)
            token: 123456789:ABCD1234****4321CBA
            maintenance: false
        second:
            # Your bot token: (required)
            token: 123456789:ABCD1234****4321CBA
            maintenance: false
    
    # The default bot returned when you call getBot()
    default: 'second' 

使用方法

您可以使用以下方式在控制器中访问bot

    use Borsaco\TelegramBotApiBundle\Service\Bot;

    ...

    public function index(Bot $bot)
    {
        $firstBot = $bot->getBot('first');
        $firstBot->getMe();
    }

Webhook

为了通过Webhook接收更新,您首先需要将您的webhook URL告诉Telegram。您可以使用setWebhook方法指定一个URL,并通过出站webhook接收传入的更新,或者使用以下命令:

获取bot webhook信息

    $ php bin/console telegram:bot:webhook:info

设置bot的webhook URL

    $ php bin/console telegram:bot:webhook:set

删除bot的webhook

    $ php bin/console telegram:bot:webhook:delete

使用setWebhook方法设置webhook后,您可以使用下面的函数检索发送到您的Webhook URL的更新。该函数返回一个包含Update对象的数组。

    $updateArray = $firstBot()->getWebhookUpdate();

维护

如果您想使用bot的维护功能,您可以在入口点控制器中检查$bot->isMaintenance('bot_name'),并发送响应消息。

下一...

请参阅Telegram Bot API官方文档以获取有关可用方法和其他信息的详细信息。

故障排除

如果您已经正确配置了所有设置,但在getMe()方法上仍然出现错误(Http错误500),那么可能是因为SSL验证问题。请确保您有最新的CA根证书包用于与cURL一起使用。

您可以通过以下方式配置您的CA根证书包

  1. 从cURL网站下载最新的cacert.pem文件,并在
  2. 您的php.ini文件中设置一个路径,例如在Windows上

curl.cainfo=c:\php\cacert.pem

您可以使用此在线webtool测试您的SSL设置:SSL Labs

许可证

BSD许可证。有关更多信息,请参阅许可证文件