makhnanov/php-telegram-bot
Composer 安装命令:
composer require makhnanov/php-telegram-bot
包简介
PHP 8.5.2 Telegram Bot API Library.
README 文档
README
PHP Telegram Bot Library
Installation
composer require makhnanov/php-telegram-bot
Long Polling Example
<?php # require 'vendor/autoload.php'; use Makhnanov\TelegramBot\Bot; $bot = new Bot('ENTER-BOT-TOKEN-HERE'); # $secondBot = new Bot('BOT_TOKEN'); # For another bot # $bot = bot(); # Automatically gets TELEGRAM_BOT_TOKEN from env and make single global instance # $chatId = 'ENTER_CHAT_ID_HERE_FOR_GET_FIRST_MESSAGES'; # $bot->sendMessage(chatId: $chatId, text: 'Hello, World!'); # $bot->sendMessage( # chatId: $chatId, # text: '<b>Bold</b> and <i>italic</i>', # parseMode: 'HTML', # ); # Simple Echo Long Polling foreach ($bot->poll() as $update) { if ($update->message?->text === '/start') { $bot->sendMessage( chatId: $update->message->chat->id, text: 'Welcome!', ); } elseif ($update->message?->text) { $bot->sendMessage( chatId: $update->message->chat->id, text: $update->message->text, ); } else { $bot->sendMessage( chatId: $update->message->chat->id, text: 'Not simple message!', ); } } # ToDo: think about catch exceptions & $allowedUpdates
Philosophy
- Minimalism: only what's necessary, no bloat
- Conciseness: user code should be short
- Readability: library source code is understandable without documentation
- PHP 8.5: leverage all modern language features
Structure
Src/
├── Bot.php # Main class, entry point
├── Functions.php # Global helper bot()
├── Api.php # HTTP client for API requests
├── Method/ # API methods (traits for Bot)
│ ├── SendMessage.php
│ ├── SendPhoto.php
│ ├── GetUpdates.php
│ └── ...
└── Type/ # Data types (DTO)
├── Message.php
├── Update.php
├── Chat.php
├── User.php
└── ...
Design Principles
1. One Bot class - one entry point
$bot = new Bot('TOKEN'); $bot->sendMessage(chatId: 123, text: 'Hello');
2. Named arguments everywhere
// Named arguments instead of parameter arrays $bot->sendMessage( chatId: $chatId, text: 'Hello', parseMode: 'HTML', disableNotification: true, );
3. Global helper for scripts
bot()->sendMessage(chatId: 123, text: 'Quick message');
4. Types are simple readonly classes
readonly class Message { public function __construct( public int $messageId, public ?User $from, public Chat $chat, public int $date, public ?string $text, // ... ) {} }
5. API methods as traits
Each method in a separate trait, Bot uses them:
class Bot { use SendMessageTrait; use SendPhotoTrait; use GetUpdatesTrait; // ... }
6. Method chaining for convenience
bot() ->sendMessage(chatId: 123, text: 'First') ->sendMessage(chatId: 123, text: 'Second');
Anti-patterns
- Using global application state (variables, functions, constants)
- Syntactic sugar (various helpers that help write less code)
makhnanov/php-telegram-bot 适用场景与选型建议
makhnanov/php-telegram-bot 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 63 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 03 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 makhnanov/php-telegram-bot 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 makhnanov/php-telegram-bot 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 63
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-04