PHP code example of laraxgram / laraquest

1. Go to this page and download the library: Download laraxgram/laraquest library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

laraxgram / laraquest example snippets


$_ENV["BOT_TOKEN"] = "123456789:ABcdEfGHigKLMnopQrStuvWXYZ"; // lt-optional

// Polling
$_ENV["sleep_interval"] = 0.5; // default-optional
$_ENV["timeout"] = 100; // default-optional
$_ENV["limit"] = 100; // default-optional
$_ENV["allow_updates"] = ["*"]; // default-optional

use LaraGram\Laraquest\Methode;

class MyBotClass {
    use Methode;
}

$bot = new MyBotClass();
$bot->sendMessage(123456789, 'hello!');

use LaraGram\Laraquest\Updates;

class MyBotClass {
    use Updates;
}

$bot = new MyBotClass();
$chatID = $bot->message->chat->id;

use LaraGram\Laraquest\Methode;
use LaraGram\Laraquest\Updates;

class MyBotClass {
    use Methode, Updates;
}

$bot = new MyBotClass();
$bot->sendMessage($bot->message->chat->id, 'hello!');

Laraquest::polling(function(Laraquest $request){
    $request->sendMessage($request->message->chat->id, "Hello, Laraquest!")
});

$response = $bot->sendMessage(123456789, 'hello!');

$response['result']['message_id'];  // the array Telegram sent
$response->message_id;              // the result's fields, as properties
$response->result();                // the Message object

$bot->getMe()->first_name;          // "LaraGram"

$response->isOk();            // bool
$response->failed();          // bool
$response->errorCode();       // int|null   (error_code() works too)
$response->description();     // string|null
$response->retryAfter();      // int|null
$response->migrateToChatId(); // int|null
$response->toArray();         // the result as an array
$response->toArray(true);     // the whole response: ok, result, description...
$response->toJson();          // the result as JSON  (toJson(true) for all of it)
$response->throw();           // raise the matching exception when it failed

use LaraGram\Laraquest\Exceptions\BotBlockedException;
use LaraGram\Laraquest\Exceptions\FloodException;
use LaraGram\Laraquest\Exceptions\TelegramApiException;

try {
    $bot->throw()->sendMessage($chatId, 'hello!');
} catch (BotBlockedException) {
    // the user blocked the bot
} catch (FloodException $e) {
    sleep($e->retryAfter());
} catch (TelegramApiException $e) {
    report($e->method(), $e->errorCode(), $e->description());
}

use LaraGram\Laraquest\Updates\InlineKeyboardButton;
use LaraGram\Laraquest\Updates\InlineKeyboardMarkup;

$bot->sendMessage(123456789, 'Pick one', reply_markup: InlineKeyboardMarkup::init(
    inline_keyboard: [[
        InlineKeyboardButton::init(text: 'Open', url: 'https://t.me/laraxgram'),
    ]],
));

use LaraGram\Laraquest\Updates\Update;

$update = Update::from($bot->getData());

$update->message->chat->id;             // objects all the way down
$update->message->entities[0]->type;    // lists too
$update->get('message.from.username');  // or a dotted path
$update->has('message.photo');
$update->message->only(['message_id', 'text'])->toArray();

$_ENV['CONNECTIONS']['first_bot']['BOT_TOKEN'] = 'XXX';
$_ENV['CONNECTIONS']['second_bot']['BOT_TOKEN'] = 'YYY';

$first_bot = $bot->connection('first_bot');
$second_bot = $bot->connection('second_bot');

$first_bot->sendMessage(...);
$second_bot->deleteMessage(...);

$first_bot->getConnection(); // first_bot