diff --git a/js/ai_ticket_summary.js b/js/ai_ticket_summary.js new file mode 100644 index 00000000..9741d353 --- /dev/null +++ b/js/ai_ticket_summary.js @@ -0,0 +1,14 @@ +$('#summaryModal').on('shown.bs.modal', function (e) { + // Perform AJAX request to get the summary + $.ajax({ + url: 'post.php?ai_ticket_summary', + method: 'POST', + data: { ticket_id: }, + success: function(response) { + $('#summaryContent').html(response); + }, + error: function() { + $('#summaryContent').html('Error generating summary.'); + } + }); +}); \ No newline at end of file diff --git a/post/ai.php b/post/ai.php index f282a432..c0ffd1ef 100644 --- a/post/ai.php +++ b/post/ai.php @@ -57,3 +57,73 @@ if (isset($_GET['ai_reword'])) { } } + +if (isset($_GET['ai_ticket_summary'])) { + + // Retrieve the ticket_id from POST + $ticket_id = intval($_POST['ticket_id']); + + // Query the database for ticket details + // (You can reuse code from ticket.php or write a simplified query here) + $sql = mysqli_query($mysqli, " + SELECT ticket_subject, ticket_details + FROM tickets + WHERE ticket_id = $ticket_id + LIMIT 1 + "); + $row = mysqli_fetch_assoc($sql); + $ticket_subject = $row['ticket_subject']; + $ticket_details = strip_tags($row['ticket_details']); // strip HTML for cleaner prompt + + // Get ticket replies + $sql_replies = mysqli_query($mysqli, " + SELECT ticket_reply, ticket_reply_type + FROM ticket_replies + WHERE ticket_reply_ticket_id = $ticket_id + AND ticket_reply_archived_at IS NULL + ORDER BY ticket_reply_id ASC + "); + + $all_replies_text = ""; + while ($reply = mysqli_fetch_assoc($sql_replies)) { + $reply_type = $reply['ticket_reply_type']; + $reply_text = strip_tags($reply['ticket_reply']); + $all_replies_text .= "\n[$reply_type]: $reply_text"; + } + + // Craft a prompt for ChatGPT + $prompt = "Summarize the following ticket and its responses in a concise and clear way. The summary should be short, highlight the main issue, the actions taken, and any resolution steps:\n\nTicket Subject: $ticket_subject\nTicket Details: $ticket_details\nReplies:$all_replies_text\n\nShort Summary:"; + + // Prepare the POST data + $post_data = [ + "model" => "$config_ai_model", + "messages" => [ + ["role" => "system", "content" => "You are a helpful assistant."], + ["role" => "user", "content" => $prompt] + ], + "temperature" => 0.7 + ]; + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $config_ai_url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Content-Type: application/json', + 'Authorization: Bearer ' . $config_ai_api_key, + ]); + curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data)); + + $response = curl_exec($ch); + if (curl_errno($ch)) { + echo "Error: " . curl_error($ch); + exit; + } + curl_close($ch); + + $response_data = json_decode($response, true); + $summary = $response_data['choices'][0]['message']['content'] ?? "No summary available."; + + // Print the summary + echo nl2br(htmlentities($summary)); +} diff --git a/ticket.php b/ticket.php index 240423ff..821519ba 100644 --- a/ticket.php +++ b/ticket.php @@ -379,6 +379,12 @@ if (isset($_GET['ticket_id'])) {
+ + + + = 2) { ?> Invoice @@ -1180,6 +1186,25 @@ require_once "footer.php"; ?> + + + @@ -1191,3 +1216,5 @@ require_once "footer.php"; + +