2024-11-12 21:37:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Jobs;
|
|
|
|
|
|
|
|
|
|
use App\Notifications\Dto\SlackMessage;
|
|
|
|
|
use Illuminate\Bus\Queueable;
|
|
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
|
|
|
|
use Illuminate\Foundation\Bus\Dispatchable;
|
|
|
|
|
use Illuminate\Queue\InteractsWithQueue;
|
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
|
|
|
|
|
|
class SendMessageToSlackJob implements ShouldQueue
|
|
|
|
|
{
|
|
|
|
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
|
|
|
|
|
|
|
|
|
public function __construct(
|
2025-01-07 14:31:43 +00:00
|
|
|
private SlackMessage $message,
|
2024-11-12 21:37:55 +00:00
|
|
|
private string $webhookUrl
|
|
|
|
|
) {
|
2024-12-06 13:33:22 +00:00
|
|
|
$this->onQueue('high');
|
2024-11-12 21:37:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function handle(): void
|
2026-01-15 20:59:51 +00:00
|
|
|
{
|
|
|
|
|
if ($this->isSlackWebhook()) {
|
|
|
|
|
$this->sendToSlack();
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This works with Mattermost and as a fallback also with Slack, the notifications just look slightly different and advanced formatting for slack is not supported with Mattermost.
|
|
|
|
|
*
|
|
|
|
|
* @see https://github.com/coollabsio/coolify/pull/6139#issuecomment-3756777708
|
|
|
|
|
*/
|
|
|
|
|
$this->sendToMattermost();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function isSlackWebhook(): bool
|
|
|
|
|
{
|
|
|
|
|
$parsedUrl = parse_url($this->webhookUrl);
|
|
|
|
|
|
|
|
|
|
if ($parsedUrl === false) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$scheme = $parsedUrl['scheme'] ?? '';
|
|
|
|
|
$host = $parsedUrl['host'] ?? '';
|
|
|
|
|
|
|
|
|
|
return $scheme === 'https' && $host === 'hooks.slack.com';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function sendToSlack(): void
|
2024-11-12 21:37:55 +00:00
|
|
|
{
|
|
|
|
|
Http::post($this->webhookUrl, [
|
2025-02-11 15:31:37 +00:00
|
|
|
'text' => $this->message->title,
|
2024-11-12 21:37:55 +00:00
|
|
|
'blocks' => [
|
|
|
|
|
[
|
|
|
|
|
'type' => 'section',
|
|
|
|
|
'text' => [
|
|
|
|
|
'type' => 'plain_text',
|
2024-12-06 13:33:22 +00:00
|
|
|
'text' => 'Coolify Notification',
|
2024-11-12 21:37:55 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
'attachments' => [
|
|
|
|
|
[
|
2025-01-07 14:31:43 +00:00
|
|
|
'color' => $this->message->color,
|
2024-11-12 21:37:55 +00:00
|
|
|
'blocks' => [
|
|
|
|
|
[
|
|
|
|
|
'type' => 'header',
|
|
|
|
|
'text' => [
|
|
|
|
|
'type' => 'plain_text',
|
2025-01-07 14:31:43 +00:00
|
|
|
'text' => $this->message->title,
|
2024-11-12 21:37:55 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
'type' => 'section',
|
|
|
|
|
'text' => [
|
|
|
|
|
'type' => 'mrkdwn',
|
2025-01-07 14:31:43 +00:00
|
|
|
'text' => $this->message->description,
|
2024-12-06 13:33:22 +00:00
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
],
|
2024-11-12 21:37:55 +00:00
|
|
|
]);
|
|
|
|
|
}
|
2026-01-15 20:59:51 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @todo v5 refactor: Extract this into a separate SendMessageToMattermostJob.php triggered via the "mattermost" notification channel type.
|
|
|
|
|
*/
|
|
|
|
|
private function sendToMattermost(): void
|
|
|
|
|
{
|
|
|
|
|
$username = config('app.name');
|
|
|
|
|
|
|
|
|
|
Http::post($this->webhookUrl, [
|
|
|
|
|
'username' => $username,
|
|
|
|
|
'attachments' => [
|
|
|
|
|
[
|
|
|
|
|
'title' => $this->message->title,
|
|
|
|
|
'color' => $this->message->color,
|
|
|
|
|
'text' => $this->message->description,
|
|
|
|
|
'footer' => $username,
|
|
|
|
|
],
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
}
|
2024-12-06 13:33:22 +00:00
|
|
|
}
|