PHP code example of grobolof / bot-payload

1. Go to this page and download the library: Download grobolof/bot-payload 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/ */

    

grobolof / bot-payload example snippets


use BotPayload\Telegram\Mapper\TelegramMapper;

$update = TelegramMapper::exec($requestJson); // array|string

$update->getUpdateId();          // 123456
$update->isMessage();             // true
$update->getChatId();            // 111
$update->getFrom()?->getId();    // 111
$update->getFrom()?->getUsername();// ivan
$update->getText();               // /start payload
$update->getMessage()?->getBotCommand(); // start

$update->getMessage()?->getContact()?->getPhoneNumber(); // +79990001122

$update->isCallbackQuery();                         // true
$update->getCallbackData();                         // menu:open
$update->getCallbackQuery()?->getId();              // cb-1  → answerCallbackQuery
$update->getChatId();                               // 222

use BotPayload\Telegram\Message\Enum\Marker;
use BotPayload\Telegram\Message\Enum\Mod;
use BotPayload\Telegram\ReplyMarkup\Model\Button;
use BotPayload\Telegram\ReplyMarkup\Model\Keyboard\KeyboardInline;
use BotPayload\Telegram\ReplyMarkup\Model\ReplyMarkup;
use BotPayload\Telegram\TelegramFormatter;

$payload = TelegramFormatter::sendMessage(
    chatId: $update->getChatId(),
    text: Marker::MARKER_FONT_BOLD_OPEN->value
        . 'Привет'
        . Marker::MARKER_FONT_BOLD_CLOSE->value
        . '. Это бот.',
    parseMode: Mod::MARKDOWN_V2,
    replyMarkup: new ReplyMarkup(
        keyboard: new KeyboardInline(buttonsPerRow: 2),
        buttons: [
            new Button(name: 'Меню', callbackData: 'menu:open'),
            new Button(name: 'Сайт', url: 'https://example.com'),
        ],
    ),
);
// [
//   'chat_id' => 111,
//   'text' => '*Привет*\\. Это бот\\.',
//   'parse_mode' => 'MarkdownV2',
//   'reply_markup' => ['inline_keyboard' => [[...], ...]],
// ]

TelegramFormatter::message(Mod::MARKDOWN_V2, $text);

use BotPayload\Telegram\ReplyMarkup\Model\Keyboard\Keyboard;

new ReplyMarkup(
    keyboard: new Keyboard(buttonsPerRow: 2, resizeKeyboard: true, oneTimeKeyboard: true),
    buttons: [
        new Button(name: 'Да'),
        new Button(name: 'Телефон', requestContact: true),
        new Button(name: 'Гео', requestLocation: true),
    ],
);

new ReplyMarkup(
    keyboard: new KeyboardInline(),
    buttons: [
        new Button(name: 'Открыть', row: 1, callbackData: 'open'),
        new Button(name: 'Сайт', row: 2, url: 'https://example.com'),
    ],
);

use BotPayload\Telegram\ReplyMarkup\Model\Keyboard\KeyboardRemove;

new ReplyMarkup(keyboard: new KeyboardRemove());
// { "remove_keyboard": true }

TelegramFormatter::answerCallbackQuery(
    callbackQueryId: $update->getCallbackQuery()->getId(),
    text: 'Готово',
    showAlert: false,
);
// { "callback_query_id": "cb-1", "text": "Готово" }

use BotPayload\VK\Mapper\Model\Confirmation;
use BotPayload\VK\Mapper\VKMapper;

$event = VKMapper::exec($requestJson);

if ($event instanceof Confirmation) {
    echo $confirmationToken; // строка из настроек сообщества, не JSON
    return;
}

use BotPayload\VK\Mapper\Model\MessageNew;

$event = VKMapper::exec($requestJson);

if ($event instanceof MessageNew) {
    $event->getPeerId();   // куда отвечать (messages.send)
    $event->getFromId();   // кто написал
    $event->getText();     // Привет
    $event->getPayload();   // ['button' => 1] или null
    $event->getObject()->getClientInfo()?->getInlineKeyboard(); // true
}

use BotPayload\VK\Mapper\Model\MessageEvent;

if ($event instanceof MessageEvent) {
    $event->getCallbackEventId(); // c9e90aab7b38 → sendMessageEventAnswer
    $event->getUserId();
    $event->getPeerId();
    $event->getPayload();         // ['button' => 'bot'] (уже массив)
}

use BotPayload\VK\Formatter\Enum\ButtonColor;
use BotPayload\VK\Formatter\Enum\ButtonType;
use BotPayload\VK\Formatter\Model\Button;
use BotPayload\VK\Formatter\Model\Keyboard;
use BotPayload\VK\Formatter\Model\ReplyMarkup;
use BotPayload\VK\Formatter\VKFormatter;

$payload = VKFormatter::sendMessage(
    peerId: $event->getPeerId(),
    text: 'Выберите действие',
    replyMarkup: new ReplyMarkup(
        keyboard: new Keyboard(oneTime: false, inline: true, buttonsPerRow: 2),
        buttons: [
            new Button(label: 'Да', type: ButtonType::CALLBACK, payload: ['cmd' => 'yes'], color: ButtonColor::POSITIVE),
            new Button(label: 'Сайт', type: ButtonType::OPEN_LINK, link: 'https://example.com'),
        ],
    ),
);
// [
//   'peer_id' => 123052131,
//   'message' => 'Выберите действие',
//   'random_id' => <int>,
//   'keyboard' => '{"one_time":false,"inline":true,"buttons":[...]}',
// ]

VKFormatter::keyboardRemove();
// { "one_time": true, "inline": false, "buttons": [] }

$payload = VKFormatter::eventAnswer(
    eventId: $event->getCallbackEventId(),
    userId: $event->getUserId(),
    peerId: $event->getPeerId(),
    eventData: VKFormatter::snackbar('Сохранено'),
);
// [
//   'event_id' => 'c9e90aab7b38',
//   'user_id' => 325017603,
//   'peer_id' => 2000000003,
//   'event_data' => '{"type":"show_snackbar","text":"Сохранено"}',
// ]

VKFormatter::snackbar('Текст до 90 символов');
VKFormatter::openLink('https://example.com');

$update = TelegramMapper::exec($json);

if ($update->isCallbackQuery()) {
    $http->post('answerCallbackQuery', TelegramFormatter::answerCallbackQuery(
        callbackQueryId: $update->getCallbackQuery()->getId(),
    ));
}

$http->post('sendMessage', TelegramFormatter::sendMessage(
    chatId: $update->getChatId(),
    text: 'Ок',
));

$event = VKMapper::exec($json);

if ($event instanceof Confirmation) {
    echo $confirmationToken;
    return;
}

echo 'ok';

if ($event instanceof MessageEvent) {
    $http->post('messages.sendMessageEventAnswer', VKFormatter::eventAnswer(
        eventId: $event->getCallbackEventId(),
        userId: $event->getUserId(),
        peerId: $event->getPeerId(),
        eventData: VKFormatter::snackbar('Ок'),
    ));
}

if ($event instanceof MessageNew) {
    $http->post('messages.send', VKFormatter::sendMessage(
        peerId: $event->getPeerId(),
        text: 'Ок',
    ));
}
json
{
  "keyboard": [
    [{ "text": "Да" }, { "text": "Телефон", "request_contact": true }],
    [{ "text": "Гео", "request_location": true }]
  ],
  "resize_keyboard": true,
  "one_time_keyboard": true
}