Code Monkey home page Code Monkey logo

laravel-slack's Introduction

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:

  • 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.

laravel-slack's People

Contributors

cfuentessalgado avatar danlake avatar djunehor avatar gpressutto5 avatar laravel-shift avatar stylecibot avatar ttrig avatar xabou avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-slack's Issues

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.

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| }

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

Lumen?

Can we get this on Lumen?

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

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.

Laravel 10 Support

Is there any support for the Illuminate Laravel 10 packages planned? A fork? I'm trying to workaround upgrading my Laravel app from Laravel 9 to 10.

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.

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?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.