xbot-my/telegram-sdk
最新稳定版本:1.0.2
Composer 安装命令:
composer require xbot-my/telegram-sdk
包简介
Telegram Bot PHP SDK
README 文档
README
一个易于使用的 PHP Telegram Bot API SDK,提供高性能客户端、语义化端点和完善的异常处理机制,并支持 Laravel 集成。
特性
- 开箱即用
- 异常捕获
- 链式调用
- 响应转换
安装
composer require xbot-my/telegram-sdk
Laravel
php artisan vendor:publish --provider="XBot\\Telegram\\Providers\\TelegramServiceProvider"
快速开始
use XBot\\Telegram\\Bot; $bot = Bot::token('YOUR_BOT_TOKEN'); $me = $bot->getMe()->toArray(); $msg = $bot->sendMessage(123456789, 'Hello')->toArray(); $bot->setWebhook('https://example.com/telegram/webhook');
Webhook 与更新处理
- 在
.env设置TELEGRAM_WEBHOOK_SECRET,可选TELEGRAM_WEBHOOK_ROUTE_PREFIX。 - ServiceProvider 注册默认路由与中间件,校验请求头
X-Telegram-Bot-Api-Secret-Token。 - 实现
UpdateHandler或继承BaseUpdateHandler:
class StartHandler extends BaseUpdateHandler { protected function onMessage(array $u): void { if ($this->text($u) === '/start') $this->replyText($u, 'Welcome!'); } }
- 命令路由可继承
CommandRouter,如/start→onStart,/help foo→onHelp。
示例
// WebApp $bot->answerWebAppQuery($queryId, [...]); // Boosts $bot->getUserChatBoosts($chatId, $userId); // Stars $bot->refundStarPayment($userId, $chargeId); // Business $bot->readBusinessMessage($chatId, $messageId);
Telegram Bot API 9.2 新参数
direct_messages_topic_id:发送至频道话题suggested_post_parameters:建议帖子reply_parameters.checklist_task_id:回复清单任务
$bot->sendMessage($chatId, 'Hello', ['direct_messages_topic_id' => 1234]);
日志
-
环境变量控制:
TELEGRAM_LOG_ENABLEDTELEGRAM_LOG_SUPPRESS_INFOTELEGRAM_LOG_CHANNEL
-
事件:
telegram.request、telegram.response、telegram.retry等
统计信息
- 总下载量: 17
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-28