Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Slack Notification to work with Slack Apps for Laravel 11 support #1481

Merged
merged 7 commits into from
Jul 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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