项目作者: spatie

项目描述 :
Send interactive Slack notifications in Laravel apps
高级语言: PHP
项目地址: git://github.com/spatie/interactive-slack-notification-channel.git
创建时间: 2021-01-20T19:14:47Z
项目社区:https://github.com/spatie/interactive-slack-notification-channel

开源协议:MIT License

下载


Send interactive Slack notifications in Laravel apps

Latest Version on Packagist
Tests
Total Downloads

This package allows you to send interactive Slack notifications. Here’s how such a notification could look like

Support us

We invest a lot of resources into creating best in class open source packages. You can
support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
You’ll find our address on our contact page. We publish all received postcards
on our virtual postcard wall.

Installation

You can install the package via composer:

  1. composer require spatie/interactive-slack-notification-channel

Usage

In your Notifiable classes you should add a method named routeNotificationForInteractiveSlack that returns an array with the
API token, an optionally the channel name

  1. public function routeNotificationForInteractiveSlack()
  2. {
  3. return [
  4. 'token' => 'xoxp-slack-token',
  5. 'channel' => '#general' // this is optional
  6. ];
  7. }

Replying to message threads

Let’s assume you want your application to send a Slack notification when an order gets placed. You also want any
subsequent messages about the order be place in the same thread.

Using the SlackApi channels you can retrieve the API response from Slack’s chat.postMessage method. With this response
you could post messages on other events that happen on the order, such as order paid, shipped, closed, etc.

Here’s an example:

  1. use Spatie\InteractiveSlackNotificationChannel\Messages\SlackMessage
  2. public function toInteractiveSlack($notifiable)
  3. {
  4. return (new SlackMessage)->content('A new order has been placed');
  5. }
  6. public function interactiveSlackResponse(array $response)
  7. {
  8. $this->order->update(['slack_thread_ts' => $response['ts']]);
  9. }

In your order paid event you can have

  1. use Spatie\InteractiveSlackNotificationChannel\Messages\SlackMessage;
  2. use Spatie\InteractiveSlackNotificationChannel\Messages\SlackAttachment;
  3. public function toInteractiveSlack($notifiable)
  4. {
  5. $order = $this->order;
  6. return (new SlackMessage)
  7. ->success()
  8. ->content('Order paid')
  9. ->threadTimestamp($order->slack_thread_ts)
  10. ->attachment(function(SlackAttachment $attachment) use ($order) {
  11. $attachment
  12. ->title("Order $order->reference has been paid for.")
  13. ->content('Should now be processed.')
  14. ->action('View Order', route('orders', $order->reference));
  15. });
  16. }

Testing

  1. composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

Some parts of the code and readme are based on this package.

License

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