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.
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.
Environment configuration
Never commit the token to source control.
TELEGRAM_BOT_TOKEN=123456789:replace_me
TELEGRAM_WEBHOOK_SECRET=replace_with_long_random_secret'telegram' => [
'token' => env('TELEGRAM_BOT_TOKEN'),
'webhook_secret' => env('TELEGRAM_WEBHOOK_SECRET'),
],Public webhook route
Use a stateless API route and verify a secret path segment or Telegram secret header.
<?php
use App\Http\Controllers\TelegramWebhookController;
use Illuminate\Support\Facades\Route;
Route::post('/telegram/webhook/{secret}', TelegramWebhookController::class)
->middleware('throttle:120,1');
Controller + Telegram API call
Validate the secret, parse the update, then respond fast.
<?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]);
}
}
Set the webhook
Point Telegram to your real HTTPS Laravel endpoint.
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());Production architecture
For expensive work, acknowledge Telegram quickly and queue the rest.
Authenticate webhook and normalize Telegram updates.
Keep Telegram API calls in a dedicated service class.
Move slow AI, media and external API jobs off the request path.
Persist users, states and idempotency keys.