• Reading time ~ 3 min
  • 03.03.2023

Laravel Notifications are great when you need to send notifications out through a variety of channels. Based on the documentation the original use case for these is to send quick information messages that notify users of something that occurred in your application. For example, if you are writing a billing application, you might send an “Invoice Paid” notification to your users via the email and SMS channels.

However, they don’t have to be restricted just those types of notifications. For example, on this site every time a new post is published I want it to go out to all the various social channels and the community has created a lot of drivers for various services.

One new area that I created a few months back, is a read-only Laravel News Telegram group, and in the past, I would manually add the day’s new posts into it. As you can imagine one day I forgot to do this, then the next, and so on. I knew I had to automate the sending of new posts and that is where the Telegram Notification package comes in, let’s take a look at how to get this setup and auto sending notifications to your Telegram room.

Creating a Telegram Bot

Before you can send messages to a room, you have to create a bot. To start, send a new message to @BotFather, and it goes like this:

You: /newbot
BotFather: Alright, a new bot. How are we going to call it? Please choose a name for your bot.
You: MyTestBot
BotFather: Good. Now let's choose a username for your bot. It must end in bot. Like this, for example: TetrisBot or tetris_bot.
You: MyNewBot
BotFather: Done! Congratulations on your new bot. ... Use this token: xxxxxx

Now you will need that token later to add to your .env file so just keep it handy for now.

Add the bot to your room

Open your Telegram room and go to the settings -> administrators and add the bot you just created to the channel. This gives it access to post new messages to the room.

Install Telegram Notifications

Installation is typical for any Laravel package. First require the package:

composer require laravel-notification-channels/telegram

Add it to the providers in app.php:

'providers' => [
    ...
    NotificationChannels\Telegram\TelegramServiceProvider::class,
],

Add the config array:

// config/services.php
...
'telegram-bot-api' => [
    'token' => env('TELEGRAM_BOT_TOKEN', 'YOUR BOT TOKEN HERE')
],

Finally, open .env and add your token:

TELEGRAM_BOT_TOKEN=1234:232jkl42l4j23kl

Create a new notification class

For my use case I am going to send a notification when a new post is published so I create a new PostPublished notification class through artisan:

php artisan make:notification PostPublished

Now open the file and adjust the via method to tell it to use the TelegramChannel:

public function via($notifiable)
{
    return [TelegramChannel::class];
}

Next, create a toTelegram method that will contain the details you want sent:

public function toTelegram($post)
{
    return TelegramMessage::create()
        ->to('@laravelnews')
        ->content($post->title.' https://laravel-news.com/'. $post->uri);
}

In this case, it’s sending to the @laravelnews room with the content of the post title and a link to the post.

Add the notifiable trait

Notifications can be sent in one of two ways. Either through the Notification facade or by adding the Notifiable trait to a model. The facade is recommended when you need to send a notification to multiple notifiable entities such as a collection of users.

In this example, it’s only going out based on one item, so I added the trait to my Post model.

use Illuminate\Notifications\Notifiable;
class Post extends Model
{
    use Notifiable;

Sending your first notification

Now, with everything setup and ready all that is left to do is send our first notification. Fetch a single item from the Model and then fire off the notification:

$post = \App\Post::find(1);
$post->notify(new \App\Notifications\PostPublished());

If all is correct, you’ll see it instantly in your Telegram channel.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

ABOUT

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

About author CrazyBoy49z
WORK EXPERIENCE
Contact
Ukraine, Lutsk
+380979856297