Using Laravel 9, Send Telegram Notifications

We’ll discover how to use Laravel 9 to send Telegram

Step:1 Install the Telegram notification channel first.
You must install the Telegram notification channel using Composer before you may deliver notifications using Telegram.

composer require laravel-notification-channels/telegram

Step:2 Create your Telegram bot in step two.
Open Telegram and say hi to the BotFather bot there.

Choose or enter /newbot. Select a username and a name to receive your token.

Step:3 In the config/service.php section, add the Telegram

# config/services.php

‘telegram-bot-api’ => [
‘token’ => env(‘TELEGRAM_BOT_TOKEN’, ‘YOUR BOT TOKEN HERE’)
],

Fill up the environment file with your

# .env

TELEGRAM_BOT_TOKEN=5388040587:AAH0Jm7h9Meycg-M9Zh7MT8AeQMvI-7VsdQ

Step:4 Get your groupId after

choose a new group

Put your bot in the group.

Choose a group name.

Group IDBot with this.

Gather your team. Id

Step: 5 Make and set up a notification

Each notification in Laravel is represented by a single class, which is normally kept in the directory app/Notifications. If you run the make command, a directory will be created for you even if you don’t see it in your application. notification artisan’s order:

php artisan make:notification SendNotification

following configure file app/Notification/SendNotification

<?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 SendNotification extends Notification
{
use Queueable; public function __construct()
{
//
}

public function via($notifiable)
{
return ['telegram'];
}public function toTelegram($notifiable)
{
return TelegramMessage::create()
->to('-794197572')
->content('Enviando nuestro primer mensaje con Telegram');
}
}

Step:6 Send Message

You can deliver notifications using the notify trait or, as an alternative, the Notification façade. When you need to notify numerous notifiable entities, such as a group of users, this strategy can be helpful.

notify trait

use App\Notifications\SendNotification;$user->notify(new SendNotification($invoice));

Notification  facade

use Illuminate\Support\Facades\Notification;Notification::send($users, new SendNotification($invoice));

Leave a Reply

Your email address will not be published.