Integrating Telegram notifications into a Laravel app is a solid move. It’s faster than email and less intrusive than SMS.
Here is a streamlined way to wire it up.
1. Database and Bot Setup
First, you need a place to store the handshake between your app and Telegram. Add a telegram_id column to your users table.
phpSchema::table('users', function (Blueprint $table) { // $table->string('telegram_id')->nullable()->unique(); // });
Next, talk to @BotFather on Telegram. Create a new bot, grab your API Token, and add it to your .env file. You’ll need this token for every request your backend makes.
2. The Frontend "Connect" Button
To link a user, you need them to start a conversation with your bot while carrying their local User ID as a parameter. Using JavaScript to handle the redirect keeps the UI clean.
html<button id="connectTelegram">Connect Telegram</button> <script> document.getElementById('connectTelegram').addEventListener('click', () => { const userId = "{{ auth()->id() }}"; const botUsername = "YourBotUsername"; window.location.href = `tg://resolve?domain=${botUsername}&start=${userId}`; }); </script>
When the user clicks this, Telegram opens and sends a /start {userId} message to your bot.
Small note: When the user sends /start, the extra parameter isn’t visible on their end. They’ll just see a normal /start message in their chat.
3. Handling the Webhook
You need a controller to listen for Telegram’s ping. When the user hits "Start," Telegram sends a POST request to your app.
phppublic function handleWebhook(Request $request) { $message = $request->input('message'); $chatId = $message['from']['id']; $text = $message['text']; // Telegram sends the start param as "/start 123" if (preg_replace('/\D/', '', $text)) { $userId = explode(' ', $text)[1]; User::where('id', $userId)->update([ 'telegram_id' => $chatId ]); } return response()->json(['status' => 'success']); }
Don't forget to exclude this route from CSRF protection in your VerifyCsrfToken middleware.
Connecting your bot to a webhook:
You can set your bot’s webhook by opening this URL in your browser (replace<YOUR_BOT_TOKEN>and your webhook endpoint accordingly):
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/setWebhook?url=your_webhook_endpointOnce opened, Telegram will push all messages to your Laravel controller automatically.
4. Sending the Notification
Laravel’s Notification system makes the actual delivery feel native to the framework. While you can use a community package, a simple Http call often suffices for basic alerts.
phppublic function sendTelegramNotification($userId, $text) { $user = User::find($userId); if ($user->telegram_id) { Http::post("https://api.telegram.org/bot" . env('TELEGRAM_BOT_TOKEN') . "/sendMessage", [ 'chat_id' => $user->telegram_id, 'text' => $text, 'parse_mode' => 'HTML' ]); } }
A few thoughts
The start={id} parameter is the "deep linking" magic of Telegram. It bypasses the need for users to type codes manually.
Just ensure your webhook URL is secured via HTTPS, or Telegram won't talk to it. It’s a simple system, but once it’s running, the engagement rates usually beat email by a mile.
