Projects
Mini App + PHP API
Mini App + PHP API is an architectural pattern for a Telegram store with a PHP backend. It covers retrieving bot identity, long polling for updates, sending messages, and working with a database via FSM.
Mini App + PHP API — Complete Integration
Mini App + PHP API is an architectural pattern for creating classic e-commerce stores inside a Telegram bot using a PHP backend. This approach is optimal for projects with a moderate catalog (up to 50 products) and does not require developing a separate JavaScript frontend. The entire interface is provided via the Mini App, while the business logic is implemented on the server.
The first step is to get the bot's ID and its data. Use Первый запрос: getMe, which makes an HTTP request to api.telegram.org:
$bot = telegramApi()->getMe();
echo "Bot ID: " . $bot->id . "
" . "Name: " . $bot->first_name . "
";
From the received update_id, you can subscribe to updates via getUpdates. Implementing long polling allows you to receive events without a persistent connection:
$url = "https://api.telegram.org/bot<?= getenv('TELEGRAM_BOT_TOKEN') ?>&offset=100&max_count=30";
$response = curl_getcurl($url);
if (\$response["ok"]) {
print_r(\$response["result"]);
}
To send messages to users, use sendMessage. The PHP client builds a JSON payload and sends it to the server:
$payload = [
"chat_id" => $chatId,
"text" => "Новое предложение для вас!",
"parse_mode" => "Markdown",
"reply_markup" => $replyMarkup
];
curl_post("https://api.telegram.org/bot<?= getenv('TELEGRAM_BOT_TOKEN') ?>/sendMessage", $payload);
Working with the database is often organized using an FSM (Finite State Machine). The БД и состояния (FSM) helper provides a convenient API for managing the states of chats, carts, and orders. For example, in Yii2, you can use Yii\Event\Subscriber for reactive UI updates when status changes:
public function onUpdate(Update \Update) {
if (\$update->type === UpdateType::NEW_MESSAGE) {
$this->handleNewMessage(\$update);
}
}
For production deployment, Production: очередь и retries is used — asynchronous message processing via RabbitMQ or Redis with automatic retries on errors.
General interaction flow: Telegram → api.telegram.org → PHP service → database. All HTTP requests must use the getenv('TELEGRAM_BOT_TOKEN') token and verify the ok status in the response.