Slack notification for Laravel as it should be. Easy, fast, simple and highly testable.

Overview

Build Status codecov Scrutinizer code quality Latest Stable Version PHP from Packagist Laravel Version Total Downloads License
Based on illuminate/mail

About Laravel Slack

Slack notification for Laravel as it should be. Easy, fast, simple and highly testable. Since it uses On-Demand Notifications, it requires Laravel 5.5 or higher.

Installation

Require this package in your composer.json and update your dependencies:

composer require gpressutto5/laravel-slack

Since this package supports Laravel's Package Auto-Discovery you don't need to manually register the ServiceProvider.

After that, publish the configuration file:

php artisan vendor:publish --provider="Pressutto\LaravelSlack\ServiceProvider"

You're gonna need to configure an "Incoming Webhook" integration for your Slack team.

Configuration

On the published configuration file config/laravel-slack.php you can change options like the Webhook URL, the default channel, the application name and the application image.

For security reasons you shouldn't commit your Webhook URL, so this package will, by default, use the environment variable SLACK_WEBHOOK_URL. You can just add it to your .env file. Like this:

SLACK_WEBHOOK_URL=https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX

Usage

You can send simple Slack messages like this:

  • Send message to a channel:
\Slack::to('#finance')->send('Hey, finance channel! A new order was created just now!');
  • Send message to an user:
\Slack::to('@joe')->send("Hey Joe! It looks like you've forgotten your password! Use this token to recover it: as34bhdfh");
  • Send message to multiple users:
\Slack::to(['@zoe', '@amy', '@mia'])->send('I swear, honey, you are the only one... :heart:');
//         ↑  look at this array  ↑
  • Mix it up:
\Slack::to('#universe', '@god', '#scientists')->send(':thinking_face:');
//         ↑ what? I don't need that array? ↑
  • No recipient:
\Slack::send('Default message to the default channel, set on config/laravel-slack.php.');
  • Send SlackMessage objects:
class HelloMessage extends SlackMessage
{
    public $content = "Hey bob, I'm a sending a custom SlackMessage";
    public $channel = '@bob';
}
\Slack::send(new SlackMessage());
  • Send to user:

    You can use any object as a recipient as long as they have the property slack_channel. If you are using Models you can just create the column slack_channel and store the @username or the #channel name there. If you already store it but on a different column you can create a method getSlackChannelAttribute.

class User extends Model
{
    public function getSlackChannelAttribute(): string
    {
        return $this->attributes['my_custom_slack_channel_column'];
    }
}
\Slack::to(User::where('verified', true))->send('Sending message to all verified users!');
  • Send message by specifying webhook:
\Slack::to('#finance')->webhook('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX')->send('Hey, finance channel! A new order was created just now!');

Testing

When testing you can easily mock the Slack service by calling Slack::fake() it will return a SlackFake object that won't send any message for real and will save them to an array. You can get this array by calling Slack::sentMessages().

This class also has some helper methods for you to use when testing:

  • Assert that at least one message with the content 'fake' was sent:
Slack::assertSent(function (SlackMessage $message) {
    return $message->content === 'fake';
});
  • Assert that at least two messages with the content being a string longer than 5 characters were sent:
Slack::assertSent(function (SlackMessage $message) {
    return strlen($message->content) >= 100;
}, 2);
  • Assert that exactly five messages where the content content contains the word 'test' were sent:
Slack::assertSent(function (SlackMessage $message) {
    return strpos($message->content, 'test') !== false;
}, 5, true);
  • Assert that exactly three messages were sent:
Slack::assertSentCount(3);

Since this package uses illuminate/notifications to send notifications you can mock the Notification service instead of the Slack one and use the class NotificationFake in your tests. Take a look.

Comments
  • Laravel 9.x Compatibility

    Laravel 9.x Compatibility

    This is an automated pull request from Shift to update your package code and dependencies to be compatible with Laravel 9.x.

    Before merging, you need to:

    • Checkout the l9-compatibility branch
    • Review all comments for additional changes
    • Thoroughly test your package

    If you do find an issue, please report it by commenting on this PR to help improve future automation.

    opened by laravel-shift 7
  • Laravel 6.x - Support

    Laravel 6.x - Support

    First of all: I love the package, works like a charm for a while now! :) I was trying to update our application to laravel 6.x recently and got the following issue:

    Problem 1 - Conclusion: remove gpressutto5/laravel-slack 1.1.0 - Conclusion: don't install gpressutto5/laravel-slack 1.1.0 - Conclusion: don't install gpressutto5/laravel-slack 1.0.1 - Conclusion: don't install laravel/framework v6.1.0 - Conclusion: don't install laravel/framework v6.0.4 - Conclusion: don't install laravel/framework v6.0.3 - Conclusion: don't install laravel/framework v6.0.2 - Conclusion: don't install laravel/framework v6.0.1 - Installation request for gpressutto5/laravel-slack ^1.0 -> satisfiable by gpressutto5/laravel-slack[1.0.0, 1.0.1, 1.1.0]. - Conclusion: don't install laravel/framework v6.0.0

    Is there any chance in getting some support for Laravel 6.x?

    opened by fatelgit 7
  • Allow user specify webhook for instance

    Allow user specify webhook for instance

    This feature is in response to issue #16 User can now call webhook method on Slack class or facade to specify webhook to use to send a message. Also, removed config array from been required, since default config is also set. We simply take new config keys specified by user and use the default when not specified. Helps write shorter code that way e.g Slack::to('#scientists')->webhook('https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/x00XXXXXXXXX')->send(':thinking_face:');

    opened by djunehor 3
  • As of L5.7 class SlackMessage is no longer available

    As of L5.7 class SlackMessage is no longer available

    As of L5.7 the class Illuminate\Notifications\Messages\SlackMessage is no longer available. I suggest adding a note about this class is now a separated package that must be install.

    Thanks

    Screen Shot 2019-04-11 at 5 21 19 PM
    opened by ralphmoran 3
  • Ability to configure multiple webhook URLs

    Ability to configure multiple webhook URLs

    Given that Slack only allows you to send to one channel (or user) per webhook, this package needs the ability to configure multiple webhook URLs, so that I can send a slack message to #my-channel or @my-user using one package.

    See #7 here which is related. It also seems like this has been solved by @tigran-cl in https://github.com/tigran-cl/laravel-slack but there is no pull request. @gpressutto5 would be great to integrate that into this package.

    opened by connecteev 2
  • Laravel 5.8 support

    Laravel 5.8 support

    As I said in #13, we need to require laravel/slack-notification-channel@^2.0 for Laravel 5.8 support, but it will break <5.8 support. It would be great if we could specify that this package requires:

    illuminate/support >= 5.5 < 5.7
    OR
    illuminate/support ~5.7.28 AND laravel/slack-notification-channel ^1.0
    OR
    illuminate/support ^5.8 AND laravel/slack-notification-channel ^2.0
    
    opened by gpressutto5 2
  • Broken on Laravel 5.7?

    Broken on Laravel 5.7?

    Hi, I'm using this package to send notifications from my application to a dedicated slack-channel for a while now. After updating Laravel to 5.7 these notifications does not seems to work anymore. I get a message like: AH01071: Got error 'PHP message: PHP Fatal error: Allowed memory size of 1073741824 bytes exhausted

    Anyone else has this message?

    Greetz, Wouter

    opened by Datalink-Wouter 2
  • Add Lumen support

    Add Lumen support

    I found that I could get this to work within Lumen (5.7.*) by requiring Ramsey's UUID package and ensuring that the constructor of the Slack class utilises the Notification facade

    This has not been regression tested in Laravel yet.

    opened by danlake 2
  • Cannot seem to get

    Cannot seem to get "application_image' to work

    First, I love this package! As far as I can tell it seems to work great. But I cannot seem to get the 'application_image' to work. I'm not sure what I'm doing wrong.

    The image is stored in: <docroot>/public/images/logos/main-logo.png

    and I'm setting the config value as follows: 'application_image' => '/images/logos/main-logo.png'

    I must be missing something, but don't know what.

    TIA, -martin.

    opened by mlanser 2
  • Update config doc block

    Update config doc block

    This PR updates the doc block of the config file. It fixes an issue where the application_image key was referenced as Application Name instead of Application Image.

    opened by xabou 2
  • Send message from commands laravel

    Send message from commands laravel

    Class 'Slack' not found

    at app/Console/Commands/CalendarUpdate.php:157

    157| \Slack::to('#ridgepointsystem')->send('Data for calendar has updated.'); 158| 159| }else{ 160| \Slack::to('#ridgepointsystem')->send('Data update for calendar has STOP'); 161| }

    opened by Hasiojjosiah 1
  • Test on more PHP versions

    Test on more PHP versions

    Maybe testing on the earliest and the latest PHP versions supported by the package is enough. In which case we should do the same for laravel versions, to make sure it works on all versions.

    opened by gpressutto5 0
  • Sending to multiple channels from one webhook URL

    Sending to multiple channels from one webhook URL

    Hi there, awesome job on the package, have a question for you.. In your examples you are able to send to different slack channels, for me, one webhook url, only allows me to send to a single channel. Specifying the channel in the to() function has no effect for me, it still sends it to the channel associated with the webhook url.

    So I guess my question is, how did you generate your webhook url on slack so that you are able to send to multiple channels using a single webhook url? It doesn't seem possible from what I've found on google, it seems like you need a unique webhook url for each channel.

    opened by tigran-cl 7
Releases(2.3.0)
Owner
Guilherme Pressutto
Foodtech entrepreneur, developer, poker player, video capturing and editing lover and future pilot of my future plane.
Guilherme Pressutto
@Authy notification channel for @Laravel, with the ability to send in-app, sms, and call verification tokens.

Authy Notification Channel for Laravel Authy notification channel for Laravel, with the ability to send in-app, sms, and call verification tokens. Tab

Laravel Notification Channels 57 Dec 19, 2022
Multiple channels of laravel exception notification(DingTalk、FeiShu、ServerChan、WeWork、XiZhi). - 多种通道的 laravel 异常通知(钉钉群机器人、飞书群机器人、Server 酱、企业微信群机器人、息知)。

laravel-exception-notify 简体中文 | ENGLISH Multiple channels of laravel exception notification(DingTalk、FeiShu、ServerChan、WeWork、XiZhi). - 多种通道的 laravel

guanguans 61 Dec 8, 2022
Google Chat - Laravel Notification Channel

Google Chat - Laravel Notification Channel This package makes it easy to send notifications using Google Chat , (formerly known as Hangouts Chat) with

Laravel Notification Channels 36 Dec 6, 2022
Laravel SMS Notification Channel

Laravel SMS Notification Channel Installation composer require guangda/laravel-sms-notification-channel env 配置 SMS_PROVIDER=yunpian SMS_SIGNATURE=【签名

Guangda 6 Dec 29, 2021
A Toast notification library for the Laravel TALL stack.

A Toast notification library for the Laravel TALL stack. You can push notifications from the backend or frontend to render customizable toasts with almost zero footprint on the published CSS/JS ????

John F 365 Jan 7, 2023
Lark Notification Channel for laravel.

Lark notifications channel for Laravel This package makes it easy to send Lark using the Laravel notification system. Supports 5.5+, 6.x, 7.x and 8.x.

null 1 Nov 3, 2021
AmaranJS stylish notification for your laravel application.

AmaranJS Laravel 5 Package AmaranJS L5 package is a Laravel wrapper for my jquery plugin AmaranJS.You can create easy and stylish notifications with A

Hakan ERSU 24 Oct 3, 2019
Our Laravel Sendgrid Notification package

laravel-sendgrid-notification-channel Laravel Sendgrid Notification channel Installation To get started, you need to require this package: composer re

Konstruktiv B.V. 4 Feb 3, 2022
Lara-Izitoast : Laravel Notification Package

Lara-Izitoast : Laravel Notification Package This is a laravel notification wrapper build with http://izitoast.marcelodolce.com javascript library. Ho

Apps:Lab KE 34 Nov 19, 2022
Notification package for Laravel

Package is looking for maintainers Please contact me if interested. Notification package for Laravel4 / Laravel5 A simple notification management pack

Edvinas Kručas 531 Oct 12, 2022
An SMS notification channel for the PHP framework Laravel.

Laravel SMS notification channel An SMS notification channel for the PHP framework Laravel. Supported SMS gateways: 46elks Cellsynt Telenor SMS Pro Tw

Andreas 2 Jan 22, 2022
Laravel notification manager

Easily manage notifications and notification subscriptions in your Laravel application.

Rubik 11 Aug 10, 2022
Laravel FCM (Firebase Cloud Messaging) Notification Channel

Laravel FCM Notification Laravel FCM (Firebase Cloud Messaging) Notification Channel Use this package to send push notifications via Laravel to Fireba

Vishal Dudhatra 3 Jul 3, 2022
Livewire Package to display Toast Notification based on TALL Stack.

livewire-toast Livewire Package to display Toast Notification based on TALL Stack. Requirements Make sure that Livewire is installed properly on your

AscSoftwares 35 Nov 12, 2022
ApnsPHP: Apple Push Notification & Feedback Provider

ApnsPHP: Apple Push Notification & Feedback Provider A full set of open source PHP classes to interact with the Apple Push Notification service for th

Immobiliare Labs 1.4k Nov 16, 2022
A filterable git commit summary notification mailer

Git commit notification A symfony application to allow receiving commit notification for all commits in a certain time period. Features: Receive one m

123inkt 4 Jan 3, 2023
Bootstrap 4 & 5 replacement for jGrowl notification

XoopsGrowl module for XOOPS CMS 2.5.11+ XoopsGrowl is a module for XOOPS CMS to configure an alternative to the jGrowl notification using Bootstrap Al

null 1 Oct 28, 2021
Bootstrap 4 & 5 replacement for jGrowl notification

XoopsGrowl module for XOOPS CMS 2.5.11+ XoopsGrowl is a module for XOOPS CMS to configure an alternative to the jGrowl notification using Bootstrap Al

null 1 Oct 24, 2021
WordPress Notification plugin

Notification This is the public repository for Notification - the WordPress plugin. This plugin allow you to send custom notifications about various e

BracketSpace 157 Nov 28, 2022