Sending Telegram notifications from Laravel requires Telegram Notification Channel installed in Laravel.
Installation of Telegram Notification Channel
Use composer to install the package:
composer require laravel-notification-channels/telegram
After this at the bottom of config/services.php
add:
'telegram-bot-api' => [
'token' => env('TELEGRAM_BOT_TOKEN', 'YOUR BOT TOKEN HERE')
],
Now we need the token from Telegram and chat ID where we'll send notifications. Chat ID is the identification of the thread in Telegram.
Create Telegram Bot
Find BotFather account inside Telegram and start a thread.
You can type /newbot
and it'll guide you through the process of creating new bot.
At the end it'll provide bot token.
Take that bot token and insert into .env
file.
TELEGRAM_BOT_TOKEN=paste-here-token
Obtain Telegram Chat ID
In this case I created new Telegram Group and added my new bot inside.
I'll type /heybot
message to this group just to start the converation, so I can pickup the ID.
The quickest way to obtain Chat ID is to open Tinker with php artisan tinker
and type:
NotificationChannels\Telegram\TelegramUpdates::create()->limit(10)->options(['timeout' => 0])->get();
If you're seeing empty response like this:
= [
"ok" => true,
"result" => [],
]
Retry command few times, until it displays /heybot
message:
= [
"ok" => true,
"result" => [
[
"update_id" => 19293881,
"message" => [
"message_id" => 4,
"from" => [
"id" => 6550505596,
"is_bot" => false,
"first_name" => "Ivan",
"language_code" => "en",
],
"chat" => [
"id" => -4109128101,
"title" => "Laravel App",
"type" => "group",
"all_members_are_administrators" => true,
],
"date" => 1708516220,
"text" => "/heybot",
"entities" => [
From this array inside [result][0][message][chat][id]
is the Chat ID we need. In this case it's -4109128101
.
Copy this into .env
file:
TELEGRAM_CHAT_ID=-4109128101
Laravel Notification Class
Now we need notification class which will use Telegram channel. You can create it with:
php artisan make:notification NewUserRegistered
In this case I decided I'll send notification whenever new user is registered. You can send notification for any event important to you.
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use NotificationChannels\Telegram\TelegramMessage;
class NewUserRegistered extends Notification
{
use Queueable;
public function via(object $notifiable): array
{
return ['telegram'];
}
public function toTelegram($notifiable)
{
return TelegramMessage::create()
->to(env('TELEGRAM_CHAT_ID'))
->content("New user registered")
->button('View Profile', 'https://tuts.codingo.me');
}
}
I put some dummy button there and text. Just to test is it working.
In order to fire this notification I'll use Tinker once more.
Notification::route('telegram', [])->notify(new App\Notifications\NewUserRegistered());
And it works!
Send Notification when new user account is created
I'll connect everything now.
First thing is to pass User object to the notification class, so Telegram receives full user name.
<?php
namespace App\Notifications;
use NotificationChannels\Telegram\TelegramMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Bus\Queueable;
class NewUserRegistered extends Notification
{
use Queueable;
public $user;
public function __construct($user)
{
$this->user = $user;
}
public function via(object $notifiable): array
{
return ['telegram'];
}
public function toTelegram($notifiable)
{
return TelegramMessage::create()
->to(env('TELEGRAM_CHAT_ID'))
->content("New user: " . $this->user->name);
}
}
Now I need to create Listener which will handle Registered event.
php artisan make:listener NotifyTelegramOfRegistration
Inside Listener I'll send on-demand Telegram notification to the desired Chat ID.
<?php
namespace App\Listeners;
use Illuminate\Support\Facades\Notification;
use App\Notifications\NewUserRegistered;
use Illuminate\Auth\Events\Registered;
class NotifyTelegramOfRegistration
{
public function handle(Registered $registered): void
{
$user = $registered->user;
Notification::route('telegram', env('TELEGRAM_CHAT_ID'))
->notify(new NewUserRegistered($user));
}
}
The only thing left is registering this new event listerner inside EventServiceProvider
class EventServiceProvider extends ServiceProvider
{
/**
* The event to listener mappings for the application.
*
* @var array<class-string, array<int, class-string>>
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
NotifyTelegramOfRegistration::class
],
];
This is one example how you can use Telegram to receive notification from your Laravel application. With this you can create any kind of notification for any Laravel resource.