Getting started
Bot API vs MTProto
Bot API vs. MTProto: Why PHP bots almost always need HTTPS/JSON rather than a binary protocol. Example of getMe in cURL parsing the response.
Telegram provides two stacks: Bot API — normal HTTPS/JSON, and MTProto — binary TCP protocol used by official clients. For 95% of tasks (stores, CRM, Mini Apps, mailings), only the Bot API is needed. MTProto is justified if you need to read other people's messages in groups, monitor the status of "prints" or reactions in real time — that is, write userbots. Price: difficult session, frequent breakdowns when updating the scheme, high risk of a ban for mass actions.
Bot API — how it works in PHP
Each method is a post request to https://api.telegram.org/bot<TOKEN>/methodName. Body — JSON, response — JSON with fields ok bool result (object/array), in case of error — error_code And description. Below is the minimum working fragment: we get information about the bot through getMe.
<?php
$token = getenv('TELEGRAM_BOT_TOKEN');
if (!$token) {
die('TELEGRAM_BOT_TOKEN не задан в окружении\n');
}
$url = "https://api.telegram.org/bot{$token}/getMe";
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_TIMEOUT => 10,
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlErr = curl_error($ch);
curl_close($ch);
if ($curlErr) {
die("cURL error: {$curlErr}\n");
}
if ($httpCode !== 200) {
die("HTTP {$httpCode}: {$response}\n");
}
$data = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
die("Invalid JSON: " . json_last_error_msg() . "\n");
}
if (!($data['ok'] ?? false)) {
die("API error: " . ($data['description'] ?? 'unknown') . "\n");
}
// Красивый вывод для терминала
echo json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
Start the php getme.php. In the terminal you will see the structure of the response — this is how you will get used to checking ok and disassemble result.
{
"ok": true,
"result": {
"id": 123456789,
"is_bot": true,
"first_name": "MyBot",
"username": "mybot_bot",
"can_join_groups": true,
"can_read_all_group_messages": false,
"supports_inline_queries": false
}
}
What to see: private static $value = '' ; — the request has passed, in result lies object Пользователь (bot) Fields can_read_all_group_messages And supports_inline_queries show the rights of the bot — they are set by BotFather. If ok = false;Look. error_code (401 — invalid token, 404 — method does not exist) and description.
Decision Making Comparison
- Protocol: Bot API — HTTPS/JSON (any HTTP client), MTProto — binary TCP (requires a special library).
- Authorization: Bot API — token from BotFather, MTProto —
API ID/api_hash+ user session file. - Latency: Bot API — tens of hundreds of milliseconds (through Telegram servers), MTProto — tens of milliseconds (direct connection).
- Data Access Bot API — only messages addressed to the bot (commands, mentions, persona), MTProto — everything that the account sees.
- Ban risk: Bot API — low during regular operation, MTProto — high for spam, mass invitations, userbot patterns.
- PHP Ecosystem: Bot API — pure cURL / Guzzle / Symfony HttpClient, MTProto — no stable libraries (port
danog/MadelineProtodifficult to support).
Practical checklist: when exactly do you need a Bot API
- Bot for store, support, newsletters, Mini App.
- You need to deploy on a regular hosting / VPS without long processes.
- The team writes in PHP (Yii2, Laravel, pure PHP).
- Predictable behavior when updating Telegram is important.
If the item about "reading other people's messages in groups without mentioning the bot" is the only blocker, then look towards MTProto (Python/Node), but be prepared for session support and schema migrations.
In this guide
Next — only Bot API: HTTPS + cURL, getenv('TELEGRAM_BOT_TOKEN'), JSON out/back. Telegram updates the protocol itself, your PHP code does not break. Let's start by getting a token from BotFather.
Next: BotFather и токен — how to create a bot, get a token and safely store it in the environment.