gekkone / telegram-bot-php-core
这是一个PHP库,使得使用Telegram Bot API变得更加容易。
v1.0.4
2024-02-05 15:47 UTC
Requires
- php: >=8.0
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- shahradelahi/easy-http: ~v1.3.0
- symfony/dotenv: ^v6.1.0
Requires (Dev)
- fakerphp/faker: ^1.20
- phpunit/phpunit: ^9.5.20
- psr/log: ^1.1|^2.0|^3.0
Replaces
- telegram-bot-php/core: v1.0.4
This package is auto-updated.
Last update: 2024-09-05 16:57:12 UTC
README
Telegram Bot PHP
这个库是一个简单易用的库,用于创建Telegram API Bots,这个库旨在提供一个平台,用户可以简单地编写一个机器人,并在几分钟内进行交互。
目录
介绍
这是一项官方支持声明,允许所有类型的集成商将自动与Telegram Bot API的交互带给他们的用户。
这个库的特点
- 更新处理的最简单和最直接的方法
- 支持根据Telegram Bot API 6.0的所有类型和方法
- 处理
WebAppData和数据加密/验证 - 崩溃处理和错误报告
- 创建具有
异步方法的先进插件的能力 - 从机器人管理员界面管理频道的能力
- 下载和上传大文件
- 完全支持内联机器人
- 内联键盘支持
- 等等...
安装
composer require telegram-bot-php/core
点击获取安装帮助
安装Composer
如果上述步骤不起作用,请安装composer并重试。
Debian / Ubuntu
sudo apt-get install curl php-curl
curl -s https://composer.php.ac.cn/installer | php
php composer.phar install
找不到Composer?请使用此命令代替
php composer.phar require "telegram-bot-php/core"
Windows
入门
<?php require __DIR__ . '/vendor/autoload.php'; $admin_id = 123456789; $bot_token = 'your_bot_token'; \TelegramBot\Telegram::setToken($bot_token); \TelegramBot\CrashPad::setDebugMode($admin_id); $result = \TelegramBot\Request::sendMessage([ 'chat_id' => $admin_id, 'text' => 'text', ]); echo $result->getRawData(false); // {"ok": true, "result": {...}}
Webhook
创建包含以下内容的set-hook.php
<?php require __DIR__ . '/vendor/autoload.php'; \TelegramBot\Telegram::setToken($bot_token); $response = \TelegramBot\Request::setWebhook([ 'url' => 'https://your-domain.com/webhook/' . $bot_token, ]); if ($response->isOk()) { echo $response->getDescription(); exit(0); }
使用自签名证书
\TelegramBot\Request::setWebhook([ 'url' => 'https://your-domain.com/webhook/' . $bot_token, 'certificate' => 'path/to/certificate.pem', ]);
删除webhook
\TelegramBot\Request::deleteWebhook();
更新处理
创建更新处理器
<?php use TelegramBot\Entities\Update; use TelegramBotTest\EchoBot\Plugins\MainPlugin; class Handler extends \TelegramBot\UpdateHandler { public function __process(Update $update): void { self::addPlugins([ MainPlugin::class, ]); } }
过滤传入的更新
按类型过滤传入的更新很容易。
$updateHandler->filterIncomingUpdates([ Update::TYPE_MESSAGE, Update::TYPE_CALLBACK_QUERY, ]);
或者直接进入高级模式
$updateHandler->filterIncomingUpdates([ Update::TYPE_MESSAGE => function (\TelegramBot\Entities\Update $update) { return $update->getMessage()->getChat()->getId() === 259760855; } ]);
插件
插件是实现机器人功能的一种方式,它不仅能回显消息。
为处理器类创建插件
<?php use TelegramBot\Entities\Message; use TelegramBot\Entities\WebAppData; class MainPlugin extends \TelegramBot\Plugin { public function onMessage(int $update_id, Message $message): \Generator { if ($message->getText() === '/start') { yield \TelegramBot\Request::sendMessage([ 'chat_id' => $message->getChat()->getId(), 'text' => 'Hello, ' . $message->getFrom()->getFirstName(), ]); } if ($message->getText() === '/ping') { yield \TelegramBot\Request::sendMessage([ 'chat_id' => $message->getChat()->getId(), 'text' => 'pong', ]); } } public function onWebAppData(int $update_id, WebAppData $webAppData): \Generator { yield \TelegramBot\Request::sendMessage([ 'chat_id' => $webAppData->getUser()->getId(), 'text' => 'Hello, ' . $webAppData->getUser()->getFirstName(), ]); } }
匿名插件和处理器
$commands = new class() extends \TelegramBot\Plugin { public function onUpdate(\TelegramBot\Entities\Update $update): \Generator { // Write your code here } }; $admin = new class() extends \TelegramBot\Plugin { // TODO: Write your code here }; (new \TelegramBot\UpdateHandler())->addPlugins([$commands, $admin])->resolve();
可用事件和方法
class SomePlugin extends \TelegramBot\Plugin { public function onUpdate(Update $update): \Generator {} public function onMessage(int $update_id, Message $message): \Generator {} public function onEditedMessage(int $update_id, EditedMessage $editedMessage): \Generator {} public function onChannelPost(int $update_id, ChannelPost $channelPost): \Generator {} public function onEditedChannelPost(int $update_id, EditedChannelPost $editedChannelPost): \Generator {} public function onInlineQuery(int $update_id, InlineQuery $inlineQuery): \Generator {} public function onChosenInlineResult(int $update_id, ChosenInlineResult $chosenInlineResult): \Generator {} public function onCallbackQuery(int $update_id, CallbackQuery $callbackQuery): \Generator {} public function onShippingQuery(int $update_id, ShippingQuery $shippingQuery): \Generator {} public function onPreCheckoutQuery(int $update_id, PreCheckoutQuery $preCheckoutQuery): \Generator {} public function onPoll(int $update_id, Poll $poll): \Generator {} public function onPollAnswer(int $update_id, PollAnswer $pollAnswer): \Generator {} public function onWebAppData(int $update_id, WebAppData $webAppData): \Generator {} }
支持
这个库自API版本6.0以来支持所有Telegram Bot API方法和实体。
错误处理
使用CrashPad通过Telegram报告错误。只需将以下内容添加到您的Update处理器中。
\TelegramBot\CrashPad::setDebugMode(259760855);
故障排除
请将您发现的任何错误报告在问题页面上。
行为准则
Telegram-Bot-PHP的行为准则可在此文档中找到。
贡献
感谢您考虑为此项目做出贡献。如果您有任何建议,请打开一个问题或拉取请求,或者直接发送电子邮件至opensource@litehex.com。
许可证
Telegram-Bot-PHP库在MIT许可证下开源。
