PHP code example of sumrak / telegram-bot

1. Go to this page and download the library: Download sumrak/telegram-bot library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sumrak / telegram-bot example snippets


$botContext = new \Sumrak\TelegramBot\TelegramBotContext('your api token here');
$bot = new \Sumrak\TelegramBot\TelegramBot($botContext);

$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;
}

$content = file_get_contents('php://input');
$serializer = new \Sumrak\TelegramBot\Serializer\TelegramApiSerializer();
$update = $serializer->deserialize(\Sumrak\TelegramBot\Entity\Update::class, $content);

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