PHP code example of wenfeng / hyperf-ext-telegram

1. Go to this page and download the library: Download wenfeng/hyperf-ext-telegram library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wenfeng / hyperf-ext-telegram example snippets


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
命令处理类:




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



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



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



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;

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

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