add hook file

This commit is contained in:
Your Name 2024-02-23 16:54:58 +05:00
parent e980c95c95
commit e04959e7ae

View File

@ -0,0 +1,190 @@
<?php
// Import necessary WHMCS classes
use WHMCS\Database\Capsule;
use GuzzleHttp\Client;
// Function to get ticket data and related information
function getTicketData($tid) {
try {
// Perform database query
$result = Capsule::table('tbltickets as tt')
->join('tblhosting as th', Capsule::raw('SUBSTRING(tt.service, 2)'), '=', 'th.id')
->join('tblservers as ts', 'th.server', '=', 'ts.id')
->leftJoin('tblcustomfieldsvalues as cfv', function($join) {
$join->on('tt.id', '=', 'cfv.relid')
->where('cfv.fieldid', '=', function($query) {
$query->select('id')
->from('tblcustomfields')
->where('fieldname', 'Issue Type (*)')
->where('type', 'support')
->limit(1);
});
})
->where('tt.id', $tid)
->select('tt.title', 'tt.message', 'th.domain', 'ts.name', 'ts.ipaddress', 'ts.hostname', 'cfv.value as issueType')
->get();
if ($result->isEmpty()) {
return "No data found for ticket ID: " . htmlspecialchars($tid);
}
// Add ticket information to data array
$data = [];
foreach ($result as $row) {
$data[] = [
'Title' => $row->title,
'Message' => $row->message,
'Domain' => $row->domain,
'ServerName' => $row->name,
'IPAddress' => $row->ipaddress,
'Hostname' => $row->hostname,
'IssueType' => $row->issueType
];
}
} catch (\Exception $e) {
return "An error occurred: " . $e->getMessage();
}
return $data;
}
// Get logs
function getLogs($server, $issueType) {
$client = new Client();
try {
// GET request with a 10-second timeout
$response = $client->get('http://' . urlencode($server) . ':5000/get_logs?issue_type=' . urlencode($issueType), [
'headers' => [
'Content-Type' => 'application/json',
],
'timeout' => 10, // Timeout set to 10 seconds
]);
$responseBody = json_decode($response->getBody(), true);
return $responseBody;
} catch (Exception $e) {
logActivity('error getLogsApi: '. $e->getMessage() );
return null; // or return a specific structure indicating failure
}
}
// Get OpenAI response
function callOpenAI($apiKey, $apiEngine, $subject, $message, $logsData) {
try {
// Constructing the prompt
$prompt = "We have received a Technical Support Ticket from a client. Having subject: $subject, ticket content: $message. Following are the server logs with file paths: $logsData. Please guide us solution of this with explanations.";
// Data for the OpenAI API call
$requestData = array(
"model" => $apiEngine,
// "response_format" => array("type" => "json_object"),
"messages" => array(
array("role" => "system", "content" => "You are a helpful sysops. You have to guide a effective solution."),
array("role" => "user", "content" => $prompt) // Using constructed prompt
)
);
$client = new Client();
$response = $client->post('https://api.openai.com/v1/chat/completions', [
'headers' => [
'Content-Type' => 'application/json',
"Authorization" => "Bearer $apiKey"
],
'json' => $requestData,
]);
$responseArray = json_decode($response->getBody(), true);
// Accessing choices
$choice = $responseArray['choices'][0];
$responseBody = $choice['message']['content'];
return $responseBody;
} catch (Exception $e) {
logActivity('error callOpenAI: '. $e->getMessage() );
// return "Error: " . $e->getMessage();
}
}
// Add ticket notes
function addNotesToTicket($ticketId, $note) {
try {
$command = 'AddTicketNote';
$postData = array(
'ticketid' => $ticketId,
'message' => "Suggestion by ChatGPT.
PLEASE NOTE THAT THIS MAY NOT BE CORRECT THEREFORE PLEASE READ AND UNDERSTAND CAREFULLY BEFORE DOING ANYTHING." . $note,
'markdown' => true
//,
//'attachments' => base64_encode(json_encode([['name' => 'sample_text_file.txt', 'data' => base64_encode('This is a sample text file contents')]])),
);
$results = localAPI($command, $postData);
logActivity("Ticket note added for ticket ID: {$ticketId}");
} catch (Exception $e) {
logActivity("Error adding ticket note: " . $e->getMessage());
}
}
function openTicketHookFunc($vars) {
// Retrieve the Ticket ID and Display Data
$tid = $vars['ticketid'] ?? '';
// Retrieve and display module configuration values
$aiApiKey = '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';
$aiEngine = "gpt-3.5-turbo-1106";
if (!empty($tid)) {
$ticketData = getTicketData($tid);
if (is_array($ticketData)) {
foreach ($ticketData as $row) {
// get logs
// $server = "localhost";
// $issueType = "Demo";
$server = htmlspecialchars($row['ServerName']);
$issueType = htmlspecialchars($row['IssueType']);
$logsDataArray = getLogs($server, $issueType);
if ($logsDataArray !== null) {
$logsData = '';
foreach ($logsDataArray as $key => $value) {
$logsData .= "\n" . "file: " . $key . "\n" . "logs: " . $value;
}
// Call the ChatGPT API
$openAIResponse = callOpenAI($aiApiKey, $aiEngine, $row['Title'], $row['Message'], $logsData);
addNotesToTicket($tid, $openAIResponse);
}
}
}
}
};
add_hook('TicketOpenAdmin', 1, function($vars) {
// logActivity('before hooks from ARH - TicketOpenAdmin');
openTicketHookFunc($vars);
// logActivity('after hooks from ARH - TicketOpenAdmin');
});
add_hook('TicketOpen', 1, function($vars) {
// logActivity('hooks from ARH - TicketOpen');
openTicketHookFunc($vars);
});