Sort of working beta
This commit is contained in:
110
app/Notification/Providers/DiscordWebHookNotification.php
Normal file
110
app/Notification/Providers/DiscordWebHookNotification.php
Normal file
@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
namespace App\Notification\Providers;
|
||||
|
||||
use App\Notification\NotificationProvider;
|
||||
use App\Notification\INotificationProvider;
|
||||
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): 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);
|
||||
$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): string {
|
||||
$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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user