This package makes it easy to send notifications using RocketChat with Laravel 9.0+.

Overview

laravel-rocket-chat-notifications

Introduction

This package makes it easy to send notifications using RocketChat with Laravel 9.0+.

Contents

Installation

You can install the package via composer:

$ composer require team-nifty-gmbh/laravel-rocket-chat-notifications

Setting up the RocketChat service

In order to send message to RocketChat channels, you need to create a bot user with an access token in your RocketChat Application

You can publish the config file with:

$ php artisan vendor:publish --provider="TeamNiftyGmbh\RocketChatNotifications\RocketChatNotificationsServiceProvider"

The config file looks as follows:

// config/rocket-chat.php
...
    /*
    |--------------------------------------------------------------------------
    | Default RocketChat Connection Name
    |--------------------------------------------------------------------------
    |
    | Here you may specify which RocketChat connections below you wish
    | to use as your default connection for all RocketChat notifications. Of course
    | you may use many connections at once using the RocketChat static methods or 
    | RocketChatMessage connection method.
    |
    */

    'default' => env('ROCKETCHAT_CONNECTION', 'rocket-chat'),

    /*
    |--------------------------------------------------------------------------
    | RocketChat Connections
    |--------------------------------------------------------------------------
    |
    | Here are each of the RocketChat connections setup for your application.
    |
    */

    'connections' => [

        'rocket-chat' => [
            // Base URL for RocketChat API server (https://your.rocketchat.server.com)
            'url' => env('ROCKETCHAT_URL'),
            'token' => env('ROCKETCHAT_TOKEN'),
            'user_id' => env('ROCKETCHAT_USER_ID')
        ]

    ]
...

If you have published the config file, you can add your RocketChat API server's base url, access token and user Id to config/rocket-chat.php. You can also create additional connections if you wish to serve multiple RocketChats in your application.

Usage

You can use the channel in your via() method inside the notification:

use Illuminate\Notifications\Notification;
use TeamNiftyGmbh\RocketChatNotifications\Channels\RocketChatNotificationChannel;
use TeamNiftyGmbh\RocketChatNotifications\Messages\RocketChatMessage;

class TaskCompleted extends Notification
{
    public function via(mixed $notifiable): array
    {
        return [
            RocketChatNotificationChannel::class
        ];
    }

    public function toRocketChat($notifiable): RocketChatMessage
    {
        return RocketChatMessage::create('Test Message');
    }
}

In order to let your notification know which RocketChat channel you are targeting, add the routeNotificationForRocketChat method to your Notifiable model:

public function routeNotificationForRocketChat(): string
{
    return 'channel_name';
}

Available Message methods

connection(): Sets the connection.

domain(): Sets the domain of your rocket chat server.

from(): Sets the sender's access token and user id.

to(): Specifies the channel id to send the notification to (overridden by routeNotificationForRocketChat if empty).

content(): Sets a content of the notification message. Supports Github flavoured markdown.

alias(): This will cause the message’s name to appear as the given alias, but your username will still display.

avatar(): This will make the avatar use the provided image url.

attachment(): This will add a single attachment.

attachments(): This will add multiple attachments.

clearAttachments(): This will remove all attachments.

Adding Attachment

There are several ways to add one or more attachments to a message

public function toRocketChat($notifiable)
{
    return RocketChatMessage::create('Test message')
        ->connection('rocket-chat') // optional to alter the default connection, overrides 'domain', 'to' and 'from'
        ->domain('https://your.rocketchat.server.com') //optional if set in config
        ->to('channel_name') // optional if set in config
        ->from('access_token', 'rocket_chat_user_id') // optional if set in config
        ->attachments([
            RocketChatAttachment::create()->imageUrl('test'),
            RocketChatAttachment::create(['image_url' => 'test']),
            new RocketChatAttachment(['image_url' => 'test']),
            [
                'image_url' => 'test'
            ]   
        ]);   
}

Available Attachment methods

color(): The color you want the order on the left side to be, any value background-css supports.

text(): The text to display for this attachment, it is different than the message’s text.

timestamp(): Displays the time next to the text portion. ISO8601 Zulu Date or instance of any \DateTime

thumbnailUrl(): An image that displays to the left of the text, looks better when this is relatively small.

messageLink(): Only applicable if the ts is provided, as it makes the time clickable to this link.

collapsed(): Causes the image, audio, and video sections to be hiding when collapsed is true.

author($name, $link, $icon): shortcut for author methods

authorName(): Name of the author.

authorLink(): Providing this makes the author name clickable and points to this link.

authorIcon(): Displays a tiny icon to the left of the Author’s name.

title(): Title to display for this attachment, displays under the author.

titleLink(): Providing this makes the title clickable, pointing to this link.

titleLinkDownload(): When this is true, a download icon appears and clicking this saves the link to file.

imageUrl(): The image to display, will be “big” and easy to see.

audioUrl(): Audio file to play, only supports what html audio does.

videoUrl(): Video file to play, only supports what html video does.

fields(): An array of Attachment Field Objects.

[
    [
        'short' => false, // Whether this field should be a short field. Default: false
        'title' => 'Title 1', //The title of this field. Required
        'value' => 'Value 1' // The value of this field, displayed underneath the title value. Required
    ],
    [
        'short' => true,
        'title' => 'Title 2',
        'value' => 'Value 2'
    ],

];   

Sending messages without a notification

use TeamNiftyGmbh\RocketChatNotifications\Messages\RocketChatMessage;
use TeamNiftyGmbh\RocketChatNotifications\RocketChat

RocketChat::send('domain', 'token', 'userId', 'channel', RocketChatMessage::create('message'));

Sending messages via connection

connection must be defined in config/rocket-chat.php

use TeamNiftyGmbh\RocketChatNotifications\Messages\RocketChatMessage;
use TeamNiftyGmbh\RocketChatNotifications\RocketChat

RocketChat::sendVia('connection', 'channel', RocketChatMessage::create('message'));

Credits

License

The MIT License (MIT). Please see License File for more information.

You might also like...
Send SMS with easy using BEEM

beem-sms-api Send SMS with easy using BEEM Installation You must be using composer to be able to use this library. If composer 1.x is installed, make

Standalone PHP library for easy devices notifications push.

NotificationPusher Standalone PHP library for easy devices message notifications push. Feel free to contribute! Thanks. Contributors Cédric Dugat (Aut

Standalone PHP library for easy devices notifications push.

NotificationPusher Standalone PHP library for easy devices message notifications push. Feel free to contribute! Thanks. Contributors Cédric Dugat (Aut

Push Notifications using Laravel
Push Notifications using Laravel

laravel-push-notification Push Notifications using Laravel PushNotification::send(['deviceToken1', 'deviceToken2',..], 'Notification Message', 'Action

Laravel package to launch toast notifications.
Laravel package to launch toast notifications.

Laravel package to launch toast notifications. This package provides assistance when using toast notifications. Using the iziTOAST package, which allo

A package to simplify automating future notifications and reminders in Laravel
A package to simplify automating future notifications and reminders in Laravel

Laravel Snooze Schedule future notifications and reminders in Laravel Why use this package? Ever wanted to schedule a future notification to go out at

Laravel package to enable sending push notifications to devices

Laravel Push Notification Package to enable sending push notifications to devices Installation Update your composer.json file to include this package

Laravel SMS allows you to send SMS from your Laravel application using multiple sms providers, allow to add custom sms provider

Laravel SMS Laravel SMS allows you to send SMS from your Laravel application using multiple sms providers, allow to add custom sms provider Requiremen

Laravel Security Notifications

This package adds security notifications to warn your users when significant security events occur so that they aren't the next victim of an attacker.

Releases(v1.1.0)
Owner
Team Nifty GmbH
Leistungsstarke Software- & Weblösungen
Team Nifty GmbH
Larafirebase is a package thats offers you to send push notifications or custom messages via Firebase in Laravel.

Introduction Larafirebase is a package thats offers you to send push notifications or custom messages via Firebase in Laravel. Firebase Cloud Messagin

Kutia Software Company 264 Jan 7, 2023
This package allows you to send notifications to Microsoft Teams.

Teams connector This package allows you to send notifications to Microsoft Teams. Installation You can install the package using the Composer package

skrepr 20 Oct 4, 2022
Send Firebase push notifications with Laravel php framework.

FCM Notification Channel for Laravel Send Firebase push notifications with Laravel php framework. Installation You can install this package via compos

Ankur Kumar 23 Oct 31, 2022
:computer: Send notifications to your desktop directly from your PHP script

About JoliNotif JoliNotif is a cross-platform PHP library to display desktop notifications. It works on Linux, Windows or MacOS. Requires PHP >= 7.2 (

JoliCode 1.2k Dec 29, 2022
A PHP Library to easily send push notifications with the Pushwoosh REST Web Services.

php-pushwoosh A PHP Library to easily send push notifications with the Pushwoosh REST Web Services. First sample, creating a Pushwoosh message // Crea

gomoob 63 Sep 28, 2022
Notifications in PHP (notify-send, growl, etc) like that.

#Nod Notifications in PHP (notify-send, growl, etc) like that. ##Examples Letting Nod figure out the best Adapter to use (not recommend ATM, only work

Filipe Dobreira 51 Mar 26, 2019
Send push notifications to apple devices (iPhone, iPad, iPod).

Apple Apn Push Send push notifications to apple devices (iPhone, iPad, iPod). Support authenticators: Certificate Json Web Token Supported protocols:

Vitaliy Zhuk 157 Dec 1, 2022
WebPush can be used to send notifications to endpoints which server delivers Web Push

WebPush can be used to send notifications to endpoints which server delivers Web Push notifications as described in the Web Push protocol. As it is standardized, you don't have to worry about what server type it relies on.

null 1.5k Jan 7, 2023
Service that helps you to send notifications for a series of failed exceptions.

Laravel Failure Notifier This package helps you to track your exceptions and do what you want to do with them such as sending an SMS or and Email. You

Kamyar Gerami 7 Nov 26, 2022
It's Pimcore Bundle to send notifications to Google Chat, Slack or Email from admin panel inside Pimcore

Send notifications to Discord, Google Chat, Slack and more from Pimcore It's Pimcore Bundle to send notifications to Discord, Google Chat, Slack, Tele

LemonMind.com 6 Aug 31, 2022