PHPackages                             shokirjonmk/telegram-bot - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. shokirjonmk/telegram-bot

ActiveYii2-extension[Queues &amp; Workers](/categories/queues)

shokirjonmk/telegram-bot
========================

Professional Telegram Bot Extension for Yii2 Framework with multi-bot support, queue integration, command routing, and rate limiting

v1.0.0(5mo ago)03MITPHPPHP &gt;=7.4

Since Nov 18Pushed 5mo agoCompare

[ Source](https://github.com/ShokirjonMK/yii2-telegram-bot)[ Packagist](https://packagist.org/packages/shokirjonmk/telegram-bot)[ RSS](/packages/shokirjonmk-telegram-bot/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (2)Versions (2)Used By (0)

Telegram Bot Extension for Yii2
===============================

[](#telegram-bot-extension-for-yii2)

Professional Telegram Bot Extension for Yii2 Framework with advanced features: multi-bot support, queue integration, command routing, rate limiting, and more.

✨ Xususiyatlar
--------------

[](#-xususiyatlar)

- ✔ Yii2 uchun maxsus yozilgan
- ✔ **Multi-bot support** - bir nechta botlarni boshqarish (student, staff, va boshqalar)
- ✔ **Queue integration** - background message sending
- ✔ **Command Router** - Laravel-like command handling
- ✔ **Rate Limiting** - per-user va per-bot rate limiting (Redis bilan)
- ✔ **Retry mechanism** - exponential backoff bilan avtomatik retry
- ✔ **File Helper** - Telegram fayllarini yuklab olish
- ✔ **Update Handler Middleware** - update handling uchun middleware
- ✔ Exception, Logging, MarkdownV2 escape
- ✔ Inline keyboard builder
- ✔ Chat actions support

📦 O'rnatish
-----------

[](#-ornatish)

### Composer orqali

[](#composer-orqali)

```
composer require shokirjonmk/telegram-bot
```

Yoki `composer.json` ga qo'shing:

```
{
    "require": {
        "shokirjonmk/telegram-bot": "*"
    }
}
```

### Qo'shimcha paketlar (ixtiyoriy, lekin tavsiya etiladi)

[](#qoshimcha-paketlar-ixtiyoriy-lekin-tavsiya-etiladi)

Queue va rate limiting uchun:

```
composer require yiisoft/yii2-queue
composer require yiisoft/yii2-redis
```

⚙️ Konfiguratsiya
-----------------

[](#️-konfiguratsiya)

### Asosiy konfiguratsiya (config/web.php va config/console.php)

[](#asosiy-konfiguratsiya-configwebphp-va-configconsolephp)

```
'components' => [
    // Redis (rate limiting va queue uchun)
    'redis' => [
        'class' => 'yii\redis\Connection',
        'hostname' => '127.0.0.1',
        'port' => 6379,
        'database' => 0,
    ],

    // Queue (background jobs uchun)
    'queue' => [
        'class' => \yii\queue\redis\Queue::class,
        'redis' => 'redis',
        'channel' => 'queue',
    ],

    // HTTP Client
    'httpClient' => [
        'class' => \yii\httpclient\Client::class,
    ],

    // Telegram Manager (multi-bot support)
    'telegramManager' => [
        'class' => 'shokirjonmk\telegram\TelegramManager',
        'bots' => [
            'student' => [
                'token' => getenv('TELEGRAM_STUDENT_TOKEN'),
                'apiUrl' => 'https://api.telegram.org/bot',
            ],
            'staff' => [
                'token' => getenv('TELEGRAM_STAFF_TOKEN'),
                'apiUrl' => 'https://api.telegram.org/bot',
            ],
        ],
        'defaultBot' => 'student',
        'enableLogs' => true,
    ],

    // Yoki oddiy single bot (backward compatibility)
    'telegram' => [
        'class' => 'shokirjonmk\telegram\Telegram',
        'botToken' => getenv('TELEGRAM_TOKEN'),
        'apiUrl' => 'https://api.telegram.org/bot',
        'timeout' => 5,
        'enableLogs' => true,
    ],
]
```

### Queue worker ishga tushirish

[](#queue-worker-ishga-tushirish)

```
php yii queue/listen
# yoki
php yii queue/run
```

### Lokal polling ishga tushirish (Development)

[](#lokal-polling-ishga-tushirish-development)

```
# Console command orqali
php yii telegram-polling/polling --bot=student --timeout=30

# Yoki standalone script
php examples/polling.php --bot=student --timeout=30
```

🚀 Ishlatish
-----------

[](#-ishlatish)

### 1. Oddiy xabar yuborish (eski usul - backward compatible)

[](#1-oddiy-xabar-yuborish-eski-usul---backward-compatible)

```
use shokirjonmk\telegram\Telegram;

Yii::$app->telegram->sendMessage(
    $chatId,
    Telegram::escape("Salom, aka!")
);
```

### 2. Multi-bot support (yangi usul)

[](#2-multi-bot-support-yangi-usul)

```
// Student bot
$studentBot = Yii::$app->telegramManager->get('student');
$studentBot->sendMessage($chatId, "Student botdan xabar");

// Staff bot
$staffBot = Yii::$app->telegramManager->get('staff');
$staffBot->sendMessage($chatId, "Staff botdan xabar");
```

### 3. Options bilan xabar yuborish

[](#3-options-bilan-xabar-yuborish)

```
use shokirjonmk\telegram\TelegramComponent;
use shokirjonmk\telegram\Keyboard;

$tg = Yii::$app->telegramManager->get('student');

$kb = Keyboard::inline([
    [
        Keyboard::inlineButton("📊 Statistika", "stats"),
        Keyboard::inlineButton("📅 Darslar", "schedule")
    ]
]);

$tg->sendMessage($chatId, "Menyuni tanlang:", [
    'keyboard' => $kb,
    'parse_mode' => 'MarkdownV2',
    'disable_web_page_preview' => true,
]);
```

### 4. Queue orqali background xabar yuborish

[](#4-queue-orqali-background-xabar-yuborish)

```
$tg = Yii::$app->telegramManager->get('student');
$tg->enqueueSendMessage(
    $chatId,
    TelegramComponent::escapeMarkdownV2("Background xabar!")
);
```

### 5. Chat action yuborish

[](#5-chat-action-yuborish)

```
$tg = Yii::$app->telegramManager->get('student');
$tg->sendChatAction($chatId, 'typing'); // typing, upload_photo, record_voice, etc.
```

### 6. Fayl yuklab olish

[](#6-fayl-yuklab-olish)

```
use shokirjonmk\telegram\FileHelper;

$tg = Yii::$app->telegramManager->get('student');
$localPath = FileHelper::downloadFile($tg, $fileId, '@runtime/tg_files');
```

### 7. Command Router ishlatish

[](#7-command-router-ishlatish)

```
use shokirjonmk\telegram\CommandRouter;
use shokirjonmk\telegram\TelegramComponent;
use shokirjonmk\telegram\commands\StartCommand;

$router = new CommandRouter();

// Closure bilan
$router->register('/start', function($message, $tg) {
    $chatId = $message['chat']['id'];
    $tg->sendMessage($chatId, TelegramComponent::escapeMarkdownV2("Welcome, aka!"));
});

// Class bilan
$router->register('/start', [new StartCommand(), 'handle']);

// Default command
$router->register('/default', function($message, $tg) {
    $tg->sendMessage($message['chat']['id'], "Type /start or /help");
});

// Callback query
$router->register('stats', function($callback, $tg) {
    $chatId = $callback['message']['chat']['id'];
    $messageId = $callback['message']['message_id'];
    $tg->editMessageText($chatId, $messageId, "📊 Statistika:");
    $tg->answerCallbackQuery($callback['id'], "Statistika ko'rsatildi");
});
```

### 8. Update Handler Middleware bilan to'liq webhook

[](#8-update-handler-middleware-bilan-toliq-webhook)

```
use shokirjonmk\telegram\CommandRouter;
use shokirjonmk\telegram\UpdateHandlerMiddleware;
use shokirjonmk\telegram\RateLimiter;
use shokirjonmk\telegram\TelegramComponent;

public function actionWebhook()
{
    $body = file_get_contents('php://input');
    $update = json_decode($body, true);

    if (!$update) {
        return 'ok';
    }

    $botName = Yii::$app->request->get('bot', 'student');
    $manager = Yii::$app->telegramManager;
    $component = $manager->get($botName);

    // Command Router
    $router = new CommandRouter();
    $router->register('/start', function($msg, $tg) {
        $tg->sendMessage($msg['chat']['id'], TelegramComponent::escapeMarkdownV2("Welcome, aka!"));
    });
    $router->register('/default', function($msg, $tg) {
        $tg->sendMessage($msg['chat']['id'], "Unknown command");
    });

    // Rate Limiter (6 requests per second per user)
    $rateLimiter = new RateLimiter(6, 1);

    // Middleware
    $handler = new UpdateHandlerMiddleware($router, $rateLimiter);
    $handler->handle($update, $component);

    return 'ok';
}
```

📚 API Metodlari
---------------

[](#-api-metodlari)

### TelegramComponent

[](#telegramcomponent)

#### Xabar metodlari

[](#xabar-metodlari)

- `sendMessage($chatId, $text, $options = [])` - Xabar yuborish
- `editMessageText($chatId, $messageId, $text, $options = [])` - Xabarni o'zgartirish
- `editMessageReplyMarkup($chatId, $messageId, $keyboard = null)` - Xabar keyboardini o'zgartirish
- `deleteMessage($chatId, $messageId)` - Xabarni o'chirish
- `forwardMessage($chatId, $fromChatId, $messageId, $options = [])` - Xabarni forward qilish

#### Media metodlari

[](#media-metodlari)

- `sendPhoto($chatId, $photo, $caption = null, $keyboard = null)` - Rasm yuborish
- `sendDocument($chatId, $document, $caption = null, $keyboard = null, $options = [])` - Hujjat yuborish
- `sendVideo($chatId, $video, $caption = null, $keyboard = null, $options = [])` - Video yuborish
- `sendLocation($chatId, $latitude, $longitude, $keyboard = null, $options = [])` - Lokatsiya yuborish

#### Callback va Actions

[](#callback-va-actions)

- `answerCallbackQuery($callbackQueryId, $text = null, $showAlert = false)` - Callback javob berish
- `sendChatAction($chatId, $action = 'typing')` - Chat action yuborish (typing, upload\_photo, record\_voice, etc.)

#### Webhook va Updates

[](#webhook-va-updates)

- `setWebhook($url, $options = [])` - Webhook o'rnatish
- `deleteWebhook($dropPendingUpdates = false)` - Webhook o'chirish
- `getWebhookInfo()` - Webhook ma'lumotlarini olish
- `getUpdates($offset = null, $limit = null, $timeout = null, $allowedUpdates = null)` - Updates olish (polling)

#### Fayl va Bot ma'lumotlari

[](#fayl-va-bot-malumotlari)

- `getFile($fileId)` - Fayl ma'lumotlarini olish
- `getMe()` - Bot ma'lumotlarini olish

#### Queue va Utility

[](#queue-va-utility)

- `enqueueSendMessage($chatId, $text, $options = [])` - Queue orqali xabar yuborish
- `escapeMarkdownV2($text)` - MarkdownV2 uchun matnni escape qilish
- `escape($text)` - Alias for escapeMarkdownV2

### TelegramManager

[](#telegrammanager)

- `get($name = null)` - Bot component olish
- `getBotNames()` - Barcha bot nomlarini olish
- `has($name)` - Bot mavjudligini tekshirish

### Keyboard

[](#keyboard)

- `Keyboard::inline($buttons)` - Inline keyboard yaratish
- `Keyboard::inlineButton($text, $callback)` - Inline button yaratish
- `Keyboard::reply($buttons, $resize = true)` - Reply keyboard yaratish

### FileHelper

[](#filehelper)

- `FileHelper::downloadFile($component, $fileId, $saveDir)` - Fayl yuklab olish

### CommandRouter

[](#commandrouter)

- `register($name, $callable)` - Command ro'yxatdan o'tkazish
- `handleUpdate($update, $component)` - Update ni handle qilish

### RateLimiter

[](#ratelimiter)

- `allow($userId)` - Rate limit tekshirish

🎛 Konfiguratsiya parametrlari
-----------------------------

[](#-konfiguratsiya-parametrlari)

### TelegramComponent

[](#telegramcomponent-1)

- `token` - Bot token
- `apiUrl` - Telegram API URL (default: `https://api.telegram.org/bot`)
- `timeout` - Request timeout (default: 5)
- `enableLogs` - Logging yoqish/yoqmaslik (default: true)
- `rateLimitPerSecond` - Per-bot rate limit (default: 20)

### RateLimiter

[](#ratelimiter-1)

- `limit` - Requests per window (default: 5)
- `window` - Time window in seconds (default: 1)

📝 Webhook Controller namunasi
-----------------------------

[](#-webhook-controller-namunasi)

`controllers/TelegramController.php`:

```
