rubikalib/rubikalib

A library for working with rubika API from PHP source code

v2.3.5 2025-01-06 11:42 UTC

This package is auto-updated.

Last update: 2025-01-06 11:43:33 UTC


README

A library for working with rubika API from PHP source. use this client to make bots, games and ...

Usage

composer require rubikalib/rubikalib
  1. create a new php file in current directory

  2. require vendor and Main class in file

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

use RubikaLib\Main;
  1. now you can send messages
$bot = new Main(9123456789);
$bot->Messages->sendMessage('u0FFeu...', 'سلام');

Get Updates From API

for getting updates, you must create new class with a name and call it

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";
}

update example:

{
    "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..."
}

Methods

Use As Shad

you can use library for 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!**'
);

Example

here as base of one bot you can run

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();

see more about methods result: here

Error Handling

we wrote an Exceptions class called Failure that specialy used for library errors. here is an example of some futures:

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;
    }
}

Library Settings

we maked an settings class that you can set allowed parameters in and pass it to Main class here is an example:

use RubikaLib\Interfaces\MainSettings;

$settings = new MainSettings();
$settings->userAgent = ...;
$settings->tmp_session = ...;

$app = new Main(9123456789, $settings);

Note ! : you can chain setting parameters by this pattern:

// ...

(new MainSettings())->
    setUserAgent('Chrome ...')->
    setAuth('a829skm32knk...');

// ...

parameters:

Links

rubika ✅

telegram ✅

documentation🔰