Skip to content

Commit

Permalink
Extend Slack Notification to work with Slack Apps for Laravel 11 supp…
Browse files Browse the repository at this point in the history
…ort (#1481)

* Add support for slack notifications by channel id

* Update LongWaitDetected.php

* Update LongWaitDetected.php

* Update LongWaitDetected.php

* Update LongWaitDetected.php

* Update LongWaitDetected.php

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
travisricks and taylorotwell authored Jul 26, 2024
1 parent 228c709 commit 8830039
Showing 1 changed file with 38 additions and 12 deletions.
50 changes: 38 additions & 12 deletions src/Notifications/LongWaitDetected.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Illuminate\Notifications\Messages\NexmoMessage;
use Illuminate\Notifications\Messages\SlackMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Slack\BlockKit\Blocks\SectionBlock;
use Illuminate\Notifications\Slack\SlackMessage as ChannelIdSlackMessage;
use Illuminate\Support\Str;
use Laravel\Horizon\Horizon;

class LongWaitDetected extends Notification
Expand Down Expand Up @@ -90,19 +93,42 @@ public function toMail($notifiable)
*/
public function toSlack($notifiable)
{
$fromName = 'Laravel Horizon';
$title = 'Long Wait Detected';
$text = 'Oh no! Something needs your attention.';
$imageUrl = 'https://laravel.com/assets/img/horizon-48px.png';

$content = sprintf(
'[%s] The "%s" queue on the "%s" connection has a wait time of %s seconds.',
config('app.name'),
$this->longWaitQueue,
$this->longWaitConnection,
$this->seconds
);

if (class_exists('\Illuminate\Notifications\Slack\SlackMessage') &&
class_exists('\Illuminate\Notifications\Slack\BlockKit\Blocks\SectionBlock') &&
! (is_string(Horizon::$slackWebhookUrl) && Str::startsWith(Horizon::$slackWebhookUrl, ['http://', 'https://']))) {
return (new ChannelIdSlackMessage)
->username($fromName)
->image($imageUrl)
->text($text)
->headerBlock($title)
->sectionBlock(function (SectionBlock $block) use ($content): void { // @phpstan-ignore-line
$block->text($content);
});
}

return (new SlackMessage) // @phpstan-ignore-line
->from('Laravel Horizon')
->to(Horizon::$slackChannel)
->image('https://laravel.com/assets/img/horizon-48px.png')
->error()
->content('Oh no! Something needs your attention.')
->attachment(function ($attachment) {
$attachment->title('Long Wait Detected')
->content(sprintf(
'[%s] The "%s" queue on the "%s" connection has a wait time of %s seconds.',
config('app.name'), $this->longWaitQueue, $this->longWaitConnection, $this->seconds
));
});
->from($fromName)
->to(Horizon::$slackChannel)
->image($imageUrl)
->error()
->content($text)
->attachment(function ($attachment) use ($title, $content) {
$attachment->title($title)
->content($content);
});
}

/**
Expand Down

0 comments on commit 8830039

Please sign in to comment.