amirrh6/litegram

Lightweight PHP wrapper library for Telegram Bot API

0.13.1 2025-03-02 14:27 UTC

README

Lightweight PHP wrapper library for Telegram Bot API

Bot API version: v7.11 (October 31, 2024) - Snapshot Link

PHP version: 8.3 - Unknown compatibility with earlier (8+) or later versions.

  • Minimal, Doesn't get in your way
  • Fully documented, Employs identical names for methods and classes as those found in the official API
  • Uses Guzzle as the HTTP client
  • Provides type hints for IDE autocompletion

Litegram is still in early development stages so expect bugs and non-backward compatible changes.

Use Github Issues for comments, bug reports and questions.

TODOs:

  • Full implementation of all types (classes) and methods
  • Provide built-in validations
  • Concurrent (bulk) requests: Has been experimentally implemented for some methods
  • Provide helper utilities (e.g. For formatting messages using HTML or Markdown) and builtin checks (e.g. Making sure the message text's length does not exceed 4096 chars)
  • Async requests
  • Better error handling (retry requests which failed due to network error etc ...)
  • Update to Bot API 8.0
  • Update to Bot API 8.1
  • Update to Bot API 8.2
  • Update to Bot API 8.3

Installation / Updating

composer require amirrh6/litegram dev-main

Usage and Examples

require_once './vendor/autoload.php';

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

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

This file provides usage example for some primary methods.

History

Over a year ago, during my free time between university classes, I started working on another Telegram bot. I wanted to challenge myself by creating my own wrapper library for it. Although the bot project was eventually abandoned, I decided to revive the wrapper library, and here we are today.

License:

GPL 2.0 only

Classes and methods:

* Make sure you view this section on Github rather than Packagist as it doesn't display checkmarks correctly.

Getting updates

(4/4 methods implemented, 2/2 classes implemented)

Available types

(71/150 classes implemented)

Available methods

(11/85 methods implemented)

* Every method here has a signature similar to:

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

See here for usage and examples.

* Experimental bulk (concurrent) version of this method is available. These methods are named like this: copyMessage() ---> _bulkCopyMessage()

Updating messages

(1/9 methods implemented)

Stickers

(0/16 methods implemented, 0/4 classes implemented)

Inline mode

(0/2 methods implemented, 3/29 classes implemented + 2 union types)

Payments

(0/6 methods implemented, 3/19 classes implemented + 2 union types)

Telegram Passport

(0/1 methods implemented, 0/13 classes implemented + 1 union type)

Games

(0/3 methods implemented, 0/3 classes implemented)