Git Product home page Git Product logo

slack-notification-channel's Introduction

Laravel Slack API Token Notification Channel

Latest Version on Packagist GitHub Workflow Status Quality Score Total Downloads

This is the Laravel Slack notification channel, but instead of using incoming webhooks, this channel makes use of OAuth access tokens. It also allows replies to thread messages.

https://phppackagedevelopment.com

If you want to learn how to create reusable PHP packages yourself, take a look at my upcoming PHP Package Development video course.

Usage

Install the package via composer:

composer require beyondcode/slack-notification-channel

The service provider gets registered automatically and you can use this package as a replacement of the core Laravel Slack notification channel.

Notification Routing

Since this notification channel makes use of Slack API tokens instead of incoming webhook URLs, you need to return an array containing the API token and an optional channel. This channel will be used, if it is not provided in the SlackMessage that you send:

public function routeNotificationForSlack()
{
    return [
        'token' => 'xoxp-slack-token',
        'channel' => '#general'
    ];
}

Replying to Message Threads

Assuming you want to keep track of orders and have your team/bot respond to a single thread of per order placed, this channel allows you to retrieve the API response from the chat.postMessage method inside your notifications. With this you could post messages on order paid, shipped, closed, etc. events to the same thread.

In your order placed event you can have

public function toSlack($notifiable)
{
    return (new SlackMessage)
        ->content('A new order has been placed');
}

public function response($response)
{
    $response = $response->getBody()->getContents();
    $this->order->data('slack.thread_ts', json_decode($response, true)['ts']);
}

And in your order paid event you can have

public function toSlack($notifiable)
{
    $order = $this->order;
    return (new SlackMessage)
        ->success()
        ->content('Order paid')
        ->threadTimestamp($order->data('slack.thread_ts'))
           ->attachment(function ($attachment) use ($order) {
               $attachment->title("Order $order->reference has been paid for.")
                          ->content('Should now be processed.')
                          ->action('View Order', route('orders', $order->reference));
           });
}

Customizing the channel name

Laravel ships with a slack notification channel which uses web hooks. This packages overwrites that default slack channel.

Should you want to use Laravel's default Slack channel and this one inside one app, you'll need to use a different channel name.

You can set the channel name using

SlackApiChannel::$channelName = 'alternativeSlackChannel'

Make sure you use the right method name on your notifications.

class AlternativeSlackChannelNameNotification extends Notification
{
    public function toAlternativeSlackChannel($notifiable) {
        // ...
    }
}

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

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

slack-notification-channel's People

Contributors

augustusnaz avatar chrysanthos avatar ecomail-cz avatar freekmurze avatar matthewnessworthy avatar mpociot avatar sweptsquash avatar tlapi 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.