mostafabaghi/telegram-bot

A full-featured PHP class for interacting with Telegram Bot API

Maintainers

Package info

github.com/mostafabaghi/telegram-bot

pkg:composer/mostafabaghi/telegram-bot

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 1

Open Issues: 0

v1.0.0 2025-07-24 18:31 UTC

This package is auto-updated.

Last update: 2026-03-24 20:05:39 UTC


README

Telegram Bot Api PHP

License: MIT

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