sumrak / telegram-bot
PHP 7 Object-based Telegram Bot Library
1.0.3
2021-05-05 19:03 UTC
Requires
- php: ^7.2.0
- guzzlehttp/guzzle: ~6.0
- phpdocumentor/reflection-docblock: *
- symfony/property-access: *
- symfony/property-info: *
- symfony/serializer: *
Requires (Dev)
- phpstan/phpstan: ^0.12.34
- phpunit/phpunit: ^8.4
- squizlabs/php_codesniffer: 3.*
This package is auto-updated.
Last update: 2024-12-29 06:14:57 UTC
README
Based on objects (all data is objects).
Installation
composer require sumrak/telegram-bot
Usage:
$botContext = new \Sumrak\TelegramBot\TelegramBotContext('your api token here'); $bot = new \Sumrak\TelegramBot\TelegramBot($botContext);
Getting updates
$updates = $bot->getUpdates(); foreach ($updates as $update) { echo $update->getUpdateId(); echo ": "; if ($update->getMessage()) { echo $update->getMessage()->getChat()->getId() . ' - ' . $update->getMessage()->getText(); $lastMessageId = $update->getMessage()->getMessageId(); } else { echo 'Not Message Update'; } echo PHP_EOL; }
Get request in Webhook
$content = file_get_contents('php://input'); $serializer = new \Sumrak\TelegramBot\Serializer\TelegramApiSerializer(); $update = $serializer->deserialize(\Sumrak\TelegramBot\Entity\Update::class, $content);
Sending Messages
$replyMarkup = new \Sumrak\TelegramBot\Entity\ReplyKeyboardMarkup(); $button1 = new \Sumrak\TelegramBot\Entity\KeyboardButton(); $button1->setText('Button1'); $button2 = new \Sumrak\TelegramBot\Entity\KeyboardButton(); $button2->setText('Button2'); $replyMarkup->setKeyboard([[ $button1, $button2 ]]); $bot->sendMessage( 218081419, 'Some Text', \Sumrak\TelegramBot\TelegramBot::PARSE_MODE_MARKDOWN_V2, null, null, $lastMessageId, $replyMarkup );