From a4c39cf8663c514deca2ddf9bb4be23aa88f0c84 Mon Sep 17 00:00:00 2001 From: Travis Ricks Date: Thu, 25 Jul 2024 22:26:50 +0900 Subject: [PATCH 1/7] Add support for slack notifications by channel id --- src/Notifications/LongWaitDetected.php | 49 +++++++++++++++++++------- 1 file changed, 36 insertions(+), 13 deletions(-) diff --git a/src/Notifications/LongWaitDetected.php b/src/Notifications/LongWaitDetected.php index bd4dcce1..3f941a1c 100644 --- a/src/Notifications/LongWaitDetected.php +++ b/src/Notifications/LongWaitDetected.php @@ -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 @@ -90,19 +93,39 @@ public function toMail($notifiable) */ public function toSlack($notifiable) { - 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 - )); - }); + $fromName = 'Laravel Horizon'; + $imageUrl = 'https://laravel.com/assets/img/horizon-48px.png'; + $text = 'Oh no! Something needs your attention.'; + $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 + ); + + if (is_string(Horizon::$slackWebhookUrl) && Str::startsWith(Horizon::$slackWebhookUrl, ['http://', 'https://'])) { + return (new SlackMessage) // @phpstan-ignore-line + ->from($fromName) + ->to(Horizon::$slackChannel) + ->image($imageUrl) + ->error() + ->content($text) + ->attachment(function ($attachment) use ($title, $content) { + $attachment->title($title) + ->content($content); + }); + } + + return (new ChannelIdSlackMessage) + ->username($fromName) + ->image($imageUrl) + ->text($text) + ->headerBlock($title) + ->sectionBlock(function (SectionBlock $block) use ($content): void { + $block->text($content); + }); } /** From 0507542e5a04ca3b47f910d3d54bd6852221b990 Mon Sep 17 00:00:00 2001 From: Travis Ricks Date: Thu, 25 Jul 2024 23:47:34 +0900 Subject: [PATCH 2/7] Update LongWaitDetected.php --- src/Notifications/LongWaitDetected.php | 30 +++++++++++++------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Notifications/LongWaitDetected.php b/src/Notifications/LongWaitDetected.php index 3f941a1c..63c04e78 100644 --- a/src/Notifications/LongWaitDetected.php +++ b/src/Notifications/LongWaitDetected.php @@ -105,26 +105,26 @@ public function toSlack($notifiable) $this->seconds ); - if (is_string(Horizon::$slackWebhookUrl) && Str::startsWith(Horizon::$slackWebhookUrl, ['http://', 'https://'])) { - return (new SlackMessage) // @phpstan-ignore-line - ->from($fromName) - ->to(Horizon::$slackChannel) + if (class_exists('Illuminate\Notifications\Slack\SlackMessage') && !(is_string(Horizon::$slackWebhookUrl) && Str::startsWith(Horizon::$slackWebhookUrl, ['http://', 'https://']))) { + return (new ChannelIdSlackMessage) // @phpstan-ignore-line + ->username($fromName) ->image($imageUrl) - ->error() - ->content($text) - ->attachment(function ($attachment) use ($title, $content) { - $attachment->title($title) - ->content($content); + ->text($text) + ->headerBlock($title) + ->sectionBlock(function (SectionBlock $block) use ($content): void { + $block->text($content); }); } - return (new ChannelIdSlackMessage) - ->username($fromName) + return (new SlackMessage) // @phpstan-ignore-line + ->from($fromName) + ->to(Horizon::$slackChannel) ->image($imageUrl) - ->text($text) - ->headerBlock($title) - ->sectionBlock(function (SectionBlock $block) use ($content): void { - $block->text($content); + ->error() + ->content($text) + ->attachment(function ($attachment) use ($title, $content) { + $attachment->title($title) + ->content($content); }); } From fb1cc54b17705fc09d5699000fe30fd19e65d19b Mon Sep 17 00:00:00 2001 From: Travis Ricks Date: Thu, 25 Jul 2024 23:54:26 +0900 Subject: [PATCH 3/7] Update LongWaitDetected.php --- src/Notifications/LongWaitDetected.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Notifications/LongWaitDetected.php b/src/Notifications/LongWaitDetected.php index 63c04e78..dd04cec5 100644 --- a/src/Notifications/LongWaitDetected.php +++ b/src/Notifications/LongWaitDetected.php @@ -106,7 +106,7 @@ public function toSlack($notifiable) ); if (class_exists('Illuminate\Notifications\Slack\SlackMessage') && !(is_string(Horizon::$slackWebhookUrl) && Str::startsWith(Horizon::$slackWebhookUrl, ['http://', 'https://']))) { - return (new ChannelIdSlackMessage) // @phpstan-ignore-line + return (new ChannelIdSlackMessage) ->username($fromName) ->image($imageUrl) ->text($text) From 6937916f42bfa94177128d52080d390770278758 Mon Sep 17 00:00:00 2001 From: Travis Ricks Date: Thu, 25 Jul 2024 23:57:33 +0900 Subject: [PATCH 4/7] Update LongWaitDetected.php --- src/Notifications/LongWaitDetected.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Notifications/LongWaitDetected.php b/src/Notifications/LongWaitDetected.php index dd04cec5..4687dce5 100644 --- a/src/Notifications/LongWaitDetected.php +++ b/src/Notifications/LongWaitDetected.php @@ -105,7 +105,7 @@ public function toSlack($notifiable) $this->seconds ); - if (class_exists('Illuminate\Notifications\Slack\SlackMessage') && !(is_string(Horizon::$slackWebhookUrl) && Str::startsWith(Horizon::$slackWebhookUrl, ['http://', 'https://']))) { + 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) From c4ec095c3bf28ca3e2066d418fd6fa3efc885462 Mon Sep 17 00:00:00 2001 From: Travis Ricks Date: Fri, 26 Jul 2024 00:02:21 +0900 Subject: [PATCH 5/7] Update LongWaitDetected.php --- src/Notifications/LongWaitDetected.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Notifications/LongWaitDetected.php b/src/Notifications/LongWaitDetected.php index 4687dce5..f95d61fe 100644 --- a/src/Notifications/LongWaitDetected.php +++ b/src/Notifications/LongWaitDetected.php @@ -105,7 +105,7 @@ public function toSlack($notifiable) $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://']))) { + 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) From cb77b74c71a93246c04acf9184abb6b108a1064f Mon Sep 17 00:00:00 2001 From: Travis Ricks Date: Fri, 26 Jul 2024 12:05:47 +0900 Subject: [PATCH 6/7] Update LongWaitDetected.php --- src/Notifications/LongWaitDetected.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Notifications/LongWaitDetected.php b/src/Notifications/LongWaitDetected.php index f95d61fe..abc4c9d8 100644 --- a/src/Notifications/LongWaitDetected.php +++ b/src/Notifications/LongWaitDetected.php @@ -111,7 +111,7 @@ public function toSlack($notifiable) ->image($imageUrl) ->text($text) ->headerBlock($title) - ->sectionBlock(function (SectionBlock $block) use ($content): void { + ->sectionBlock(function (SectionBlock $block) use ($content): void { // @phpstan-ignore-line $block->text($content); }); } From 4c0e0f43f1db2ab0fa91c4003b123cbfbe1aff19 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Fri, 26 Jul 2024 08:40:30 +0300 Subject: [PATCH 7/7] formatting --- src/Notifications/LongWaitDetected.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Notifications/LongWaitDetected.php b/src/Notifications/LongWaitDetected.php index abc4c9d8..5d974c59 100644 --- a/src/Notifications/LongWaitDetected.php +++ b/src/Notifications/LongWaitDetected.php @@ -94,9 +94,10 @@ public function toMail($notifiable) public function toSlack($notifiable) { $fromName = 'Laravel Horizon'; - $imageUrl = 'https://laravel.com/assets/img/horizon-48px.png'; - $text = 'Oh no! Something needs your attention.'; $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'), @@ -105,7 +106,9 @@ public function toSlack($notifiable) $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://']))) { + 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)