BotBhai
BotBhaiDeveloper Docs
Built-in runtime

PHP Webhook Bot

This is the native deployment path for the platform. The Start/Restart controls publish your workspace and connect Telegram to a public bot.php webhook.

Compatibility summary

Main file: bot.php · Request body: Telegram JSON · Mode: webhook · Long-running polling: not supported. The dashboard's encrypted bot token is used by platform controls to set/delete the webhook; your PHP script should load its own server-side token/config for Telegram API methods.

01

Minimal working bot

Paste this as bot.php, replace the token, Start the bot, then send /start.

bot.php
<?php

declare(strict_types=1);

$update = json_decode(file_get_contents('php://input') ?: '[]', true);
if (!is_array($update)) {
    http_response_code(200);
    exit('OK');
}

$token = 'PASTE_BOT_TOKEN_HERE';
$message = $update['message'] ?? null;
$chatId = $message['chat']['id'] ?? null;
$text = trim((string) ($message['text'] ?? ''));

if ($chatId && $text === '/start') {
    telegram($token, 'sendMessage', [
        'chat_id' => $chatId,
        'text' => "Welcome! Your webhook bot is online ✅",
    ]);
}

http_response_code(200);
echo 'OK';

function telegram(string $token, string $method, array $payload): array
{
    $url = "https://api.telegram.org/bot{$token}/{$method}";
    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_POST => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT => 15,
        CURLOPT_POSTFIELDS => $payload,
    ]);
    $body = curl_exec($ch);
    curl_close($ch);

    return json_decode((string) $body, true) ?: [];
}
02

Recommended multi-file structure

Keep the entrypoint small and move commands into helper files.

bot.phpWebhook entrypointsrc/Telegram.phpTelegram API helpersrc/Commands.phpCommand router.envServer-side secrets
bot.php
<?php

declare(strict_types=1);

require __DIR__.'/src/Telegram.php';
require __DIR__.'/src/Commands.php';

$config = parse_ini_file(__DIR__.'/.env', false, INI_SCANNER_RAW) ?: [];
$token = (string) ($config['TELEGRAM_BOT_TOKEN'] ?? '');
$update = json_decode(file_get_contents('php://input') ?: '[]', true) ?: [];

if ($token !== '') {
    handleTelegramUpdate($token, $update);
}

http_response_code(200);
echo 'OK';
.env
TELEGRAM_BOT_TOKEN=123456789:replace_with_real_token
03

Command routing pattern

A small router keeps AI-generated and hand-written code maintainable.

src/Commands.php
<?php

function handleTelegramUpdate(string $token, array $update): void
{
    $message = $update['message'] ?? [];
    $chatId = $message['chat']['id'] ?? null;
    $text = trim((string) ($message['text'] ?? ''));

    if (!$chatId) return;

    $reply = match (true) {
        $text === '/start' => 'Welcome to the bot!',
        $text === '/help'  => "Commands:\n/start\n/help\n/status",
        $text === '/status' => 'Bot status: online ✅',
        default => 'Unknown command. Send /help',
    };

    telegram($token, 'sendMessage', [
        'chat_id' => $chatId,
        'text' => $reply,
    ]);
}
04

Platform-specific do and don't

These rules prevent the most common deployment failures.

Do

  • Name the entry file bot.php.
  • Use __DIR__ for helper file paths.
  • Return HTTP 200 quickly.
  • Restart after publishing file changes.
  • Read Terminal Logs after errors.

Don't

  • Do not run while(true) polling loops.
  • Do not expose the token in HTML/JS.
  • Do not rely on a CLI worker staying alive.
  • Do not print debug secrets to public output.
  • Do not hardcode absolute server paths.