logo

Hcody

๐Ÿš€ Promote your brand here โ€” Reach our amazing audience!
Home/ Articles / create-telegram-bot-laravel-webhook

How to Build a Telegram Bot in Laravel using Webhooks

By Hisham Al Nahas ยท Published on December 31, 2025

Integrating a Telegram bot into a Laravel application opens up amazing possibilities. Instead of running separate background scripts, you can handle user messages directly within your application's lifecycle.

In this guide, we will use Webhooks, the most efficient method for modern web applications, to connect Laravel with Telegram in real-time.

Prerequisites

Laravel Project

An installed and running Laravel instance (preferably version 10 or 11).

Secure Connection (HTTPS)

Telegram requires SSL. For local development, we will use Ngrok.

Telegram Account

To access BotFather and obtain your API credentials.


Step 1: Bot Setup and Token

Everything starts with BotFather inside the Telegram app:

  1. Search for @BotFather.
  2. Send the /newbot command.
  3. Choose a display name and a username (must end in "bot").
  4. Copy the API Token and add it to your .env file:
TELEGRAM_BOT_TOKEN=123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11

Step 2: Create Route and Controller

We need an endpoint to receive the data. Let's create a Controller:

php artisan make:controller TelegramBotController

Then, add the route in routes/web.php:

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

Route::post('/telegram/webhook', [TelegramBotController::class, 'handle']);
Bypass CSRF Protection

Laravel rejects external POST requests that lack a CSRF token. Since Telegram is the sender, you must exclude telegram/webhook in bootstrap/app.php (for v11) or the VerifyCsrfToken middleware.

Step 3: Writing the Bot Logic

Here is the complete Controller code to reply to messages:

<?php

namespace App\Http\Controllers;

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

class TelegramBotController extends Controller
{
    public function handle(Request $request)
    {
        $update = $request->all();
        
        // Check if the update contains a text message
        if (isset($update['message'])) {
            $chatId = $update['message']['chat']['id'];
            $text = $update['message']['text'] ?? '';

            if ($text === '/start') {
                $response = "Welcome to your Laravel Bot!";
            } else {
                $response = "You said: " . $text;
            }

            $this->sendMessage($chatId, $response);
        }

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

    private function sendMessage($chatId, $text)
    {
        $token = env('TELEGRAM_BOT_TOKEN');
        $url = "https://api.telegram.org/bot{$token}/sendMessage";

        Http::post($url, [
            'chat_id' => $chatId,
            'text' => $text,
        ]);
    }
}

Step 4: Activating the Webhook

Developer Tip

If you are working locally, use Ngrok to generate a public HTTPS URL. Telegram will not accept http://localhost links.

To link the bot to your app, visit this URL in your browser (after replacing placeholders):

https://api.telegram.org/bot<YOUR_TOKEN>/setWebhook?url=https://your-domain.com/telegram/webhook
Congratulations!

Your bot is now live. Try sending a message on Telegram and watch your Laravel app reply instantly.

Related Articles

๐Ÿš€ Promote your brand here โ€” Reach our amazing audience!