wenfeng/hyperf-ext-telegram

There is no license information available for the latest version (v2.11.26) of this package.

A telegram reusable Hyperf module template with JWT and routes

Installs: 52

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/wenfeng/hyperf-ext-telegram

v2.11.26 2025-10-18 05:21 UTC

README

安装

composer require wenfeng/hyperf-ext-telegram

// 发布配置文件 在 config/autoload下生成 telegram.php 配置文件
php bin/hyperf vendor:publish wenfeng/hyperf-ext-telegram

// 数据库迁移
php bin/hyperf migrate

使用

配置 autoload/telegram.php

1、dev_token 机器人TOKEN
2、mode  模式:pulling 主动拉取模式,webhook模式两种
webhook模式下无需设置url,会根据机器人id和token自动注册webhook,
默认的回调处理 Controller/WebhookController.php 可继承重写 
3、menus
底部菜单二维数组,一维是行,二维是按钮
4、languages
机器人支持的语言
5、commands
机器人的所有的命令,以/开头
6、command_handlers
命令处理类:

消息回复写法示例

<?php

namespace App\Bot\Reply;

use App\Constants\BotConstants;
use App\Context\LangContext;
use Telegram\Bot\Api;
use Telegram\Bot\Exceptions\TelegramSDKException;
use William\HyperfExtTelegram\Core\AbstractMessage;

class WelcomeMessage extends AbstractMessage
{
    public function __construct(Api               $telegram, int $chatId,
                                protected array   $keyboards,
                                protected ?string $username = null)
    {
        parent::__construct($telegram, $chatId);
    }

    /**
     * @throws TelegramSDKException
     */
    public function reply(): void
    {
        $this->newMessage()
            ->photo('welcome.jpg')
            ->transMessage(BotConstants::MESSAGE_WELCOME, ['bot_username' => $this->username ?? ""])
            ->addRow()->addButton(//按钮...)
            ->replyMarkup($this->keyboards)
            ->send($this->telegram);
    }
}

机器人command

使用注解 William\HyperfExtTelegram\Core\Annotation\Command

<?php

namespace App\Bot\Command;

use App\Bot\Reply;
use App\FlushEnergyMatrix\MatrixService;
use App\Helper\Logger;
use App\Model\User;
use Hyperf\Contract\TranslatorInterface;
use Telegram\Bot\Exceptions\TelegramSDKException;
use Telegram\Bot\Objects\Update;
use William\HyperfExtTelegram\Core\AbstractCommand;
use William\HyperfExtTelegram\Core\Annotation\Command;
use William\HyperfExtTelegram\Core\Instance;
use function Hyperf\Support\make;

#[Command(command: '/start')]
class StartCommand extends AbstractCommand
{
    protected MatrixService $matrix;

    public function __construct(TranslatorInterface $translator)
    {
        parent::__construct($translator);
        $this->matrix = make(MatrixService::class);
    }

    /**
     * @throws TelegramSDKException
     */
    public function handle(Instance $instance, Update $update): void
    {
        $instance->reply(new Reply\WelcomeMessage(
            $instance->telegram,
            $instance->getChatId($update),
            $this->matrix->getRandomAddressList($instance->getBotID()),
        ));
    }
}

按钮 Query Callback 注解

<?php

namespace App\Bot\Query;

use App\Bot\Reply\WelcomeMessage;
use App\Helper\Logger;
use Hyperf\Contract\TranslatorInterface;
use Telegram\Bot\Exceptions\TelegramSDKException;
use William\HyperfExtTelegram\Core\AbstractQueryCallback;
use William\HyperfExtTelegram\Core\Annotation\QueryCallback;

#[QueryCallback(path: '/switch_language')]
class SwitchLanguage extends AbstractQueryCallback
{

    public function __construct(TranslatorInterface $translator, protected MatrixService $matrix)
    {
    }

    /**
     * @throws TelegramSDKException
     * @throws \RedisException
     */
    function _handle(): void
    {
        Logger::debug("switching language...");
        $this->telegramInstance->changeLanguage($this->telegramUpdate);
        $this->reply(WelcomeMessage::class);
    }
}

底部菜单Menu注解

<?php

namespace App\Bot\Menu;

use App\Bot\Menus;
use App\Bot\Reply\MyInfo;
use App\Helper\Logger;
use App\Service\UserService;
use Telegram\Bot\Exceptions\TelegramSDKException;
use William\HyperfExtTelegram\Core\AbstractMenu;
use William\HyperfExtTelegram\Core\Annotation\Menu;

#[Menu(menu: Menus::MINE)]
class MineMenu extends AbstractMenu
{
    public function __construct(protected UserService $userService)
    {
    }

    /**
     * @throws TelegramSDKException
     */
    function _handle(): void
    {
        Logger::debug("Menu#mine ...");
        $this->reply(MyInfo::class, $this->userService->getUserInfo($this->telegramInstance->getCurrentUser()));
    }
}

充值服务

use William\HyperfExtTelegram\Service\RechargeService;