Skip to content

Commit 8830039

Browse files
Extend Slack Notification to work with Slack Apps for Laravel 11 support (#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 <[email protected]>
1 parent 228c709 commit 8830039

File tree

1 file changed

+38
-12
lines changed

1 file changed

+38
-12
lines changed

src/Notifications/LongWaitDetected.php

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use Illuminate\Notifications\Messages\NexmoMessage;
88
use Illuminate\Notifications\Messages\SlackMessage;
99
use Illuminate\Notifications\Notification;
10+
use Illuminate\Notifications\Slack\BlockKit\Blocks\SectionBlock;
11+
use Illuminate\Notifications\Slack\SlackMessage as ChannelIdSlackMessage;
12+
use Illuminate\Support\Str;
1013
use Laravel\Horizon\Horizon;
1114

1215
class LongWaitDetected extends Notification
@@ -90,19 +93,42 @@ public function toMail($notifiable)
9093
*/
9194
public function toSlack($notifiable)
9295
{
96+
$fromName = 'Laravel Horizon';
97+
$title = 'Long Wait Detected';
98+
$text = 'Oh no! Something needs your attention.';
99+
$imageUrl = 'https://laravel.com/assets/img/horizon-48px.png';
100+
101+
$content = sprintf(
102+
'[%s] The "%s" queue on the "%s" connection has a wait time of %s seconds.',
103+
config('app.name'),
104+
$this->longWaitQueue,
105+
$this->longWaitConnection,
106+
$this->seconds
107+
);
108+
109+
if (class_exists('\Illuminate\Notifications\Slack\SlackMessage') &&
110+
class_exists('\Illuminate\Notifications\Slack\BlockKit\Blocks\SectionBlock') &&
111+
! (is_string(Horizon::$slackWebhookUrl) && Str::startsWith(Horizon::$slackWebhookUrl, ['http://', 'https://']))) {
112+
return (new ChannelIdSlackMessage)
113+
->username($fromName)
114+
->image($imageUrl)
115+
->text($text)
116+
->headerBlock($title)
117+
->sectionBlock(function (SectionBlock $block) use ($content): void { // @phpstan-ignore-line
118+
$block->text($content);
119+
});
120+
}
121+
93122
return (new SlackMessage) // @phpstan-ignore-line
94-
->from('Laravel Horizon')
95-
->to(Horizon::$slackChannel)
96-
->image('https://laravel.com/assets/img/horizon-48px.png')
97-
->error()
98-
->content('Oh no! Something needs your attention.')
99-
->attachment(function ($attachment) {
100-
$attachment->title('Long Wait Detected')
101-
->content(sprintf(
102-
'[%s] The "%s" queue on the "%s" connection has a wait time of %s seconds.',
103-
config('app.name'), $this->longWaitQueue, $this->longWaitConnection, $this->seconds
104-
));
105-
});
123+
->from($fromName)
124+
->to(Horizon::$slackChannel)
125+
->image($imageUrl)
126+
->error()
127+
->content($text)
128+
->attachment(function ($attachment) use ($title, $content) {
129+
$attachment->title($title)
130+
->content($content);
131+
});
106132
}
107133

108134
/**

0 commit comments

Comments
 (0)