Send On-Demand Slack Notifications from Laravel

By Priyash Patil | 2 min read | Updated: Apr 22, 2023 10PM UTC

Discover how to integrate Slack and Laravel to send important notifications to your team for improved communication and productivity. Learn how this powerful tool can benefit your sales team and beyond.

Problem statement

You have a requirement of sending notification to a specific slack channel using Laravel. You need to configure the incoming Slack WebHook on your Slack channel and then send notification from Laravel application.

Solution

Slack offers many ways for integration with external services. I’ll be using WebHooks to satisfy the requirement. This requires admin access to slack workspace.

Configure Slack Incoming WebHook

  1. Navigate to the Slack channel you want to send notifications to.
  2. Click channel details.
  3. Click Integrations.
  4. Click Add apps in apps card.
  5. Search WebHook.
  6. Click Install on Incoming WebHooks.
  7. Select add to slack.
  8. From dropdown select the channel.
  9. Click add Incoming WebHook Integration.

After that, On the Incoming WebHooks page, from the WebHook URL field, copy the WebHook URL, you should have a link something like:

https://hooks.slack.com/services/your_unique_identifiers

Installing and configuring packages

Laravel offers official slack channel package. We can install it by composer.

composer require laravel/slack-notification-channel

Create a Laravel Notification.

php artisan make:notification SendSlackNotification

And then update the Notification as follows:

<?php

namespace  AppNotifications;
 
use  IlluminateBusQueueable;
use  IlluminateContractsQueueShouldQueue;
use  IlluminateNotificationsMessagesSlackMessage;
use  IlluminateNotificationsNotification;

class SendSlackNotification extends Notification {
	use  Queueable;
	
	public $level;
	public $message;

	/**
	* Create a new notification instance.
	* 
	* @return void
	* */
	public function __construct(string $level, string $message) 
	{ 
		$this->level = $level;
		$this->message = $message;
	}

	/**
	* Get the notification's delivery channels.  
	* 
	* @return array 
	* */ 
	public function via() {
		 return ['slack']; 
	} 

	/** 
	* Get the Slack representation of the notification. 
	* * * @return IlluminateNotificationsMessagesSlackMessage 
	* */  
	public function toSlack() { 
		$slackMessage = new  SlackMessage();
		$slackMessage->level = $this->level;
		$slackMessage->content = $this->message; 

		return  $slackMessage; 
	} 
}

Sending notification

Sending notifications on-demand. Laravel provides an on-demand notification facade.

<?php

use IlluminateSupportFacadesNotification;

$slackWebhookUrl = 'https://hooks.slack.com/services/your_unique_ids';
$message = 'Test message';

Notification::route('slack', $slackWebhookUrl)
	->notify(new  SendSlackNotification('success', $message));

Conclusion

Congratulations 🎉, you can use the Notification route to trigger dynamic notifications from anywhere in the application. There are a lot of improvements can be made added. My goal was to get you started quickly.

Keep the Conversation Going

I hope you found this post helpful! If you have any questions or feedback, feel free to reach out. You can also find me on X (Twitter) @priyashpatil for additional insights and updates on my latest content.

Related

Featured on laravel-news.com and benjamincrozat.com.

Optimized image uploads with CKEditor and Laravel

By Priyash Patil on Friday, 03 November 2023

Bootstrap 5 Remove Unused CSS with Vite and PurgeCSS

By Priyash Patil on Wednesday, 17 January 2024

Laravel Vite Deploy Assets to Global CDN

By Priyash Patil on Friday, 19 January 2024

Laravel file upload with validation example

By Priyash Patil on Thursday, 25 January 2024