wenfeng / hyperf-ext-telegram
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
Requires
- php: >=8.3
- ext-curl: *
- ext-fileinfo: *
- ext-redis: *
- hyperf/redis: ^3.1
Requires (Dev)
- hyperf/cache: ~3.1.0
- hyperf/command: ~3.1.0
- hyperf/config: ~3.1.0
- hyperf/constants: ^3.1
- hyperf/database: ~3.1.0
- hyperf/db-connection: ~3.1.0
- hyperf/devtool: ~3.1.0
- hyperf/engine: ^2.10
- hyperf/framework: ~3.1.0
- hyperf/guzzle: ~3.1.0
- hyperf/http-server: ~3.1.0
- hyperf/logger: ~3.1.0
- hyperf/memory: ~3.1.0
- hyperf/paginator: ^3.1
- hyperf/process: ~3.1.0
- hyperf/swagger: ^3.1
- hyperf/testing: ~3.1.0
- hyperf/translation: ^3.1
- irazasyed/telegram-bot-sdk: ^3.15
- kornrunner/secp256k1: ^0.3.0
- mockery/mockery: ^1.0
- phper666/jwt-auth: ^4.0
- simplito/elliptic-php: ^1.0
- stephenhill/base58: ^1.1
- swoole/ide-helper: ^5.0
This package is auto-updated.
Last update: 2025-10-18 05:21:39 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;