We noticed you're using an ad blocker

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Laravel 6 Slack notification in real-time

world cup 2022

FootballDesk

Get update standing EPL, UEFA, LaLiga and more

Get Now

Realtime notification is very helpful to know any application event in just time so that we can take any action depending on that event. In Laravel, there are several ways for sending real-time notification from your application but here in this post, I'll show you the easiest way for Slack notification system that you can easily configure to your Laravel project for sending notification within few steps.

Scenario: Suppose, We have a blog site, where users can register and post their blog post and every blog post will be live on the website after review. If a user post review takes a long time to review that will be a very bad experience for your users. So in this scenario, you can send a real-time slack notification to your slack channel, so that you will get notified when new post will be submitted for review and you can take action in just time. Here is one scenario but it can be anything according to your need, like.

  • Notification when a new user registers to your site.
  • Notification when an emergency event happened to your application
  • Notification when an invoice gets paid
  • Notification when your SMS balance near to finish
  • Notification when important event happened.
  • Notification application logs
  • so on

 

Laravel 6 Slack Notification process

  1. Make a Slack app
  2. Make a Slack Notification Helper
  3. Send slack notification to a channel

 

Step 01: Make a Slack app

Log in to your Slack account and create a Slack app for send notification to your slack channel. After app creation, add Incoming Webhooks features to your slack app and active. In this step, you will get a webhook URL. Copy it for use in your Laravel application.

slack-app-webhook.png

 

Step 02: Make a Slack Notification Helper

Make a sendSlackNotification helper method, where you have already made your necessary helper methods. In the helper method, please change the webhook URL with what you have gotten from step 01.

function sendSlackNotification($text)
{
    $headers = array();
    $headers[] = 'Content-type: application/json';
    $data = json_encode(["text" => $text]);
    
    $webhookUrl = "HERE_PUT_YOUR_WEBHOOK_URL";

    $ch = curl_init($webhookUrl);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_exec($ch);
    $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return $statusCode == 200 ? true : false;
}

 

Step 03: Send Slack notification to a channel

Our configuration is finished. Now we can send a notification to our Slack channel from anywhere of our application. Let's try the scenario which I have told in the early of this post.

...
...

$post = new Post();
$post->title = $request->input('title');
$post->description= $request->input('description');
$post->user_id = auth()->user()->id;
$post->status = "pending";
$post->save();

//send a slack notification to admin
sendSlackNotification("A new article posted for review");

You can send multi-line notification by just concatenating the \n into your message.

sendSlackNotification("Hi! Admin\nNew article posted for review\nTitle: ". $post->title);

You will get a notification like this.

slack-notification-sample.png

 

Thank you for reading this article with patience. Hope this article helps you to learn how to send slack notification from your Laravel application. If you find this article is helpful to learn how to send slack notification from Laravel application easily then please share this article with others.


Share on




Related Post - Latest Post


Tinkerpad - A minimal Laravel code editor

What's new in Laravel 9

Make laravel site super fast by page-cache!

Laravel maintenance mode bypass by Secret Route!

Laravel database backup automatically - cPanel shared hosting, VPS

Laravel Datatables - Ajax, Column, Buttons, Customization