The package provides a simple and convenient way to interact with the Telegram Bot API.
Telegram Bot API 7.7 (July 7, 2024) is full supported.
- PHP 8.2 or higher.
The package could be installed with Composer:
composer require vjik/telegram-bot-apiTo make requests to the Telegram Bot API, you need to create an instance of the TelegramBotApi class
that requires an instance of the TelegramClientInterface implementation. Out of the box, the package provides PsrTelegramClient based on the PSR-18 HTTP client
and PSR-17 HTTP factories.
For example, you can use the php-http/curl-client and httpsoft/http-message:
composer require php-http/curl-client httpsoft/http-messageIn this case, TelegramBotApi instance will be created as follows:
use Http\Client\Curl\Client;
use HttpSoft\Message\RequestFactory;
use HttpSoft\Message\ResponseFactory;
use HttpSoft\Message\StreamFactory;
use Vjik\TelegramBot\Api\Client\PsrTelegramClient;
use Vjik\TelegramBot\Api\TelegramBotApi;
// Telegram bot authentication token
$token = '110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw';
// Dependencies
$streamFactory = new StreamFactory();
$responseFactory = new ResponseFactory();
$requestFactory = new RequestFactory();
$client = new Client($responseFactory, $streamFactory);
// API
$api = new TelegramBotApi(
new PsrTelegramClient(
$token,
$client,
$requestFactory,
$streamFactory,
),
);Now you can use the $api instance to interact with the Telegram Bot API. Method names are the same as in the Telegram Bot API documentation. For example:
use Vjik\TelegramBot\Api\Type\InputFile
// Specify a URL for outgoing webhook
$api->setWebhook('https://example.com/webhook');
// Send text message
$api->sendMessage(
chatId: 22351,
text: 'Hello, world!',
);
// Send local photo
$api->sendPhoto(
chatId: 22351,
photo: InputFile::fromLocalFile('/path/to/photo.jpg'),
);The result will be either FailResult instance (on error) or an object of the corresponding type (on success). For example:
// Result is an array of `Vjik\TelegramBot\Api\Update\Update` objects
$updates = $api->getUpdates();You can make custom requests using the send() method and TelegramRequest object:
use Vjik\TelegramBot\Api\Request\TelegramRequest;
$request = new TelegramRequest(
httpMethod: HttpMethod::GET,
apiMethod: 'getChat',
data: ['chat_id' => '@sergei_predvoditelev'],
resultType: ChatFullInfo::class,
);
// Result is an object of `Vjik\TelegramBot\Api\Type\ChatFullInfo`
$result = $api->send($request);You can create an Update object from the incoming webhook PSR-7 request:
use Vjik\TelegramBot\Api\Type\Update\Update;
try {
$update = Update::fromServerRequest($request);
} catch (TelegramParseResultException $e) {
// ...
}or from JSON string received from POST request body:
use Vjik\TelegramBot\Api\Type\Update\Update;
try {
$update = Update::fromJson($jsonString);
} catch (TelegramParseResultException $e) {
// ...
}If you have any questions or problems with the package, use author telegram chat for communication.
The vjik/telegram-bot-api is free software. It is released under the terms of the BSD License.
Please see LICENSE for more information.
The package is inspired by Botasis code originally created by Viktor Babanov.

