sumrak/telegram-bot

PHP 7 Object-based Telegram Bot Library

1.0.3 2021-05-05 19:03 UTC

This package is auto-updated.

Last update: 2024-04-29 04:20:17 UTC


README

Latest Version Travis (.com) branch Total Downloads Coverage

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
);