Files
DatBrowser/app/Notification/Providers/DiscordWebHookNotification.php
Matthias Guillitte c26a09701a
All checks were successful
Push image to registry / build-image (push) Successful in 4m24s
Allow sending helcase battle to custom discord webhook
2025-03-20 19:28:40 +01:00

114 lines
4.1 KiB
PHP

<?php
namespace App\Notification\Providers;
use App\Notification\NotificationProvider;
use App\Models\JobInfo;
use Illuminate\Support\Facades\Cache;
class DiscordWebHookNotification extends NotificationProvider {
private const EMBED_COLOR = ["521254", "16058119"];
/**
* @inheritDoc
*/
public static function send(\App\Notification\Notification $notification, array $options): void {
/*
Test Json for a complete embed :
{
"content": "",
"tts": false,
"embeds": [
{
"id": 652627557,
"title": "Title",
"description": "This is the markdown body\n",
"color": 521254,
"fields": [
{
"id": 984079152,
"name": "Field 1",
"value": "test"
}
],
"url": "https://localhost:8000",
"image": {
"url": "https://www.thoughtco.com/thmb/jzJO77P0K9zIbqxQOVOaHWFCfj4=/1732x1732/filters:fill(auto,1)/GettyImages-186451154-58c3965a3df78c353cf8cc7b.jpg"
}
}
],
"components": [],
"actions": {},
"username": "Datboi",
"avatar_url": "https://www.fairytailrp.com/t40344-here-come-dat-boi"
}
*/
$webHookUrl = static::getDiscordWebHookUrl($notification->isError, $options);
$body = [
"content"=> "",
"tts"=> false,
"embeds" => [
[
"id" => 652627557,
"title" => $notification->getTitle()->toString(),
"description" => $notification->getBody()->toMarkdownString(),
"color" => self::EMBED_COLOR[(int)$notification->isError],
"url" => $notification->getLinkURL(),
],
],
"username" => "Datboi",
"avatar_url" => "https://media1.giphy.com/media/yDTWAecZcB2Jq/200w.gif?cid=6c09b952f68kz3wnkqsmyha8e7xrpe8n2kx0nkf2b8cir6am&rid=200w.gif&ct=g",
];
if ($notification->getImageURL() !== null) {
$body["embeds"][0]["image"] = [
"url" => "attachment://image.png"
];
}
$payloadJson = json_encode($body);
$formData = [
'payload_json' => $payloadJson,
];
if ($notification->getImageURL() !== null) {
$formData['file'] = curl_file_create($notification->getImageProjectPath(), 'image/png', 'image.png');
}
$ch = curl_init($webHookUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: multipart/form-data'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $formData);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
}
private static function getDiscordWebHookUrl(bool $isError, array $options): string {
if ($options["discord_webhook_url"] !== null) {
return $options["discord_webhook_url"];
}
$generalWebHookUrlKey = 'discord_webhook_url';
$generalWebHookUrl = Cache::rememberForever($generalWebHookUrlKey, function () use ($generalWebHookUrlKey) {
return JobInfo::where('key', $generalWebHookUrlKey)->first()->value;
});
if ($generalWebHookUrl === null) {
throw new \Exception("Le webhook discord n'a pas été configuré");
}
if ($isError) {
$errorWebHookUrlKey = 'discord_error_webhook_url';
$errorWebHookUrl = Cache::rememberForever($errorWebHookUrlKey, function () use ($errorWebHookUrlKey) {
return JobInfo::where('key', $errorWebHookUrlKey)->first()->value;
});
return $errorWebHookUrl ?? $generalWebHookUrl;
}
else {
return $generalWebHookUrl;
}
}
}