Unify Discord webhook notifications between API and manual ticket creation

- Standardized embed format across both ticket creation paths
- Added consistent priority colors (P1-P5) with distinct hex values
- Added priority labels (e.g., "P1 - Critical" instead of just "1")
- Added Source field showing hostname extracted from ticket title
- Added Status field to both webhook formats
- Added footer distinguishing "Automated Alert" vs "Manual Entry"
- Added timestamp to API endpoint webhooks
- Added error logging for failed webhook calls
- Added timeout (10s) to API endpoint curl calls
- Added null check for webhook URL in API endpoint

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-26 11:11:40 -05:00
parent ee796dce91
commit 8b89114607
2 changed files with 111 additions and 60 deletions

View File

@@ -216,47 +216,71 @@ class TicketController {
$host = $_SERVER['HTTP_HOST'] ?? 't.lotusguild.org';
$ticketUrl = "{$protocol}://{$host}/ticket/{$ticketId}";
// Map priorities to Discord colors
// Map priorities to Discord colors (matching API endpoint)
$priorityColors = [
1 => 0xff4d4d, // Red
2 => 0xffa726, // Orange
3 => 0x42a5f5, // Blue
4 => 0x66bb6a, // Green
5 => 0x9e9e9e // Gray
1 => 0xDC3545, // P1 Critical - Red
2 => 0xFD7E14, // P2 High - Orange
3 => 0x0DCAF0, // P3 Medium - Cyan
4 => 0x198754, // P4 Low - Green
5 => 0x6C757D // P5 Info - Gray
];
// Priority labels for display
$priorityLabels = [
1 => "P1 - Critical",
2 => "P2 - High",
3 => "P3 - Medium",
4 => "P4 - Low",
5 => "P5 - Info"
];
$priority = (int)($ticketData['priority'] ?? 4);
$color = $priorityColors[$priority] ?? 0x3498db;
$color = $priorityColors[$priority] ?? 0x6C757D;
$priorityLabel = $priorityLabels[$priority] ?? "P{$priority}";
$title = $ticketData['title'] ?? 'Untitled';
$category = $ticketData['category'] ?? 'General';
$type = $ticketData['type'] ?? 'Issue';
$status = $ticketData['status'] ?? 'Open';
// Extract hostname from title for cleaner display
preg_match('/^\[([^\]]+)\]/', $title, $hostnameMatch);
$sourceHost = $hostnameMatch[1] ?? 'Manual';
$embed = [
'title' => '🎫 New Ticket Created',
'description' => "**#{$ticketId}** - " . $ticketData['title'],
'title' => 'New Ticket Created',
'description' => "**#{$ticketId}** - {$title}",
'url' => $ticketUrl,
'color' => $color,
'fields' => [
[
'name' => 'Priority',
'value' => 'P' . $priority,
'value' => $priorityLabel,
'inline' => true
],
[
'name' => 'Category',
'value' => $ticketData['category'] ?? 'General',
'value' => $category,
'inline' => true
],
[
'name' => 'Type',
'value' => $ticketData['type'] ?? 'Issue',
'value' => $type,
'inline' => true
],
[
'name' => 'Status',
'value' => $ticketData['status'] ?? 'Open',
'value' => $status,
'inline' => true
],
[
'name' => 'Source',
'value' => $sourceHost,
'inline' => true
]
],
'footer' => [
'text' => 'Tinker Tickets'
'text' => 'Tinker Tickets | Manual Entry'
],
'timestamp' => date('c')
];
@@ -279,9 +303,9 @@ class TicketController {
curl_close($ch);
if ($curlError) {
error_log("Discord webhook cURL error: $curlError");
} else {
error_log("Discord webhook sent for new ticket. HTTP Code: $httpCode");
error_log("Discord webhook cURL error: {$curlError}");
} elseif ($httpCode !== 204 && $httpCode !== 200) {
error_log("Discord webhook failed for ticket #{$ticketId}. HTTP Code: {$httpCode}");
}
}
}

View File

@@ -223,33 +223,53 @@ if ($stmt->execute()) {
$stmt->close();
$conn->close();
// Discord webhook
// Discord webhook notification
if (isset($envVars['DISCORD_WEBHOOK_URL']) && !empty($envVars['DISCORD_WEBHOOK_URL'])) {
$discord_webhook_url = $envVars['DISCORD_WEBHOOK_URL'];
// Map priorities to Discord colors (decimal format)
$priorityColors = [
"1" => 16736589, // --priority-1: #ff4d4d
"2" => 16753958, // --priority-2: #ffa726
"3" => 4363509, // --priority-3: #42a5f5
"4" => 6736490 // --priority-4: #66bb6a
"1" => 0xDC3545, // P1 Critical - Red
"2" => 0xFD7E14, // P2 High - Orange
"3" => 0x0DCAF0, // P3 Medium - Cyan
"4" => 0x198754, // P4 Low - Green
"5" => 0x6C757D // P5 Info - Gray
];
// Priority labels for display
$priorityLabels = [
"1" => "P1 - Critical",
"2" => "P2 - High",
"3" => "P3 - Medium",
"4" => "P4 - Low",
"5" => "P5 - Info"
];
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = $_SERVER['HTTP_HOST'] ?? 't.lotusguild.org';
$ticketUrl = "{$protocol}://{$host}/ticket/{$ticket_id}";
// Extract hostname from title for cleaner display
preg_match('/^\[([^\]]+)\]/', $title, $hostnameMatch);
$sourceHost = $hostnameMatch[1] ?? 'Unknown';
$discord_data = [
"content" => "",
"embeds" => [[
"title" => "New Ticket Created: #" . $ticket_id,
"description" => $title,
"title" => "New Ticket Created",
"description" => "**#{$ticket_id}** - {$title}",
"url" => $ticketUrl,
"color" => $priorityColors[$priority],
"color" => $priorityColors[$priority] ?? 0x6C757D,
"fields" => [
["name" => "Priority", "value" => $priority, "inline" => true],
["name" => "Priority", "value" => $priorityLabels[$priority] ?? "P{$priority}", "inline" => true],
["name" => "Category", "value" => $category, "inline" => true],
["name" => "Type", "value" => $type, "inline" => true]
]
["name" => "Type", "value" => $type, "inline" => true],
["name" => "Status", "value" => $status, "inline" => true],
["name" => "Source", "value" => $sourceHost, "inline" => true]
],
"footer" => [
"text" => "Tinker Tickets | Automated Alert"
],
"timestamp" => date('c')
]]
];
@@ -258,5 +278,12 @@ curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($discord_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$webhookResult = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 204 && $httpCode !== 200) {
error_log("Discord webhook failed for ticket #{$ticket_id}. HTTP Code: {$httpCode}");
}
}