Equivalent Complete Bots: Helper PHP and Plain PHP
Choose ONE example as your test bot's index.php. Both handle /start, /help and text replies. They use plain text responses, bound the echoed input and ignore other update types. They are small demonstrations, not a production retry/state system.
Both examples need PHP mbstring. The plain PHP example also needs cURL and a configured BOT_TOKEN environment variable. Botlify's hosted runtime supplies that variable; standalone PHP needs its own environment setup. Never paste a real token into shared example code.
Botlify helper version
Call tg_all_fun before the first helper. The platform resolves the active bot token. No file include is needed.
<?php
declare(strict_types=1);
tg_all_fun();
$update = json_decode((string)file_get_contents('php://input'), true) ?: [];
$message = $update['message'] ?? [];
if (!isset($message['chat']['id'], $message['text'])) {
http_response_code(200);
return;
}
$text = trim($message['text']);
$command = explode('@', explode(' ', $text)[0])[0];
$reply = match ($command) {
'/start' => 'Hello! Send /help or a short message.',
'/help' => 'This demo replies to text messages.',
default => 'You said: ' . mb_substr($text, 0, 1000, 'UTF-8'),
};
$response = tg_send($message['chat']['id'], $reply, ['parse_mode' => null]);
if (!($response['ok'] ?? false)) {
error_log('Demo: Telegram reply failed.');
}
http_response_code(200);
Plain PHP/cURL version
This uses no tg_* function and no loader. You own the HTTP configuration, TLS checks, JSON encoding, timeout and error handling.
<?php
declare(strict_types=1);
// Hosted Botlify provides the environment. Standalone PHP must configure it.
$token = (string)(getenv('BOT_TOKEN') ?: '');
if ($token === '' || !function_exists('curl_init')) {
error_log('Demo: BOT_TOKEN or PHP cURL is missing.');
http_response_code(500);
return;
}
$update = json_decode((string)file_get_contents('php://input'), true) ?: [];
$message = $update['message'] ?? [];
if (!isset($message['chat']['id'], $message['text'])) {
http_response_code(200);
return;
}
$text = trim($message['text']);
$command = explode('@', explode(' ', $text)[0])[0];
$reply = match ($command) {
'/start' => 'Hello! Send /help or a short message.',
'/help' => 'This demo replies to text messages.',
default => 'You said: ' . mb_substr($text, 0, 1000, 'UTF-8'),
};
$curl = curl_init('https://api.telegram.org/bot' . $token . '/sendMessage');
curl_setopt_array($curl, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 15,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => json_encode([
'chat_id' => $message['chat']['id'],
'text' => $reply,
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
]);
$raw = curl_exec($curl);
curl_close($curl);
$response = is_string($raw) ? json_decode($raw, true) : null;
if (!is_array($response) || !($response['ok'] ?? false)) {
error_log('Demo: Telegram reply failed.');
}
http_response_code(200);
What differs?
The helper version delegates request transport to Botlify; the plain version constructs it locally. The helper may apply shared retry/fallback behavior, while the plain example makes one attempt. Both still depend on Telegram and network latency. Neither version claims guaranteed delivery or a fixed response time.
Deploy in the Botlify test bot editor, check the webhook is active, then send /start, /help and a short message. Review new error.log entries if a reply is missing. Do not replace the platform root entrypoint with either example.
Add features deliberately
For buttons and callbacks, use the update/state guide. For media, rich messages, custom emoji and effects, use the complete helper reference. For scheduling and deployment, use the operations guide.