Git Product home page Git Product logo

laravel-quick-slack's Introduction

Laravel QuickSlack

A simple package to quickly send messages to Slack channels through Laravel. Tested with Laravel 5.4 through 8.

Installation

Use Composer to install the package:

composer require "joelhinz/laravel-quick-slack=0.*"

This package supports auto-discovery, so if you're using Laravel 5.5, you're all set.

For Laravel 5.4, or if you don't want to use auto-discovery, add the provider and optionally the facade alias to your config/app.php file:

'providers' => [
    // ...
    JoelHinz\LaravelQuickSlack\ServiceProvider::class,
],

'aliases' => [
    // ...
    'QuickSlack' => JoelHinz\LaravelQuickSlack\Facade::class,
],

Basic usage

Before sending messages, all you need to do is go to Slack and create an incoming webhook, then copy the webhook url.

use QuickSlack;

QuickSlack::to($webhook)->send('My hovercraft is full of eels.');

The webhook url can be remembered for subsequent calls during the same script execution by passing a boolean as the second argument to the to() method:

# Remember webhook for next call
QuickSlack::to($webhook, true)->send('My nipples explode with delight!');

# No need for the to() method this time
QuickSlack::send('I cannot wait till lunchtime.');

# Set a new webhook - the new webhook will now be remembered instead
QuickSlack::to($webhook)->send('Drop your panties, Sir William!');

# Set a new webhook but stop remembering
QuickSlack::to($webhook, false)->send('Bouncy-bouncy');

# Stop remembering without sending a message
QuickSlack::forgetRecipient();

Since QuickSlack is fluent, you can just keep chaining more messages if you want to. Please note that this requires a remembered webhook url, or a default webhook (see configuration options below).

QuickSlack::send('first message')->send('second message')->send('third message');

Don't like how long the webhook urls get? No worries, you can just skip the first part of them. Instead of https://hooks.slack.com/services/[rest of url], just enter everything after services/ instead. QuickSlack will handle the rest automatically.

Configuration

QuickSlack can be used without any configuration, but you can export the configuration to get features such as a default webhook and named webhooks.

php artisan vendor:publish --provider="JoelHinz\LaravelQuickSlack\ServiceProvider"

This will create a file config/quick-slack.php where you can set your configuration options as follows:

<?php

return [
    /**
     * Set names for your webhooks.
     */
    'webhooks' => [
        'my-webhook' => 'some-webhook-url',
    ],

    /**
     * Set a default webhook to use if no other url is given explicitly.
     * This can be either a webhook url, or the name of a named webhook above.
     */
    'default' => '',
];

Again, you can enter either full urls or just the webhook-specific part of them.

By using named webhooks, you don't have to remember their full addresses when you send messages:

QuickSlack::to('my-webhook')->send('I will not buy this record, it is scratched.');

If you only or mainly send to just one endpoint, you can put that as the default and omit the to() method. The default value can be overwritten by using the to() method, and the overwriting url can be remembered as per above.

Webhook names are recursive, so that you can use different names for the same endpoint:

'webhooks' => [
    'email bounces' => 'https://hooks.slack.com/services/...',
    'email complaints' => 'email bounces',
]

They are also nestable if you prefer to organise things that way:

'webhooks' => [
    'email' => [
        'bounces' => 'https://hooks.slack.com/services/...',
        'complaints' => 'email.bounces',
    ]
]

Webhook precedence

When sending a message, QuickSlack will determine which endpoint to use in the following order of precedence:

  1. The webhook given in the last to() call if no message has been sent since then.
  2. The webhook given in the last to() call if the remember option is set to true.
  3. The default webhook in the configuration file.

TODO

Planned functionality includes

  • Automated tests and Travis integration
  • Style CI integration
  • Support for custom emojis and usernames
  • Support for changing channel ad-hoc
  • Support for saved teams in the configuration
  • Support for additional styling as per Slack's payload documentation
  • Custom response and exception handling
  • Global helper quickslack($message[, $endpoint])

License

Standard MIT License (MIT), available in LICENSE.

laravel-quick-slack's People

Contributors

joelhinz avatar

Watchers

 avatar

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.