├── LICENSE.md ├── composer.json ├── readme.md └── src ├── Channels └── SlackApiChannel.php ├── Messages ├── SlackAttachment.php ├── SlackAttachmentBlock.php ├── SlackAttachmentDividerBlock.php ├── SlackAttachmentField.php └── SlackMessage.php └── SlackChannelServiceProvider.php /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Taylor Otwell 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "beyondcode/slack-notification-channel", 3 | "description": "Slack Notification Channel for Laravel using API tokens.", 4 | "keywords": ["laravel", "notifications", "slack"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Marcel Pociot", 9 | "email": "marcel@beyondco.de" 10 | } 11 | ], 12 | "require": { 13 | "php": "^7.3.0|^8.0|^8.1", 14 | "guzzlehttp/guzzle": "^6.0|^7.0", 15 | "illuminate/notifications": "^6.0|^7.0|^8.0|^9.0|^10.0" 16 | }, 17 | "require-dev": { 18 | "mockery/mockery": "^1.3", 19 | "phpunit/phpunit": "^8.0|^9.0|^10.0" 20 | }, 21 | "autoload": { 22 | "psr-4": { 23 | "BeyondCode\\SlackNotificationChannel\\": "src/" 24 | } 25 | }, 26 | "autoload-dev": { 27 | "psr-4": { 28 | "BeyondCode\\SlackNotificationChannel\\Tests\\": "tests/" 29 | } 30 | }, 31 | "config": { 32 | "sort-packages": true 33 | }, 34 | "extra": { 35 | "branch-alias": { 36 | "dev-master": "2.0-dev" 37 | }, 38 | "laravel": { 39 | "providers": [ 40 | "BeyondCode\\SlackNotificationChannel\\SlackChannelServiceProvider" 41 | ] 42 | } 43 | }, 44 | "minimum-stability": "dev", 45 | "prefer-stable": true 46 | } 47 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Laravel Slack API Token Notification Channel 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/beyondcode/slack-notification-channel.svg?style=flat-square)](https://packagist.org/packages/beyondcode/slack-notification-channel) 4 | ![GitHub Workflow Status](https://img.shields.io/github/workflow/status/beyondcode/slack-notification-channel/run-tests?label=tests) 5 | [![Quality Score](https://img.shields.io/scrutinizer/g/beyondcode/slack-notification-channel.svg?style=flat-square)](https://scrutinizer-ci.com/g/beyondcode/slack-notification-channel) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/beyondcode/slack-notification-channel.svg?style=flat-square)](https://packagist.org/packages/beyondcode/slack-notification-channel) 7 | 8 | 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. 9 | 10 | 11 | [![https://phppackagedevelopment.com](https://beyondco.de/courses/phppd.jpg)](https://phppackagedevelopment.com) 12 | 13 | If you want to learn how to create reusable PHP packages yourself, take a look at my upcoming [PHP Package Development](https://phppackagedevelopment.com) video course. 14 | 15 | ### Usage 16 | 17 | Install the package via composer: 18 | 19 | ``` 20 | composer require beyondcode/slack-notification-channel 21 | ``` 22 | 23 | The service provider gets registered automatically and you can use this package as a replacement of the core Laravel Slack notification channel. 24 | 25 | ### Notification Routing 26 | 27 | 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. 28 | This channel will be used, if it is not provided in the `SlackMessage` that you send: 29 | 30 | ```php 31 | public function routeNotificationForSlack() 32 | { 33 | return [ 34 | 'token' => 'xoxp-slack-token', 35 | 'channel' => '#general' 36 | ]; 37 | } 38 | ``` 39 | 40 | ### Replying to Message Threads 41 | 42 | 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. 43 | 44 | In your order placed event you can have 45 | 46 | ```php 47 | public function toSlack($notifiable) 48 | { 49 | return (new SlackMessage) 50 | ->content('A new order has been placed'); 51 | } 52 | 53 | public function response($response) 54 | { 55 | $response = $response->getBody()->getContents(); 56 | $this->order->data('slack.thread_ts', json_decode($response, true)['ts']); 57 | } 58 | ``` 59 | 60 | And in your order paid event you can have 61 | 62 | ```php 63 | public function toSlack($notifiable) 64 | { 65 | $order = $this->order; 66 | return (new SlackMessage) 67 | ->success() 68 | ->content('Order paid') 69 | ->threadTimestamp($order->data('slack.thread_ts')) 70 | ->attachment(function ($attachment) use ($order) { 71 | $attachment->title("Order $order->reference has been paid for.") 72 | ->content('Should now be processed.') 73 | ->action('View Order', route('orders', $order->reference)); 74 | }); 75 | } 76 | ``` 77 | 78 | ## Customizing the channel name 79 | 80 | Laravel ships with a slack notification channel which uses web hooks. This packages overwrites that default slack channel. 81 | 82 | 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. 83 | 84 | You can set the channel name using 85 | 86 | ```php 87 | SlackApiChannel::$channelName = 'alternativeSlackChannel' 88 | ``` 89 | 90 | Make sure you use the right method name on your notifications. 91 | 92 | ```php 93 | class AlternativeSlackChannelNameNotification extends Notification 94 | { 95 | public function toAlternativeSlackChannel($notifiable) { 96 | // ... 97 | } 98 | } 99 | ``` 100 | 101 | 102 | ### Changelog 103 | 104 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 105 | 106 | ## Contributing 107 | 108 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 109 | 110 | ### Security 111 | 112 | If you discover any security related issues, please email marcel@beyondco.de instead of using the issue tracker. 113 | 114 | ## Credits 115 | 116 | - [Marcel Pociot](https://github.com/mpociot) 117 | - [All Contributors](../../contributors) 118 | 119 | ## License 120 | 121 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 122 | -------------------------------------------------------------------------------- /src/Channels/SlackApiChannel.php: -------------------------------------------------------------------------------- 1 | http = $http; 41 | 42 | $this->token = null; 43 | } 44 | 45 | public static function channelDriverName(string $channelDriverName) 46 | { 47 | self::$channelDriverName = $channelDriverName; 48 | } 49 | 50 | /** 51 | * Send the given notification. 52 | * 53 | * @param mixed $notifiable 54 | * @param \Illuminate\Notifications\Notification $notification 55 | * @return \Psr\Http\Message\ResponseInterface|null 56 | */ 57 | public function send($notifiable, Notification $notification) 58 | { 59 | if (! $config = $notifiable->routeNotificationFor(self::$channelDriverName, $notification)) { 60 | return; 61 | } 62 | 63 | $this->token = $config['token']; 64 | $this->channel = $config['channel'] ?? null; 65 | 66 | 67 | $methodName = 'to' . ucfirst(self::$channelDriverName); 68 | 69 | $response = $this->http->post( 70 | self::API_ENDPOINT, 71 | $this->buildJsonPayload( 72 | 73 | $notification->$methodName($notifiable) 74 | ) 75 | ); 76 | 77 | if(method_exists($notification, 'response')){ 78 | return $notification->response($response); 79 | } 80 | 81 | return $response; 82 | } 83 | 84 | /** 85 | * Build up a JSON payload for the Slack webhook. 86 | * 87 | * @param \Illuminate\Notifications\Messages\SlackMessage $message 88 | * @return array 89 | */ 90 | protected function buildJsonPayload(SlackMessage $message) 91 | { 92 | $optionalFields = array_filter([ 93 | 'channel' => data_get($message, 'channel', $this->channel), 94 | 'icon_emoji' => data_get($message, 'icon'), 95 | 'icon_url' => data_get($message, 'image'), 96 | 'link_names' => data_get($message, 'linkNames'), 97 | 'unfurl_links' => data_get($message, 'unfurlLinks'), 98 | 'unfurl_media' => data_get($message, 'unfurlMedia'), 99 | 'username' => data_get($message, 'username'), 100 | 'thread_ts' => data_get($message, 'threadTimestamp'), 101 | 'reply_broadcast' => data_get($message, 'threadBroadcast') 102 | ]); 103 | 104 | $payload = [ 105 | 'json' => array_merge([ 106 | 'text' => $message->content, 107 | 'attachments' => $this->attachments($message), 108 | ], $optionalFields), 109 | ]; 110 | 111 | 112 | $payload['headers'] = [ 113 | 'Content-type' => 'application/json', 114 | 'Authorization' => 'Bearer '.$this->token, 115 | ]; 116 | 117 | return array_merge($payload, $message->http); 118 | } 119 | 120 | /** 121 | * Format the message's attachments. 122 | * 123 | * @param \Illuminate\Notifications\Messages\SlackMessage $message 124 | * @return array 125 | */ 126 | protected function attachments(SlackMessage $message) 127 | { 128 | return collect($message->attachments)->map(function ($attachment) use ($message) { 129 | return array_filter([ 130 | 'actions' => $attachment->actions, 131 | 'author_icon' => $attachment->authorIcon, 132 | 'author_link' => $attachment->authorLink, 133 | 'author_name' => $attachment->authorName, 134 | 'blocks' => $this->blocks($attachment), 135 | 'color' => $attachment->color ?: $message->color(), 136 | 'callback_id' => $attachment->callbackId, 137 | 'fallback' => $attachment->fallback, 138 | 'fields' => $this->fields($attachment), 139 | 'footer' => $attachment->footer, 140 | 'footer_icon' => $attachment->footerIcon, 141 | 'image_url' => $attachment->imageUrl, 142 | 'mrkdwn_in' => $attachment->markdown, 143 | 'pretext' => $attachment->pretext, 144 | 'text' => $attachment->content, 145 | 'thumb_url' => $attachment->thumbUrl, 146 | 'title' => $attachment->title, 147 | 'title_link' => $attachment->url, 148 | 'ts' => $attachment->timestamp, 149 | ]); 150 | })->all(); 151 | } 152 | 153 | /** 154 | * Format the attachment's fields. 155 | * 156 | * @param \BeyondCode\SlackNotificationChannel\Messages\SlackAttachment $attachment 157 | * @return array 158 | */ 159 | protected function fields(SlackAttachment $attachment) 160 | { 161 | return collect($attachment->fields)->map(function ($value, $key) { 162 | if ($value instanceof SlackAttachmentField) { 163 | return $value->toArray(); 164 | } 165 | 166 | return ['title' => $key, 'value' => $value, 'short' => true]; 167 | })->values()->all(); 168 | } 169 | 170 | /** 171 | * Format the attachment's blocks. 172 | * 173 | * @param \BeyondCode\SlackNotificationChannel\Messages\SlackAttachment $attachment 174 | * @return array 175 | */ 176 | protected function blocks(SlackAttachment $attachment) 177 | { 178 | return collect($attachment->blocks)->map(function ($value) { 179 | return $value->toArray(); 180 | })->values()->all(); 181 | } 182 | } 183 | -------------------------------------------------------------------------------- /src/Messages/SlackAttachment.php: -------------------------------------------------------------------------------- 1 | title = $title; 155 | $this->url = $url; 156 | 157 | return $this; 158 | } 159 | 160 | /** 161 | * Set the pretext of the attachment. 162 | * 163 | * @param string $pretext 164 | * @return $this 165 | */ 166 | public function pretext($pretext) 167 | { 168 | $this->pretext = $pretext; 169 | 170 | return $this; 171 | } 172 | 173 | /** 174 | * Set the content (text) of the attachment. 175 | * 176 | * @param string $content 177 | * @return $this 178 | */ 179 | public function content($content) 180 | { 181 | $this->content = $content; 182 | 183 | return $this; 184 | } 185 | 186 | /** 187 | * A plain-text summary of the attachment. 188 | * 189 | * @param string $fallback 190 | * @return $this 191 | */ 192 | public function fallback($fallback) 193 | { 194 | $this->fallback = $fallback; 195 | 196 | return $this; 197 | } 198 | 199 | /** 200 | * Set the color of the attachment. 201 | * 202 | * @param string $color 203 | * @return $this 204 | */ 205 | public function color($color) 206 | { 207 | $this->color = $color; 208 | 209 | return $this; 210 | } 211 | 212 | /** 213 | * Set the color of the attachment. 214 | * 215 | * @param string $callbackId 216 | * @return $this 217 | */ 218 | public function callbackId($callbackId) 219 | { 220 | $this->callbackId = $callbackId; 221 | 222 | return $this; 223 | } 224 | 225 | /** 226 | * Add a field to the attachment. 227 | * 228 | * @param \Closure|string $title 229 | * @param string $content 230 | * @return $this 231 | */ 232 | public function field($title, $content = '') 233 | { 234 | if (is_callable($title)) { 235 | $callback = $title; 236 | 237 | $callback($attachmentField = new SlackAttachmentField); 238 | 239 | $this->fields[] = $attachmentField; 240 | 241 | return $this; 242 | } 243 | 244 | $this->fields[$title] = $content; 245 | 246 | return $this; 247 | } 248 | 249 | /** 250 | * Set the fields of the attachment. 251 | * 252 | * @param array $fields 253 | * @return $this 254 | */ 255 | public function fields(array $fields) 256 | { 257 | $this->fields = $fields; 258 | 259 | return $this; 260 | } 261 | 262 | /** 263 | * Set the fields containing markdown. 264 | * 265 | * @param array $fields 266 | * @return $this 267 | */ 268 | public function markdown(array $fields) 269 | { 270 | $this->markdown = $fields; 271 | 272 | return $this; 273 | } 274 | 275 | /** 276 | * Set the image URL. 277 | * 278 | * @param string $url 279 | * @return $this 280 | */ 281 | public function image($url) 282 | { 283 | $this->imageUrl = $url; 284 | 285 | return $this; 286 | } 287 | 288 | /** 289 | * Set the URL to the attachment thumbnail. 290 | * 291 | * @param string $url 292 | * @return $this 293 | */ 294 | public function thumb($url) 295 | { 296 | $this->thumbUrl = $url; 297 | 298 | return $this; 299 | } 300 | 301 | /** 302 | * Add an action (button) under the attachment. 303 | * 304 | * @param string $title 305 | * @param string $url 306 | * @param string $style 307 | * @return $this 308 | */ 309 | public function action($title, $url, $style = '') 310 | { 311 | $this->actions[] = [ 312 | 'type' => 'button', 313 | 'text' => $title, 314 | 'url' => $url, 315 | 'style' => $style, 316 | ]; 317 | 318 | return $this; 319 | } 320 | 321 | public function button($title, $name, $value, $style = '') 322 | { 323 | $this->actions[] = [ 324 | 'type' => 'button', 325 | 'text' => $title, 326 | 'name' => $name, 327 | 'value' => $value, 328 | 'style' => $style, 329 | ]; 330 | 331 | return $this; 332 | } 333 | 334 | public function select($title, $name, array $options, $style = '') 335 | { 336 | $this->actions[] = [ 337 | 'type' => 'select', 338 | 'text' => $title, 339 | 'name' => $name, 340 | 'options' => $options, 341 | 'style' => $style, 342 | ]; 343 | 344 | return $this; 345 | } 346 | 347 | /** 348 | * Set the author of the attachment. 349 | * 350 | * @param string $name 351 | * @param string|null $link 352 | * @param string|null $icon 353 | * @return $this 354 | */ 355 | public function author($name, $link = null, $icon = null) 356 | { 357 | $this->authorName = $name; 358 | $this->authorLink = $link; 359 | $this->authorIcon = $icon; 360 | 361 | return $this; 362 | } 363 | 364 | /** 365 | * Set the footer content. 366 | * 367 | * @param string $footer 368 | * @return $this 369 | */ 370 | public function footer($footer) 371 | { 372 | $this->footer = $footer; 373 | 374 | return $this; 375 | } 376 | 377 | /** 378 | * Set the footer icon. 379 | * 380 | * @param string $icon 381 | * @return $this 382 | */ 383 | public function footerIcon($icon) 384 | { 385 | $this->footerIcon = $icon; 386 | 387 | return $this; 388 | } 389 | 390 | /** 391 | * Set the timestamp. 392 | * 393 | * @param \DateTimeInterface|\DateInterval|int $timestamp 394 | * @return $this 395 | */ 396 | public function timestamp($timestamp) 397 | { 398 | $this->timestamp = $this->availableAt($timestamp); 399 | 400 | return $this; 401 | } 402 | 403 | /** 404 | * Add a block to the attachment. 405 | * 406 | * @param \Closure $callback 407 | * @return $this 408 | */ 409 | public function block(Closure $callback) 410 | { 411 | $this->blocks[] = $block = new SlackAttachmentBlock; 412 | 413 | $callback($block); 414 | 415 | return $this; 416 | } 417 | 418 | /** 419 | * Add a divider block to the attachment. 420 | * 421 | * @return $this 422 | */ 423 | public function dividerBlock() 424 | { 425 | $this->blocks[] = new SlackAttachmentDividerBlock; 426 | 427 | return $this; 428 | } 429 | } 430 | -------------------------------------------------------------------------------- /src/Messages/SlackAttachmentBlock.php: -------------------------------------------------------------------------------- 1 | type = $type; 81 | 82 | return $this; 83 | } 84 | 85 | /** 86 | * Set the text of the block. 87 | * 88 | * @param array $text 89 | * @return $this 90 | */ 91 | public function text($text) 92 | { 93 | $this->text = $text; 94 | 95 | return $this; 96 | } 97 | 98 | /** 99 | * Set the ID of the block. 100 | * 101 | * @param string $id 102 | * @return $this 103 | */ 104 | public function id($id) 105 | { 106 | $this->id = $id; 107 | 108 | return $this; 109 | } 110 | 111 | /** 112 | * Set the fields of the block. 113 | * 114 | * @param array $fields 115 | * @return $this 116 | */ 117 | public function fields($fields) 118 | { 119 | $this->fields = $fields; 120 | 121 | return $this; 122 | } 123 | 124 | /** 125 | * Set the accessory of the block. 126 | * 127 | * @param array $accessory 128 | * @return $this 129 | */ 130 | public function accessory($accessory) 131 | { 132 | $this->accessory = $accessory; 133 | 134 | return $this; 135 | } 136 | 137 | /** 138 | * Set the image url of the block. 139 | * 140 | * @param string $imageUrl 141 | * @return $this 142 | */ 143 | public function imageUrl($imageUrl) 144 | { 145 | $this->imageUrl = $imageUrl; 146 | 147 | return $this; 148 | } 149 | 150 | /** 151 | * Set the alt text of the block. 152 | * 153 | * @param string $altText 154 | * @return $this 155 | */ 156 | public function altText($altText) 157 | { 158 | $this->altText = $altText; 159 | 160 | return $this; 161 | } 162 | 163 | /** 164 | * Set the title of the block. 165 | * 166 | * @param array $title 167 | * @return $this 168 | */ 169 | public function title($title) 170 | { 171 | $this->title = $title; 172 | 173 | return $this; 174 | } 175 | 176 | /** 177 | * Set the elements of the block. 178 | * 179 | * @param array $elements 180 | * @return $this 181 | */ 182 | public function elements($elements) 183 | { 184 | $this->elements = $elements; 185 | 186 | return $this; 187 | } 188 | 189 | /** 190 | * Get the array representation of the attachment block. 191 | * 192 | * @return array 193 | */ 194 | public function toArray() 195 | { 196 | return array_filter([ 197 | 'type' => $this->type, 198 | 'text' => $this->text, 199 | 'block_id' => $this->id, 200 | 'fields' => $this->fields, 201 | 'accessory' => $this->accessory, 202 | 'image_url' => $this->imageUrl, 203 | 'alt_text' => $this->altText, 204 | 'title' => $this->title, 205 | 'elements' => $this->elements, 206 | ]); 207 | } 208 | } -------------------------------------------------------------------------------- /src/Messages/SlackAttachmentDividerBlock.php: -------------------------------------------------------------------------------- 1 | 'divider', 16 | ]; 17 | } 18 | } -------------------------------------------------------------------------------- /src/Messages/SlackAttachmentField.php: -------------------------------------------------------------------------------- 1 | title = $title; 37 | 38 | return $this; 39 | } 40 | 41 | /** 42 | * Set the content of the field. 43 | * 44 | * @param string $content 45 | * @return $this 46 | */ 47 | public function content($content) 48 | { 49 | $this->content = $content; 50 | 51 | return $this; 52 | } 53 | 54 | /** 55 | * Indicates that the content should not be displayed side-by-side with other fields. 56 | * 57 | * @return $this 58 | */ 59 | public function long() 60 | { 61 | $this->short = false; 62 | 63 | return $this; 64 | } 65 | 66 | /** 67 | * Get the array representation of the attachment field. 68 | * 69 | * @return array 70 | */ 71 | public function toArray() 72 | { 73 | return [ 74 | 'title' => $this->title, 75 | 'value' => $this->content, 76 | 'short' => $this->short, 77 | ]; 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /src/Messages/SlackMessage.php: -------------------------------------------------------------------------------- 1 | level = 'info'; 109 | 110 | return $this; 111 | } 112 | 113 | /** 114 | * Indicate that the notification gives information about a successful operation. 115 | * 116 | * @return $this 117 | */ 118 | public function success() 119 | { 120 | $this->level = 'success'; 121 | 122 | return $this; 123 | } 124 | 125 | /** 126 | * Indicate that the notification gives information about a warning. 127 | * 128 | * @return $this 129 | */ 130 | public function warning() 131 | { 132 | $this->level = 'warning'; 133 | 134 | return $this; 135 | } 136 | 137 | /** 138 | * Indicate that the notification gives information about an error. 139 | * 140 | * @return $this 141 | */ 142 | public function error() 143 | { 144 | $this->level = 'error'; 145 | 146 | return $this; 147 | } 148 | 149 | /** 150 | * Set a custom username and optional emoji icon for the Slack message. 151 | * 152 | * @param string $username 153 | * @param string|null $icon 154 | * @return $this 155 | */ 156 | public function from($username, $icon = null) 157 | { 158 | $this->username = $username; 159 | 160 | if (! is_null($icon)) { 161 | $this->icon = $icon; 162 | } 163 | 164 | return $this; 165 | } 166 | 167 | /** 168 | * Set a custom image icon the message should use. 169 | * 170 | * @param string $image 171 | * @return $this 172 | */ 173 | public function image($image) 174 | { 175 | $this->image = $image; 176 | 177 | return $this; 178 | } 179 | 180 | /** 181 | * Set the Slack channel the message should be sent to. 182 | * 183 | * @param string $channel 184 | * @return $this 185 | */ 186 | public function to($channel) 187 | { 188 | $this->channel = $channel; 189 | 190 | return $this; 191 | } 192 | 193 | /** 194 | * Set the content of the Slack message. 195 | * 196 | * @param string $content 197 | * @return $this 198 | */ 199 | public function content($content) 200 | { 201 | $this->content = $content; 202 | 203 | return $this; 204 | } 205 | 206 | /** 207 | * Define an attachment for the message. 208 | * 209 | * @param \Closure $callback 210 | * @return $this 211 | */ 212 | public function attachment(Closure $callback) 213 | { 214 | $this->attachments[] = $attachment = new SlackAttachment; 215 | 216 | $callback($attachment); 217 | 218 | return $this; 219 | } 220 | 221 | /** 222 | * Get the color for the message. 223 | * 224 | * @return string|null 225 | */ 226 | public function color() 227 | { 228 | switch ($this->level) { 229 | case 'success': 230 | return 'good'; 231 | case 'error': 232 | return 'danger'; 233 | case 'warning': 234 | return 'warning'; 235 | } 236 | } 237 | 238 | /** 239 | * Find and link channel names and usernames. 240 | * 241 | * @return $this 242 | */ 243 | public function linkNames() 244 | { 245 | $this->linkNames = 1; 246 | 247 | return $this; 248 | } 249 | 250 | /** 251 | * Unfurl links to rich display. 252 | * 253 | * @param string $unfurl 254 | * @return $this 255 | */ 256 | public function unfurlLinks($unfurl) 257 | { 258 | $this->unfurlLinks = $unfurl; 259 | 260 | return $this; 261 | } 262 | 263 | /** 264 | * Unfurl media to rich display. 265 | * 266 | * @param string $unfurl 267 | * @return $this 268 | */ 269 | public function unfurlMedia($unfurl) 270 | { 271 | $this->unfurlMedia = $unfurl; 272 | 273 | return $this; 274 | } 275 | 276 | /** 277 | * Set additional request options for the Guzzle HTTP client. 278 | * 279 | * @param array $options 280 | * @return $this 281 | */ 282 | public function http(array $options) 283 | { 284 | $this->http = $options; 285 | 286 | return $this; 287 | } 288 | 289 | /** 290 | * Set the threadTimestamp. 291 | * 292 | * @param \DateTimeInterface|\DateInterval|string $threadTimestamp 293 | * @return $this 294 | */ 295 | public function threadTimestamp($threadTimestamp) 296 | { 297 | $this->threadTimestamp = $threadTimestamp; 298 | 299 | return $this; 300 | } 301 | 302 | /** 303 | * Set the thread to Broadcast 304 | * 305 | * @param bool $threadBroadcast 306 | * @return $this 307 | */ 308 | public function threadBroadcast($threadBroadcast = true) 309 | { 310 | $this->threadBroadcast = $threadBroadcast; 311 | 312 | return $this; 313 | } 314 | } 315 | -------------------------------------------------------------------------------- /src/SlackChannelServiceProvider.php: -------------------------------------------------------------------------------- 1 | extend('slack', function ($app) { 21 | return $app->make(Channels\SlackApiChannel::class); 22 | }); 23 | }); 24 | } 25 | } 26 | --------------------------------------------------------------------------------