I have been working on OpenAI integration to rewrite my reply's in a more professional manner.
Its a rough draft but functional.
Im sharing for the pro's to take it and run with it to make it so much better. My PHP programming knowledge is limited in comparison.
BACKUP OR MAKE COPIES BEFORE ALTERING!!
EDIT #2:
I have made more changes adding the settings to the Admin -> Hesk Settings option on Admin page.
OpenAI Hesk Documentation / Download
I have simplified it, Only manual edit is adding the settings to hesk_settings.inc.php
Then Extract the downloaded php files to your /admin or renamed admin folder.
This will allow you to tweak the OpenAI responses settings.

OLD METHOD:
hesk_settings.inc.php
Add the following to your settings php.
Code: Select all
// --> OpenAI settings
$hesk_settings['openai_enable']=1;
$hesk_settings['openai_model']='gpt-4.1-nano-2025-04-14'; // change to your proffered model
$hesk_settings['openai_max_tokens']=300;
$hesk_settings['openai_temperature']=0.7;
$hesk_settings['openai_api_key'] = 'API_KEY_CHANGE_ME';
$hesk_settings['openai_system_prompt'] = "Your name is Theodore, Your role is a <YOUR COMPANY> IT Manager dedicated to supporting and helping staff and customers. Your mission is to solve issues, answer questions, and provide guidance on IT-related matters. You are here to assist with any technical challenges that arise, ensuring a smooth and efficient IT experience for everyone at <YOUR COMPANY>.";
$hesk_settings['openai_user_prompt'] = "Rewrite the draft reply to be more helpful and complete."
Forum Post Edit: I added the button to click to create the AI suggestion. Sorry forgot to add this on original post.
Find:
Code: Select all
<div class="block--message" id="message-block">
Code: Select all
<?php
if ($can_delete && $hesk_settings['openai_enable'] == 1) { ?>
<button type="button" id="openai-suggest" class="btn btn-border" style="margin-bottom:10px;">Suggest with AI</button>
<div id="openai-loading" style="display:none;">Generating suggestion...</div>
<?php } ?>
Code: Select all
</script>
<!-- END REPLY FORM -->
Code: Select all
document.getElementById('openai-suggest').addEventListener('click', function() {
var toxEditor = tinymce.get('message');
// console.log(toxEditor.getContent({format: 'text'}));
// console.log(document.getElementById('message').value);
// var customerMessage = 'fetched ticket info; // get the ticket information or customer message if needed
var draftReply = toxEditor ? toxEditor.getContent({format: 'text'}) : document.getElementById('message').value;
if (!draftReply.trim()) {
alert('Please enter some ticket details before requesting an AI suggestion.');
return;
}
var prompt = /*"Customer message:\n" + customerMessage + "\n\n*/"Draft reply:\n" + draftReply + "\n\n" + <?php $hesk_settings['openai_user_prompt']; ?>
document.getElementById('openai-loading').style.display = 'block';
fetch('openai_suggest.php', {
method: 'POST',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: 'prompt=' + encodeURIComponent(prompt)
})
.then(response => response.json())
.then(data => {
document.getElementById('openai-loading').style.display = 'none';
console.log(data); // debuging output
if (data.choices && data.choices[0] && data.choices[0].message && data.choices[0].message.content) {
if (toxEditor) {
toxEditor.setContent(data.choices[0].message.content.replace(/\n/g, '<br>'));
} else {
document.getElementById('message').value = data.choices[0].message.content;
}
} else if (data.error) {
alert('AI suggestion failed: ' + (typeof data.error === 'object' ? JSON.stringify(data.error) : data.error));
} else {
alert('AI suggestion failed: Unknown error');
}
});
});
Add this to the your admin folder (or your renamed admin folder)
Code: Select all
<?php
define('IN_SCRIPT', 1);
require_once('../hesk_settings.inc.php');
header('Content-Type: application/json');
if (!isset($_POST['prompt']) || empty($hesk_settings['openai_api_key'])) {
echo json_encode(['error' => 'Missing prompt or API key']);
exit;
}
$prompt = $_POST['prompt'];
$apiKey = $hesk_settings['openai_api_key'];
$prompt = isset($_POST['prompt']) ? trim($_POST['prompt']) : '';
if ($prompt === '') {
echo json_encode(['error' => 'Prompt cannot be empty']);
exit;
}
if (!isset($hesk_settings['openai_model']) || !isset($hesk_settings['openai_system_prompt']) || !isset($hesk_settings['openai_max_tokens']) || !isset($hesk_settings['openai_temperature'])) {
echo json_encode(['error' => 'OpenAI settings are not properly configured']);
exit;
}
if (!function_exists('curl_init')) {
echo json_encode(['error' => 'cURL is not installed on the server']);
exit;
}
$data = [
'model' => $hesk_settings['openai_model'],
'messages' => [
['role' => 'system', 'content' => $hesk_settings['openai_system_prompt']],
['role' => 'user', 'content' => $prompt]
],
'max_tokens' => $hesk_settings['openai_max_tokens'],
'temperature' => $hesk_settings['openai_temperature'],
];
$ch = curl_init('https://api.openai.com/v1/chat/completions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Authorization: Bearer ' . $apiKey,
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
echo json_encode(['error' => curl_error($ch)]);
exit;
}
curl_close($ch);
$result = json_decode($response, true);
if ($http_code !== 200 || isset($result['error'])) {
echo json_encode(['error' => $result['error']['message'] ?? 'Unknown error from OpenAI']);
exit;
}
echo $response;