gpressutto5 / laravel-slack

:hash: Slack notification for Laravel as it should be. Easy, fast, simple and highly testable.
MIT License
289 stars 41 forks source link
laravel php slack testable

Build Status codecov 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.

This library is archived and no longer maintained. It works as expected, but I don't have time to maintain it anymore. As a last update, I've removed version constraints from the composer.json file, so you can use it with any future Laravel versions. Feel free to fork it and use it as you wish.

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:

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

Slack::assertSent(function (SlackMessage $message) {
    return $message->content === 'fake';
});
Slack::assertSent(function (SlackMessage $message) {
    return strlen($message->content) >= 100;
}, 2);
Slack::assertSent(function (SlackMessage $message) {
    return strpos($message->content, 'test') !== false;
}, 5, true);
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.