vjik / telegram-bot-api
PHP library for working with Telegram API
Installs: 1 036
Dependents: 1
Suggesters: 0
Security: 0
Stars: 49
Watchers: 1
Forks: 3
Open Issues: 0
Requires
- php: ^8.2
- php-http/multipart-stream-builder: ^1.4.2
- psr/http-client: ^1.0
- psr/http-factory: ^1.1
- psr/http-message: ^1.1|^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- httpsoft/http-message: ^1.1
- maglnet/composer-require-checker: ^4.11
- php-http/curl-client: ^2.3
- phpunit/phpunit: ^10.5
- psr/log: ^3.0
- roave/infection-static-analysis-plugin: ^1.35
- vimeo/psalm: ^5.26
- yiisoft/test-support: ^3.0
Suggests
- psr/log: To log requests to Telegram Bot API and response handling errors.
This package is auto-updated.
Last update: 2024-11-18 11:37:37 UTC
README
The package provides a simple and convenient way to interact with the Telegram Bot API.
✔️ Telegram Bot API 8.0 (November 17, 2024) is fully supported.
Requirements
- PHP 8.2 or higher.
Installation
The package can be installed with Composer:
composer require vjik/telegram-bot-api
General usage
To 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-message
In this case, a TelegramBotApi
instance can 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 a FailResult
instance (occuring on an error) or an object of the corresponding type (occuring on success). For example:
// Result is an array of `Vjik\TelegramBot\Api\Update\Update` objects $updates = $api->getUpdates();
Documentation
If you have any questions or problems with this package, use author telegram chat for communication.
License
The vjik/telegram-bot-api
is free software. It is released under the terms of the BSD License.
Please see LICENSE
for more information.
Credits
The package is inspired by Botasis code originally created by Viktor Babanov.