vet1kk/telegram-bot-core 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

vet1kk/telegram-bot-core

Composer 安装命令:

composer require vet1kk/telegram-bot-core

包简介

Lightweight Telegram Bot PHP Core Library

README 文档

README

PHP 8.3+ Packagist Version GitHub Release Packagist Downloads Telegram Bot API PHPUnit Psalm Type Coverage Line Coverage License

Lightweight Telegram bot core for PHP 8.3+ with typed updates, attribute-based handlers, middleware, events, and a small DI-driven runtime.

It is designed for projects that want Telegram bot wiring without bringing in a full framework.

Highlights

  • Strictly typed PHP 8.3 codebase
  • Attribute-based commands, actions, and listeners
  • Typed DTO tree for Telegram updates
  • Middleware pipeline for cross-cutting behavior
  • Event manager for decoupled side effects
  • Inline and reply keyboard builders
  • PSR-11 container support through php-di/php-di
  • Covered by PHPUnit tests and Psalm static analysis

Requirements

  • PHP 8.3+
  • Extensions: curl, json, mbstring
  • A Telegram bot token from @BotFather
  • A public webhook URL for production webhook handling

Installation

composer require vet1kk/telegram-bot-core

Quick start

Create a webhook entrypoint and register your handlers. The library bootstraps its core services for you.

<?php

declare(strict_types=1);

use Bot\Bot;
use App\Command\StartCommand;
use App\Action\MenuAction;
use App\Listener\FallbackListener;
use App\Middleware\TimingMiddleware;
use App\Provider\AppServiceProvider;

require __DIR__ . '/vendor/autoload.php';

Bot::create($_ENV['TELEGRAM_BOT_TOKEN'])
    ->withServiceProvider(new AppServiceProvider())
    ->withMiddleware(TimingMiddleware::class)
    ->withCommand(StartCommand::class)
    ->withAction(MenuAction::class)
    ->withListener(FallbackListener::class)
    ->runFromWebhook();

If you only need to register the webhook once:

<?php

declare(strict_types=1);

use Bot\Bot;

require __DIR__ . '/vendor/autoload.php';

Bot::create($_ENV['TELEGRAM_BOT_TOKEN'])
    ->registerWebhook('https://example.com/telegram/webhook.php');

Your first command

Commands handle Telegram messages such as /start.

<?php

declare(strict_types=1);

namespace App\Command;

use Bot\Attribute\Command;
use Bot\Command\CommandInterface;
use Bot\DTO\Update\MessageUpdateDTO;

#[Command(name: 'start', description: 'Send a welcome message')]
final class StartCommand implements CommandInterface
{
    public function handle(MessageUpdateDTO $update): void
    {
        $update->reply('Hello! Your bot is running.');
    }
}

Your first action

Actions handle callback_query updates from inline keyboard buttons.

<?php

declare(strict_types=1);

namespace App\Action;

use Bot\Action\ActionInterface;
use Bot\Attribute\Action;
use Bot\DTO\Update\CallbackQueryUpdateDTO;
use Bot\Keyboard\Buttons\InlineButton;
use Bot\Keyboard\InlineKeyboard;

#[Action(name: 'menu', description: 'Open the main menu')]
final class MenuAction implements ActionInterface
{
    public function handle(CallbackQueryUpdateDTO $update, array $params = []): void
    {
        $keyboard = InlineKeyboard::create()
            ->addButton(
                InlineButton::create()
                    ->setText('Help')
                    ->setCallbackData('help'),
                1
            )
            ->addButton(
                InlineButton::create()
                    ->setText('Settings')
                    ->setCallbackData('settings'),
                1
            );

        $update->reply('Choose an option:', $keyboard);
    }
}

Middleware

Middleware runs before the router dispatches an update and is useful for timing, auth, maintenance mode, or logging.

<?php

declare(strict_types=1);

namespace App\Middleware;

use Bot\DTO\Update\UpdateDTO;
use Bot\Middleware\MiddlewareInterface;
use Psr\Log\LoggerInterface;

final class TimingMiddleware implements MiddlewareInterface
{
    public function __construct(private LoggerInterface $logger)
    {
    }

    public function process(UpdateDTO $update, callable $next): void
    {
        $startedAt = microtime(true);

        $next($update);

        $this->logger->info('Update handled', [
            'duration_ms' => (int) ((microtime(true) - $startedAt) * 1000),
        ]);
    }
}

Event listeners

Listeners let you react to lifecycle events without coupling side effects to your handlers.

Built-in events include:

  • Bot\Event\Events\ReceivedEvent
  • Bot\Event\Events\CommandHandledEvent
  • Bot\Event\Events\ActionHandledEvent
  • Bot\Event\Events\UnhandledEvent
<?php

declare(strict_types=1);

namespace App\Listener;

use Bot\Attribute\Listener;
use Bot\Event\Events\UnhandledEvent;
use Bot\Listener\ListenerInterface;
use Psr\Log\LoggerInterface;

final class FallbackListener implements ListenerInterface
{
    public function __construct(private LoggerInterface $logger)
    {
    }

    #[Listener(eventClass: UnhandledEvent::class)]
    public function onUnhandled(UnhandledEvent $event): void
    {
        $this->logger->info('Unhandled update received');

        $update = $event->getUpdate();
        if ($update->getChatId() !== null) {
            $update->reply("Sorry, I didn't understand that message.");
        }
    }
}

Service providers and customization

The core runtime registers sensible defaults, including a NullLogger, webhook handler, client, router, managers, and update factory. Override or extend them with your own service provider.

<?php

declare(strict_types=1);

namespace App\Provider;

use Bot\Provider\ServiceProviderInterface;
use Bot\Webhook\WebhookHandlerInterface;
use DI\Container;
use App\Webhook\FrameworkWebhookHandler;

final class AppServiceProvider implements ServiceProviderInterface
{
    public function register(Container $container): void
    {
        $container->set(
            WebhookHandlerInterface::class,
            \DI\autowire(FrameworkWebhookHandler::class)
        );
    }
}

If you are integrating this library into Laravel, Symfony, Slim, or another HTTP stack, swapping Bot\Webhook\WebhookHandlerInterface is usually the first extension point.

Running updates manually

For tests or custom transports, you can create an UpdateDTO and execute the pipeline directly with Bot::run($update).

Bot::runFromWebhook() is just a convenience around reading the webhook payload, building the update DTO, emitting ReceivedEvent, and routing the update through middleware.

Development commands

composer test
composer test:coverage
composer psalm
composer psalm:stats
composer phpcs
composer phpcbf

Quality snapshot

Current local quality snapshot for this repository:

  • Psalm inferred types for 99.7207% of the codebase
  • PHPUnit suite: 106 tests, 240 assertions
  • PHPUnit coverage: 98.17% lines, 94.93% methods, 85.37% classes

You can refresh these numbers locally with:

composer psalm
composer test:coverage

Project notes

  • Namespace: Bot\\
  • License: MIT
  • Homepage: https://github.com/vet1kk/telegram-bot-core
  • Issue tracker: https://github.com/vet1kk/telegram-bot-core/issues

Contributing

Issues and pull requests are welcome. If you add new features or integrations, please include tests where it makes sense so the public API stays reliable.

vet1kk/telegram-bot-core 适用场景与选型建议

vet1kk/telegram-bot-core 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 02 月 16 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「library」 「bot」 「core」 「webhook」 「long-polling」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 vet1kk/telegram-bot-core 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 vet1kk/telegram-bot-core 我们能提供哪些服务?
定制开发 / 二次开发

基于 vet1kk/telegram-bot-core 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 0
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 17
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-16