rubikalib / rubikalib
一个用于从PHP源代码操作rubika API的库
v2.0.1
2024-09-12 11:41 UTC
Requires
- php: >= 8.1.0
- ext-gd: *
- ext-json: *
- ext-mbstring: *
- ext-openssl: *
- james-heinrich/getid3: ^1.9
- paragonie/sodium_compat: ^2.0.1
- phpseclib/phpseclib: ^3.0
- ratchet/pawl: ^0.4.1
Requires (Dev)
- phpunit/phpunit: ^11.3
README
一个从PHP源代码操作rubika API的库。 使用此客户端制作机器人、游戏等...
用法
composer require rubikalib/rubikalib
-
在当前目录中创建一个新的PHP文件
-
在文件中引入vendor和主类
require_once __DIR__ . '/vendor/autoload.php'; use RubikaLib\Main;
- 现在您可以发送消息了
$bot = new Main(9123456789); $bot->Messages->SendMessage('u0FFeu...', 'سلام');
从API获取更新
要获取更新,您必须创建一个新类,并给它命名然后调用
require_once __DIR__ . '/vendor/autoload.php'; use RubikaLib\Enums\ChatActivities; use RubikaLib\Interfaces\Runner; use RubikaLib\{ Failure, Main }; try { $app = new Main(9123456789); $app->proccess( new class implements Runner { # when this class declared as update getter on Main, this method get called public function onStart(array $mySelf): void { } # All updates will pass to this method (not action updates) public function onMessage(array $updates, Main $class): void { } # All action updates (Typing, Recording, uploading) will pass to this method public function onAction(ChatActivities $activitie, string $guid, string $from, Main $class): void { } } )->RunAndLoop(); } catch (Failure $e) { echo $e->getMessage() . "\n"; }
更新示例
{ "chat_updates": [ { "object_guid": "u0HMRZI03...", "action": "Edit", "chat": { "time_string": "172329480300001076130340791385", "last_message": { "message_id": "1076130340791385", "type": "Text", "text": "hello dear", "author_object_guid": "u0HMRZI03...", "is_mine": true, "author_title": "\u0634\u0645\u0627", "author_type": "User" }, "last_seen_my_mid": "1076130340791385", "last_seen_peer_mid": "0", "status": "Active", "time": 1723294803, "last_message_id": "1076130340791385" }, "updated_parameters": [ "last_message_id", "last_message", "status", "time_string", "last_seen_my_mid", "last_seen_peer_mid", "time" ], "timestamp": "1723294804", "type": "User" } ], "message_updates": [ { "message_id": "1076130340791385", "action": "New", "message": { "message_id": "1076130340791385", "text": "hello dear", "time": "1723294803", "is_edited": false, "type": "Text", "author_type": "User", "author_object_guid": "u0HMRZI03...", "allow_transcription": false }, "updated_parameters": [], "timestamp": "1723294804", "prev_message_id": "1076130340663385", "object_guid": "u0HMRZI03...", "type": "User", "state": "1723294744" } ], "user_guid": "u0HMRZI03..." }
方法
使用示例
您可以使用库来操作Shad API。
use RubikaLib\Interfaces\MainSettings; use RubikaLib\Main; use RubikaLib\Enums\AppType; $settings = new MainSettings(); $settings->AppType = AppType::Shad; $app = new Main(9123456789, settings: $settings); $app->Messages->SendMessage( guid: $app->Account->getMySelf()['user_guid'], text: '**hello wolrd!**' );
示例
这里可以作为一个机器人的基础运行
declare(strict_types=1); require_once 'vendor/autoload.php'; use RubikaLib\Enums\ChatActivities; use RubikaLib\Interfaces\Runner; use RubikaLib\{ Failure, Main }; $app = new Main(9123456789, 'test-app'); $app->proccess( new class implements Runner { private ?array $me; private array $userData = []; public function onStart(array $mySelf): void { $this->me = $mySelf; echo "bot is running...\n"; } public function onMessage(array $updates, Main $class): void { if (isset($updates['message_updates'])) { foreach ($updates['message_updates'] as $update) { if ($update['action'] == 'New') { $guid = $update['object_guid']; # chat guid $message_id = $update['message_id']; $from = $update['message']['author_object_guid']; # message from who? $author_type = $update['message']['author_type']; # Group, Channel, User, Bot, ... $action = $update['action']; $text = $update['message']['text']; echo "new message: $from => $text\n"; if ($text == 'شروع' && $author_type == 'User' && $from != $this->me['user_guid']) { $this->setUp($from); $class->Messages->SendMessage($guid, "به منوی اصلی خوش آمدید 😎\n\nگزینه مورد نظر را ارسال کنید:\n\nراهنما 📚(5) | پشتیبانی 🆘(6)", $message_id); } } } } } public function onAction(ChatActivities $activitie, string $guid, string $from, Main $class): void { } private function setUp(string $guid) { $this->userData = [ 'step' => 'none' ]; file_put_contents("users/$guid.json", json_encode($this->userData)); } } ); $app->RunAndLoop();
了解更多关于方法结果的信息:这里
错误处理
我们编写了一个名为Failure的异常类,专门用于库错误。以下是一些示例
try { $app = new Main(9123456789); // ... $app->RunAndLoop(); } catch (Failure $error) { echo $error->getMessage() . "\n"; if ($error->obj != array()) { var_dump($error->obj); echo PHP_EOL; } }
库设置
我们创建了一个设置类,您可以设置允许的参数并传递给主类。以下是一个示例
use RubikaLib\Interfaces\MainSettings; $settings = new MainSettings(); $settings->userAgent = ...; $settings->tmp_session = ...; $app = new Main(9123456789, $settings);
注意! : 您可以通过此模式链接设置参数
// ... (new MainSettings())-> setUserAgent('Chrome ...')-> setAuth('a829skm32knk...'); // ...
参数