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.
Ensure you have Composer installed and a bot token from BotFather.
-
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.
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',
]
)
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.
Documentation in progress – stay tuned!
Documentation in progress – stay tuned!