169 lines
4.9 KiB
PHP
169 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Services;
|
|
|
|
use App\Models\Setting;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class Discord
|
|
{
|
|
protected ?string $token;
|
|
protected ?string $group;
|
|
|
|
// Discord embed colors
|
|
const COLOR_SUCCESS = 3066993; // Green
|
|
const COLOR_ERROR = 15158332; // Red
|
|
const COLOR_INFO = 3447003; // Blue
|
|
const COLOR_WARNING = 16776960; // Yellow
|
|
const COLOR_VIP = 16711935; // Purple
|
|
|
|
public function __construct()
|
|
{
|
|
$record = Setting::where('key', 'setting')->first();
|
|
if ($record) {
|
|
$setting = json_decode($record->value);
|
|
$this->token = $setting->discord_webhook_id ?? null;
|
|
$this->group = $setting->discord_webhook_token ?? null;
|
|
} else {
|
|
$this->token = null;
|
|
$this->group = null;
|
|
}
|
|
}
|
|
|
|
protected function sendDiscordMessage(array $payload)
|
|
{
|
|
if (!$this->token || !$this->group) {
|
|
Log::channel('Custom')->warning('Discord notification not sent: Missing webhook ID or token');
|
|
return null;
|
|
}
|
|
|
|
$webhookUrl = "https://discord.com/api/webhooks/{$this->token}/{$this->group}";
|
|
|
|
try {
|
|
return Http::post($webhookUrl, $payload);
|
|
} catch (\Exception $exception) {
|
|
Log::channel('Custom')->error('Failed to send Discord notification: ' . $exception->getMessage());
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Send simple message with embed
|
|
*/
|
|
public function SendMessage($message = '', $color = self::COLOR_INFO, $title = null)
|
|
{
|
|
$embed = [
|
|
'description' => $message,
|
|
'color' => $color,
|
|
'timestamp' => now()->toIso8601String(),
|
|
];
|
|
|
|
if ($title) {
|
|
$embed['title'] = $title;
|
|
}
|
|
|
|
$payload = [
|
|
'embeds' => [$embed]
|
|
];
|
|
|
|
return $this->sendDiscordMessage($payload);
|
|
}
|
|
|
|
/**
|
|
* Send error message with enhanced formatting
|
|
*/
|
|
public function SendErrorMessage($data, $context = null)
|
|
{
|
|
$content = is_string($data) ? $data : json_encode($data, JSON_PRETTY_PRINT);
|
|
|
|
$fields = [];
|
|
if ($context) {
|
|
$fields[] = [
|
|
'name' => '📍 Ngữ cảnh',
|
|
'value' => $context,
|
|
'inline' => false,
|
|
];
|
|
}
|
|
|
|
$fields[] = [
|
|
'name' => '⚠️ Chi tiết lỗi',
|
|
'value' => "```json\n" . substr($content, 0, 1000) . "\n```",
|
|
'inline' => false,
|
|
];
|
|
|
|
$embed = [
|
|
'title' => '🚨 Lỗi hệ thống',
|
|
'description' => '**Admin:** Đã xảy ra lỗi trong quá trình xử lý dịch vụ',
|
|
'color' => self::COLOR_ERROR,
|
|
'fields' => $fields,
|
|
'footer' => [
|
|
'text' => 'Error Notification System'
|
|
],
|
|
'timestamp' => now()->toIso8601String(),
|
|
];
|
|
|
|
return $this->sendDiscordMessage(['embeds' => [$embed]]);
|
|
}
|
|
|
|
/**
|
|
* Send job execution notification
|
|
*/
|
|
public function SendJobRunInfo($jobName, $status, $additionalInfo = null, $duration = null)
|
|
{
|
|
$statusEmoji = match (strtolower($status)) {
|
|
'success', 'completed' => '✅',
|
|
'failed', 'error' => '❌',
|
|
'running', 'processing' => '⏳',
|
|
'warning' => '⚠️',
|
|
default => '📊'
|
|
};
|
|
|
|
$color = match (strtolower($status)) {
|
|
'success', 'completed' => self::COLOR_SUCCESS,
|
|
'failed', 'error' => self::COLOR_ERROR,
|
|
'warning' => self::COLOR_WARNING,
|
|
default => self::COLOR_INFO
|
|
};
|
|
|
|
$fields = [
|
|
[
|
|
'name' => '⏱️ Thời gian chạy',
|
|
'value' => Carbon::now()->format('d/m/Y H:i:s'),
|
|
'inline' => true,
|
|
],
|
|
[
|
|
'name' => '📊 Trạng thái',
|
|
'value' => "{$statusEmoji} {$status}",
|
|
'inline' => true,
|
|
],
|
|
];
|
|
|
|
if ($duration) {
|
|
$fields[] = [
|
|
'name' => '⏲️ Thời gian thực thi',
|
|
'value' => $duration,
|
|
'inline' => true,
|
|
];
|
|
}
|
|
|
|
if ($additionalInfo) {
|
|
$info = is_string($additionalInfo) ? $additionalInfo : json_encode($additionalInfo, JSON_PRETTY_PRINT);
|
|
$fields[] = [
|
|
'name' => '📋 Thông tin chi tiết',
|
|
'value' => "```\n" . substr($info, 0, 1000) . "\n```",
|
|
'inline' => false,
|
|
];
|
|
}
|
|
|
|
$embed = [
|
|
'title' => "⚙️ Job Execution: {$jobName}",
|
|
'color' => $color,
|
|
'fields' => $fields,
|
|
'timestamp' => now()->toIso8601String(),
|
|
];
|
|
|
|
return $this->sendDiscordMessage(['embeds' => [$embed]]);
|
|
}
|
|
} |