59 lines
2.4 KiB
PHP
59 lines
2.4 KiB
PHP
|
|
<?php
|
||
|
|
require_once dirname(__DIR__) . '/helpers/UrlHelper.php';
|
||
|
|
|
||
|
|
class NotificationHelper {
|
||
|
|
/**
|
||
|
|
* Send a Matrix webhook notification for a new ticket.
|
||
|
|
*
|
||
|
|
* @param string $ticketId Ticket ID (9-digit string)
|
||
|
|
* @param array $ticketData Ticket fields (title, priority, category, type, status, ...)
|
||
|
|
* @param string $trigger 'manual' (web UI) or 'automated' (API)
|
||
|
|
*/
|
||
|
|
public static function sendTicketNotification($ticketId, $ticketData, $trigger = 'manual') {
|
||
|
|
$webhookUrl = $GLOBALS['config']['MATRIX_WEBHOOK_URL'] ?? null;
|
||
|
|
if (empty($webhookUrl)) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Parse notify users from config (comma-separated Matrix user IDs)
|
||
|
|
$notifyRaw = $GLOBALS['config']['MATRIX_NOTIFY_USERS'] ?? '';
|
||
|
|
$notifyUsers = array_values(array_filter(array_map('trim', explode(',', $notifyRaw))));
|
||
|
|
|
||
|
|
// Extract hostname from [hostname] prefix in title
|
||
|
|
preg_match('/^\[([^\]]+)\]/', $ticketData['title'] ?? '', $m);
|
||
|
|
$source = $m[1] ?? ($trigger === 'automated' ? 'Automated' : 'Manual');
|
||
|
|
|
||
|
|
$payload = [
|
||
|
|
'ticket_id' => $ticketId,
|
||
|
|
'title' => $ticketData['title'] ?? 'Untitled',
|
||
|
|
'priority' => (int)($ticketData['priority'] ?? 4),
|
||
|
|
'category' => $ticketData['category'] ?? 'General',
|
||
|
|
'type' => $ticketData['type'] ?? 'Issue',
|
||
|
|
'status' => $ticketData['status'] ?? 'Open',
|
||
|
|
'source' => $source,
|
||
|
|
'url' => UrlHelper::ticketUrl($ticketId),
|
||
|
|
'trigger' => $trigger,
|
||
|
|
'notify_users' => $notifyUsers,
|
||
|
|
];
|
||
|
|
|
||
|
|
$ch = curl_init($webhookUrl);
|
||
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
|
||
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
||
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
|
||
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
||
|
|
|
||
|
|
$response = curl_exec($ch);
|
||
|
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||
|
|
$curlError = curl_error($ch);
|
||
|
|
curl_close($ch);
|
||
|
|
|
||
|
|
if ($curlError) {
|
||
|
|
error_log("Matrix webhook cURL error for ticket #{$ticketId}: {$curlError}");
|
||
|
|
} elseif ($httpCode < 200 || $httpCode >= 300) {
|
||
|
|
error_log("Matrix webhook failed for ticket #{$ticketId}. HTTP {$httpCode}: {$response}");
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
?>
|