Laravel Rocket Chat Notifications


April 19th, 2022

Laravel Rocket Chat Notifications

Laravel Rocket Chat Notifications is a package that makes it easy to send notifications using RocketChat with Laravel 9+:

1use Illuminate\Notifications\Notification;
2use TeamNiftyGmbh\RocketChatNotifications\Channels\RocketChatNotificationChannel;
3use TeamNiftyGmbh\RocketChatNotifications\Messages\RocketChatMessage;
5class TaskCompleted extends Notification
7 public function via(mixed $notifiable): array
8 {
9 return [
10 RocketChatNotificationChannel::class
11 ];
12 }
14 public function toRocketChat($notifiable): RocketChatMessage
15 {
16 return RocketChatMessage::create('Test Message');
17 }

The RocketChatMessage class has convenient methods to formulate your RocketChat messages. Here's an example of a few of them, including adding an attachment:

1public function toRocketChat($notifiable)
3 return RocketChatMessage::create('Test message')
4 // optional if set in config
5 ->to('channel_name')
6 // optional if set in config
7 ->from('access_token', 'rocket_chat_user_id')
8 ->attachments([
9 RocketChatAttachment::create()
10 ->imageUrl('test'),
11 RocketChatAttachment::create([
12 'image_url' => 'test'
13 ]),
14 new RocketChatAttachment(['image_url' => 'test']),
15 // Or via array
16 [
17 'image_url' => 'test'
18 ]
19 ]);

For a complete list of everything, you can do to format a message, check out the available methods in the readme.

Rocket.Chat is a communication platform with private chats, team chats, etc. and includes pricing options for the SaaS product and self-managed installations. You can learn more about this package, get full installation instructions, and view the source code on GitHub.

This package was submitted to our Laravel News Links section. Links is a place the community can post packages and tutorials around the Laravel ecosystem. Follow along on Twitter @LaravelLinks

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.