https://github.com/kuvardin/TelegramBotsApi
PHP SDK for Telegram bots API
https://github.com/kuvardin/TelegramBotsApi
Last synced: 5 months ago
JSON representation
PHP SDK for Telegram bots API
- Host: GitHub
- URL: https://github.com/kuvardin/TelegramBotsApi
- Owner: kuvardin
- License: mit
- Created: 2022-08-20T13:14:51.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2025-08-31T23:59:46.000Z (9 months ago)
- Last Synced: 2025-09-01T01:26:36.461Z (9 months ago)
- Language: PHP
- Homepage:
- Size: 419 KB
- Stars: 11
- Watchers: 1
- Forks: 7
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-opensource-projects - TelegramBotsApi - PHP SDK for building Telegram bots using the Telegram Bot API. (Project Categories)
README
# TelegramBotsApi v.8.2
[](https://github.com/kuvardin/TelegramBotsApi/releases)
[](https://packagist.org/packages/kuvardin/telegram-bots-api)
SDK for latest version of Telegram bots API (from January 1, 2025)
Author: https://t.me/kuvardin
## Using Examples
### Installing
```bash
composer require "kuvardin/telegram-bots-api: dev-master"
```
### Init bot
```php
sendMessage($chat_id, $message_text);
try {
$message = $request->sendRequest();
echo 'Successful sent';
} catch (Kuvardin\TelegramBotsApi\Exceptions\TelegramBotsApiException $e) {
echo "API error #{$e->getCode()}: {$e->getMessage()}";
} catch (GuzzleHttp\Exception\GuzzleException $e) {
echo "cURL error #{$e->getCode()}: {$e->getMessage()}";
}
```
### Sending media group
```php
sendMediaGroup(
chat_id: $chat_id,
media: [
new Kuvardin\TelegramBotsApi\Types\InputMedia\Photo(
media: $attached_files->attachByPath('photo1.jpg'),
caption: 'First photo',
),
new Kuvardin\TelegramBotsApi\Types\InputMedia\Video(
media: $attached_files->attachByPath('video1.mp4'),
caption: 'First video',
),
new Kuvardin\TelegramBotsApi\Types\InputMedia\Video(
media: $attached_files->attachByPath('video2.mp4'),
caption: 'Second video',
),
],
);
$attached_files->attachToRequest($request);
try {
$messages = $request->sendRequest();
foreach ($messages as $message) {
echo "Message #{$message->message_id} successfully sent to chat with ID {$message->chat->id}\n";
}
} catch (Kuvardin\TelegramBotsApi\Exceptions\TelegramBotsApiException $e) {
echo "API error #{$e->getCode()}: {$e->getMessage()}";
} catch (GuzzleHttp\Exception\GuzzleException $e) {
echo "cURL error #{$e->getCode()}: {$e->getMessage()}";
}
```
### Set webhooks handler
```php
setWebhook($webhooks_handler_url);
try {
$request->sendRequest();
echo 'Success';
} catch (Kuvardin\TelegramBotsApi\Exceptions\TelegramBotsApiException $e) {
echo "API error #{$e->getCode()}: {$e->getMessage()}";
} catch (GuzzleHttp\Exception\GuzzleException $e) {
echo "cURL error #{$e->getCode()}: {$e->getMessage()}";
}
```
### Receive incoming updates via an outgoing webhook
```php
getType()) {
case Kuvardin\TelegramBotsApi\Enums\UpdateType::Message:
$request = $bot->sendMessage(
chat_id: $update->message->chat->id,
text: 'Hello World',
parse_mode: Kuvardin\TelegramBotsApi\Enums\ParseMode::HTML,
reply_markup: new Kuvardin\TelegramBotsApi\Types\InlineKeyboardMarkup([
[ // Buttons row 1
new Kuvardin\TelegramBotsApi\Types\InlineKeyboardButton(
text: 'Open URL',
url: 'https://github.com/kuvardin',
),
new Kuvardin\TelegramBotsApi\Types\InlineKeyboardButton(
text: 'Send callback command',
callback_data: 'like_it',
),
],
[ // Buttons row 2
// ...
],
[ // Buttons row 3
// ...
],
// ...
]),
);
break;
case Kuvardin\TelegramBotsApi\Enums\UpdateType::EditedMessage:
// ...
break;
case Kuvardin\TelegramBotsApi\Enums\UpdateType::ChannelPost:
// ...
break;
case Kuvardin\TelegramBotsApi\Enums\UpdateType::EditedChannelPost:
// ...
break;
case Kuvardin\TelegramBotsApi\Enums\UpdateType::InlineQuery:
// ...
break;
case Kuvardin\TelegramBotsApi\Enums\UpdateType::ChosenInlineResult:
// ...
break;
case Kuvardin\TelegramBotsApi\Enums\UpdateType::CallbackQuery:
// ...
break;
case Kuvardin\TelegramBotsApi\Enums\UpdateType::ShippingQuery:
// ...
break;
// ...
}
if ($request !== null) {
header('Content-Type: application/json');
echo json_encode($request->getRequestData(), JSON_THROW_ON_ERROR);
}
```