griffin0t03h / telegram-bot-sdk
The Unofficial PHP SDK for the Telegram Bot API, designed to simplify the process of building and interacting with Telegram bots.
Requires
- php: >=8.2
- ext-curl: *
- ext-json: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ~3.62
- guzzlehttp/guzzle: ^7.9
- php-http/multipart-stream-builder: ^1.3
- psr/http-client: ^1.0
- psr/http-factory: ^1.1
- symfony/http-client: ^7.1
- symfony/mime: ^7.1
- symfony/phpunit-bridge: *
- vimeo/psalm: ^5.25
Suggests
- guzzlehttp/guzzle: To use guzzlehttp/guzzle psr implementation
- php-http/multipart-stream-builder: To use psr/http-client
- psr/http-client: To use psr/http-client
- psr/http-factory: To use psr/http-client
- symfony/http-client: To use symfony/http-client
- symfony/mime: To use symfony/http-client
This package is auto-updated.
Last update: 2024-11-19 11:14:44 UTC
README
An extended native php wrapper for Telegram Bot API without requirements. Supports all methods and types of responses.
Bots: An introduction for developers
Bots are special Telegram accounts designed to handle messages automatically. Users can interact with bots by sending them command messages in private or group chats.
You control your bots using HTTPS requests to bot API.
The Bot API is an HTTP-based interface created for developers keen on building bots for Telegram. To learn how to create and set up a bot, please consult Introduction to Bots and Bot FAQ.
Installation
Via Composer
$ composer require telegram-bot-sdk/api
API Wrapper
Send message
$bot = new \TelegramBotSDK\Api\BotApi('YOUR_BOT_API_TOKEN'); $bot->sendMessage($chatId, $messageText);
Send document
$bot = new \TelegramBotSDK\Api\BotApi('YOUR_BOT_API_TOKEN'); $document = new \CURLFile('document.txt'); $bot->getContentService()->sendDocument($chatId, $document);
Send message with reply keyboard
$bot = new \TelegramBotSDK\Api\BotApi('YOUR_BOT_API_TOKEN'); $keyboard = new \TelegramBotSDK\Types\ReplyKeyboardMarkup(array(array("one", "two", "three")), true); // true for one-time keyboard $bot->sendMessage($chatId, $messageText, replyMarkup: $keyboard);
Send message with inline keyboard
$bot = new \TelegramBotSDK\Api\BotApi('YOUR_BOT_API_TOKEN'); $keyboard = new \TelegramBotSDK\Types\Inline\InlineKeyboardMarkup( [ [ ['text' => 'link', 'url' => 'https://core.telegram.org'] ] ] ); $bot->sendMessage($chatId, $messageText, replyMarkup: $keyboard);
Send media group
$bot = new \TelegramBotSDK\Api\BotApi('YOUR_BOT_API_TOKEN'); $media = new \TelegramBotSDK\Types\InputMedia\ArrayOfInputMedia(); $media->addItem(new TelegramBotSDK\Types\InputMedia\InputMediaPhoto('photo_url')); $media->addItem(new TelegramBotSDK\Types\InputMedia\InputMediaPhoto('photo_url')); // Same for video // $media->addItem(new TelegramBotSDK\Types\InputMedia\InputMediaVideo('video_url')); $bot->getContentService()->sendMediaGroup($chatId, $media);
Client
require_once "vendor/autoload.php"; try { $bot = new \TelegramBotSDK\Client('YOUR_BOT_API_TOKEN'); //Handle /ping command $bot->command('ping', function ($message) use ($bot) { $bot->sendMessage($message->getChat()->getId(), 'pong!'); }); //Handle text messages $bot->on(function (\TelegramBotSDK\Types\Update $update) use ($bot) { $message = $update->getMessage(); $id = $message->getChat()->getId(); $bot->sendMessage($id, 'Your message: ' . $message->getText()); }, function () { return true; }); $bot->run(); } catch (\TelegramBotSDK\Exception $e) { $e->getMessage(); }
Local Bot API Server
For using custom local bot API server
use TelegramBotSDK\Client; $token = 'YOUR_BOT_API_TOKEN'; $bot = new Client($token);
Third-party Http Client
use TelegramBotSDK\Client; use TelegramBotSDK\Http\SymfonyHttpClient; use Symfony\Component\HttpClient\HttpClient; $token = 'YOUR_BOT_API_TOKEN'; $bot = new Client($token, new SymfonyHttpClient(HttpClient::create()));
Change log
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test
Third-Party Licenses
This project also includes code from the following third-party projects:
- Library Name: Licensed under the Library License.