botasis / telegram-client
Yet another PHP Telegram bot client
Requires
- php: ^8.2
- nyholm/psr7: ^1.8
- php-http/multipart-stream-builder: ^1.3
- psr/http-client: ^1.0
- psr/http-factory: ^1.0
- psr/http-message: ^1.0||^2.0
- psr/log: ^1.0.0||^2.0.0||^3.0.0
- yiisoft/friendly-exception: ^1.1
Requires (Dev)
- maglnet/composer-require-checker: ^4.5
- phpunit/phpunit: ^10.1
- roave/infection-static-analysis-plugin: ^1.16
- symfony/console: ^6.2
- symfony/http-client: ^6.2
- vimeo/psalm: ^5.4
Suggests
- httpsoft/http-message: ^1.0.10
- symfony/http-client: ^6.0
- yiisoft/yii-console: ^1.0
README
Overview
Botasis offers a lightweight and strictly PSR-compliant implementation of the Telegram Bot API. Designed for developers seeking a robust and advanced bot SDK, Botasis simplifies the creation and management of Telegram bots.
Explore our advanced SDK at botasis/runtime and access a ready-to-deploy application template at botasis/bot-template.
Quick Start
Ensure you have Composer installed and a bot token from BotFather.
Setup
-
Install the Package:
composer require botasis/telegram-client
-
Install PSR HTTP Client and Message Factory: If you don't have preferences, use:
composer require httpsoft/http-message php-http/socket-client
-
Create an API Client Instance: Either use your DI container or manually instantiate:
$streamFactory = new \HttpSoft\Message\StreamFactory(); $client = new \Botasis\Client\Telegram\Client\ClientPsr( getenv('BOT_TOKEN'), new \Http\Client\Socket\Client(), new \HttpSoft\Message\RequestFactory(), $streamFactory, new \Http\Message\MultipartStream\MultipartStreamBuilder($streamFactory), );
-
Send a Request: Example to send a "Hello" message:
$response = $client->send(new \Botasis\Client\Telegram\Request\TelegramRequest( 'sendMessage', // any telegram bot api method you like // data to send [ 'text' => 'Hello Botasis!', 'chat_id' => $chatId, ] ));
$response
will contain the Telegram Bot API response data. Refer to the API specification for details.
Feature Highlights
Method-Specific Request Classes
Utilize strictly-typed classes for API requests. For instance:
$requestTyped = new \Botasis\Client\Telegram\Request\Message\Message( 'Hello *Botasis*\!', // be sure you correctly escape special characters when use Markdown message format \Botasis\Client\Telegram\Request\Message\MessageFormat::MARKDOWN, $chatId, ); // This is equal, but not strictly typed request: $requestGeneric = new \Botasis\Client\Telegram\Request\TelegramRequest( 'sendMessage', [ 'text' => 'Hello *Botasis*\!', 'chat_id' => $chatId, 'parse_mode' => 'MarkdownV2', ] )
Extending Functionality
Missing a method-specific request class? Extend the library:
- Contribute: Submit a PR with your implementation.
- Fallback: Use the generic
TelegramRequest
class.
This approach ensures immediate adaptability to any Telegram API updates.
Files uploading
Documentation in progress – stay tuned!
Error Handling
Documentation in progress – stay tuned!