Skip to content

Commit 51301fd

Browse files
authored
feat(notifications): add mattermost notifications (#7963)
1 parent fbacf70 commit 51301fd

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

app/Jobs/SendMessageToSlackJob.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,36 @@ public function __construct(
2222
}
2323

2424
public function handle(): void
25+
{
26+
if ($this->isSlackWebhook()) {
27+
$this->sendToSlack();
28+
29+
return;
30+
}
31+
32+
/**
33+
* 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.
34+
*
35+
* @see https://github.com/coollabsio/coolify/pull/6139#issuecomment-3756777708
36+
*/
37+
$this->sendToMattermost();
38+
}
39+
40+
private function isSlackWebhook(): bool
41+
{
42+
$parsedUrl = parse_url($this->webhookUrl);
43+
44+
if ($parsedUrl === false) {
45+
return false;
46+
}
47+
48+
$scheme = $parsedUrl['scheme'] ?? '';
49+
$host = $parsedUrl['host'] ?? '';
50+
51+
return $scheme === 'https' && $host === 'hooks.slack.com';
52+
}
53+
54+
private function sendToSlack(): void
2555
{
2656
Http::post($this->webhookUrl, [
2757
'text' => $this->message->title,
@@ -57,4 +87,24 @@ public function handle(): void
5787
],
5888
]);
5989
}
90+
91+
/**
92+
* @todo v5 refactor: Extract this into a separate SendMessageToMattermostJob.php triggered via the "mattermost" notification channel type.
93+
*/
94+
private function sendToMattermost(): void
95+
{
96+
$username = config('app.name');
97+
98+
Http::post($this->webhookUrl, [
99+
'username' => $username,
100+
'attachments' => [
101+
[
102+
'title' => $this->message->title,
103+
'color' => $this->message->color,
104+
'text' => $this->message->description,
105+
'footer' => $username,
106+
],
107+
],
108+
]);
109+
}
60110
}

0 commit comments

Comments
 (0)