BotBhai
BotBhaiDeveloper Docs
External / compatible hosting

Laravel Telegram Bot Integration

Use Laravel when you need queues, database models, services, rate limits and a larger application architecture. This code runs on a Laravel-capable server, not inside the built-in PHP file runner.

Hosting note

Create the bot record on this platform for management only if that fits your workflow, but deploy this Laravel application to a server that runs a full Laravel project. Telegram's webhook should point to that Laravel endpoint.

01

Environment configuration

Never commit the token to source control.

.env
TELEGRAM_BOT_TOKEN=123456789:replace_me
TELEGRAM_WEBHOOK_SECRET=replace_with_long_random_secret
config/services.php
'telegram' => [
    'token' => env('TELEGRAM_BOT_TOKEN'),
    'webhook_secret' => env('TELEGRAM_WEBHOOK_SECRET'),
],
02

Public webhook route

Use a stateless API route and verify a secret path segment or Telegram secret header.

routes/api.php
<?php

use App\Http\Controllers\TelegramWebhookController;
use Illuminate\Support\Facades\Route;

Route::post('/telegram/webhook/{secret}', TelegramWebhookController::class)
    ->middleware('throttle:120,1');
03

Controller + Telegram API call

Validate the secret, parse the update, then respond fast.

TelegramWebhookController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class TelegramWebhookController extends Controller
{
    public function __invoke(Request $request, string $secret)
    {
        abort_unless(hash_equals((string) config('services.telegram.webhook_secret'), $secret), 403);

        $chatId = data_get($request->all(), 'message.chat.id');
        $text = trim((string) data_get($request->all(), 'message.text', ''));

        if ($chatId && $text === '/start') {
            Http::timeout(10)->post(
                'https://api.telegram.org/bot'.config('services.telegram.token').'/sendMessage',
                ['chat_id' => $chatId, 'text' => 'Laravel bot is online ✅']
            );
        }

        return response()->json(['ok' => true]);
    }
}
04

Set the webhook

Point Telegram to your real HTTPS Laravel endpoint.

Artisan / one-time script
use Illuminate\Support\Facades\Http;

$url = url('/api/telegram/webhook/'.config('services.telegram.webhook_secret'));

$response = Http::asForm()->post(
    'https://api.telegram.org/bot'.config('services.telegram.token').'/setWebhook',
    ['url' => $url]
);

dump($response->json());
05

Production architecture

For expensive work, acknowledge Telegram quickly and queue the rest.

Controller

Authenticate webhook and normalize Telegram updates.

Service

Keep Telegram API calls in a dedicated service class.

Queue

Move slow AI, media and external API jobs off the request path.

Database

Persist users, states and idempotency keys.