mostafabaghi / telegram-bot
A full-featured PHP class for interacting with Telegram Bot API
v1.0.0
2025-07-24 18:31 UTC
Requires
- php: >=7.4
Requires (Dev)
- phpunit/phpunit: ^10
README
A modern, fully featured PHP wrapper for the Telegram Bot API โ built for developers who want full control and modern structure with PSR-4 compatibility.
GitHub: github.com/mostafabaghi/telegram-bot
๐ Features
- PSR-4 autoloading
- Full API support (sendMessage, sendMedia, Polls, Payments...)
- Debug logging
- Webhook handler system
- Event Dispatcher system (like Laravel listeners)
- IP validation for Telegram
- File uploads via URL, local, or stream (memory)
๐ฆ Installation
composer require mostafabaghi/telegram-bot
Or manually clone:
git clone https://github.com/mostafabaghi/telegram-bot.git
cd telegram-bot
composer install
๐ง Usage Example
Basic usage:
use TelegramBot\TelegramBotPlus; $bot = new TelegramBotPlus('YOUR_BOT_TOKEN'); $bot->sendMessage(123456789, 'Hello world!');
Webhook handling:
$bot->setWebhookHandler(function($update) use ($bot) { if (isset($update['message']['text'])) { $chatId = $update['message']['chat']['id']; $bot->sendMessage($chatId, "Received: " . $update['message']['text']); } }); $bot->handleWebhook();
๐ Documentation
โ Core Methods
| Method | Description |
|---|---|
sendMessage($chatId, $text, $options = []) |
Send text message |
sendPhoto($chatId, $photoPath, $caption = '') |
Send image |
sendMedia($type, $chatId, $media, $caption = '') |
Send media (photo, video, document, ...) |
sendMediaGroup($chatId, $mediaArray) |
Send album (array of media) |
sendPoll($chatId, $question, $options) |
Create poll |
sendInvoice(...) |
Send payment invoice |
answerCallbackQuery(...) |
Answer inline button click |
โ๏ธ Webhook Management
$bot->setWebhook('https://yourdomain.com/webhook.php'); $bot->deleteWebhook(); $bot->getWebhookInfo();
๐ก Event System
$bot->on('message.text', function($message) use ($bot) { $bot->sendMessage($message['chat']['id'], 'Handled via event!'); });
๐ IP Validation
if (!$bot->isFromTelegram()) { http_response_code(403); exit('Access Denied'); }
๐งช Testing
composer install vendor/bin/phpunit
๐ License
MIT License ยฉ mostafabaghi
