PHP code example of amirrh6 / litegram

1. Go to this page and download the library: Download amirrh6/litegram 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/ */

    

amirrh6 / litegram example snippets




// --- --- --- --- --- --- ---

$token = '0123456789:...';
$some_chat_id = '-100...';

// Options for Guzzle (https://docs.guzzlephp.org/en/stable/request-options.html)
$global_guzzle_options = [
    'timeout' => 10,
    // 'proxy' => 'http://localhost:8118',
];

// --- --- --- --- --- --- ---

use Litegram\SendMessageParams;
use Litegram\InlineKeyboardMarkup;
use Litegram\InlineKeyboardButton;
use Litegram\TelegramMethods;

try {
    // If the request doesn't fail, an object of type Litegram\Message will be returned
    $res = TelegramMethods::sendMessage(
        token: $token,
        params: SendMessageParams::build(
            chat_id: $some_chat_id,
            text: 'Test',
            reply_markup: InlineKeyboardMarkup::build([
                [
                    InlineKeyboardButton::build('Hi', callback_data: 'say_hi'),
                    InlineKeyboardButton::build('Bye', callback_data: 'say_bye'),
                ],
                [InlineKeyboardButton::build('Close', callback_data: 'close')],
            ]),
        ),
        // If you don't pass a guzzle_options array to each method or pass an empty one (default parameter), Litegram will check for existence of a global variable
        // named 'global_guzzle_options' and use it instead, if it exists.
        guzzle_options: [
            'timeout' => 5,
            // 'proxy' => 'http://localhost:8118',
        ]
    );
    var_dump('Result:', $res);

} catch (\Throwable $th) {
    var_dump('Exception:', $th);
}

static function sendMessage(
    string $token,
    SendMessageParams $params,
    $guzzle_options = []
): Message