How to send Notifications via Telegram with Laravel

October 10, 2022 | 3 minute read

A quick guide to getting started with Telegram notifications in Laravel

How to send Notifications via Telegram with Laravel

Telegram Messenger is one of the most popular messaging services out there, with over 700 million monthly active users at the time of this writing. They also have a well-documented API. One handy use case for Telegram is sending notifications to users from a Web App; Laravel in this case. This tutorial assumes you have a Laravel project and a Telegram account.

Creating a Bot (And Generating a Token)

We'll begin by creating a Telegram Bot. To do this just search for the "BotFather" within the Telegram app and begin an interaction. You'll then use /newbot option to create and name a new "Bot". Once you've created the bot, the BotFather will present you with your token. Be sure to securely store it somewhere.

Installing a Telegram Notification Channel

Fortunately, there are several handy community-built notification channels out there for Laravel, so we don't have to build one. Isn't the Laravel community just awesome? Go ahead and require the Telegram notification channel:

composer require laravel-notification-channels/telegram

Configuration

Per the package docs, you'll need to add this to your services.php config file, then add the appropriate key/value to your .env file.

'telegram-bot-api' => [
'token' => env('TELEGRAM_BOT_TOKEN')
],

Obtaining a Chat ID

Next you'll need to fetch the "Chat ID" of the entity you wish to send messages to: a user, group, or channel. Some key differences:

  • A group is a traditional group message where all users can participate
  • A channel is a message board with subscribers that only authorized users can post to

In this example, I'll be sending messages to a channel via an on-demand notification.

The package docs outline a helper script to get the chat ID of an entity. An easy way to run this script is by executing it via a closure-based console command. Add the following to routes/console.php:

Note: you need to interact with your bot from the entity you wish to communicate with to create an "update". If it's a channel, you'll create a channel and add your bot as an administrator of the channel and send the channel a message. If you want to send messages to a user, have that user message your bot to create an "update".

Artisan::command('telegram:chat-id', function(){
 
// Response is an array of updates.
$updates = NotificationChannels\Telegram\TelegramUpdates::create()
->limit(2)
->options([
'timeout' => 0,
])
->get();
 
if($updates['ok']) {
$chatId = $updates['result'][0]['message']['chat']['id'];
}
 
$this->comment($chatId);
});

Now you can run the closure-based command with php artisan telegram:chat-id. In this case, it will output the Channel's Chat ID.

If sending notifications to a single user, I would recommend persisting the Chat ID via a relationship to the appropriate user model.

Create a Notification

Let's scaffold a notification:

php artisan make:notification GoodMorning

Update the notification class's via method:

public function via($notifiable)
{
return ['telegram'];
}

Add a toTelegram method to the notification class with your notification's content.

use NotificationChannels\Telegram\TelegramMessage;
 
public function toTelegram($notifiable)
{
return TelegramMessage::create()
->to($notifiable->routes['telegram'])
->content("Good Morning! ⚡");
}

Send a Notification to Telegram

A simple way to test out our new notification is by registering another closure command in routes/console.php:

use Illuminate\Support\Facades\Notification;
 
Artisan::command('telegram:say-goodmorning', function(){
 
// My Channel
Notification::route('telegram', 'myChannelId')
->notify(new GoodMorning());
});

Note: The second argument of route is the Chat ID of the channel we'll be posting to.

Now you can run the command and your message will appear in your Telegram channel!

php artisan telegram:say-goodmorning

Be sure to check out the package docs - there are several more neat ways to interact with Telegram programmatically.