An open API service indexing awesome lists of open source software.

https://github.com/mostafabaghi/telegram-bot

A full-featured PHP class for interacting with Telegram Bot API
https://github.com/mostafabaghi/telegram-bot

api bot php telegram telegram-bot-api telegrambot

Last synced: 7 months ago
JSON representation

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

Awesome Lists containing this project

README

          

# Telegram Bot Plus

![Telegram Bot Api PHP](screenshot.png)

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/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](https://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

```bash
composer require mostafabaghi/telegram-bot
```

> Or manually clone:
```bash
git clone https://github.com/mostafabaghi/telegram-bot.git
cd telegram-bot
composer install
```

---

## ๐Ÿ”ง Usage Example

### Basic usage:

```php
use TelegramBot\TelegramBotPlus;

$bot = new TelegramBotPlus('YOUR_BOT_TOKEN');
$bot->sendMessage(123456789, 'Hello world!');
```

### Webhook handling:

```php
$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

```php
$bot->setWebhook('https://yourdomain.com/webhook.php');
$bot->deleteWebhook();
$bot->getWebhookInfo();
```

### ๐Ÿ’ก Event System

```php
$bot->on('message.text', function($message) use ($bot) {
$bot->sendMessage($message['chat']['id'], 'Handled via event!');
});
```

### ๐Ÿ” IP Validation

```php
if (!$bot->isFromTelegram()) {
http_response_code(403);
exit('Access Denied');
}
```

---

## ๐Ÿงช Testing

```bash
composer install
vendor/bin/phpunit
```

---

## ๐Ÿ“œ License

MIT License ยฉ [mostafabaghi](https://github.com/mostafabaghi)