oihso / telegram-bot-api-bundle

telegram-bot-api的简单封装。

安装: 7

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 17

开放问题: 0

类型:symfony-bundle

v2.0.1 2024-02-26 16:14 UTC

This package is auto-updated.

Last update: 2024-09-26 17:26:39 UTC


README

Telegram Bot API提供的symfony封装包。

与原始版本有什么区别?

  • 更新到Symfony 6.4/7.0(暂时移除了Symfony 7的支持,直到irazasyed/telegram-bot-sdk和illuminate/support的作者提高他们的包要求)
  • 修复了一些弃用问题

安装

通过Composer

composer require oihso/telegram-bot-api-bundle

如果是从原始包迁移

  1. 通过运行composer remove borsaco/telegram-bot-api-bundle来移除旧包
  2. 将代码中的use语句从Borsaco\...更改为Oihso\...
  3. 通过运行composer require oihso/telegram-bot-api-bundle来安装新包

新版本的包完全兼容旧版本。除了use语句外,无需更改配置/代码

配置包

此包被设计为即开即用。为了使此包运行,您必须配置的只有一个东西,那就是您的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 Oihso\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();

维护

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

下一步...

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

故障排除

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

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

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

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

您可以使用此方便的在线Web工具测试您的SSL设置: SSL Labs

许可

BSD许可。请参阅许可文件以获取更多信息。