如何在 osTicket 中实现客户回复 API
How to implement Customer reply API in osTicket
我一直致力于 osTicket 以实现 API 客户回复。
我已经将来自客户的传入电子邮件配置为转换为新票证,然后我已经完成了启动邮件获取的 cron 作业代码。
我发现了一些用于工作人员回复的 API,我已经实现了 API。
我的问题是如何在 osTicket 中让客户回复 API 以及我应该从哪里开始。
如果有人准备好实施它,请给我 git 存储库 link。
请帮忙。
在文件upload/api/http.php
中添加以下代码建立路由。
......
$dispatcher = patterns('',
url_post("^/tickets/clientReply$",
array('api.tickets.php:TicketApiController','postClientReply')),
.......
然后,在文件upload/include/api.tickets.php
中添加以下代码
//client reply API
function postClientReply() {
try {
if (!($key = $this->requireApiKey()) || !$key->canCreateTickets())
return $this->exerr(401, __('API key not authorized'));
$errors = $this->validateParams($_POST);
// echo count($errors);
if (!count($errors)) {
$user = TicketUser::lookupByEmail($_POST['clientUserMail']);
$data['id'] = Ticket::getIdByNumber($_POST['ticketNumber']); //ticket id
$data['userId'] = $user->getId(); //user id
$data['staffId'] = 0;
$data['poster'] = ''; //$user->getUserName();
$data['ip_address'] = $_POST['ip_address']; //'::1';
$data['type'] = OsticketConfig::$commonParams['ownerMessageType']; //'M' - Message owner
$data['flags'] = ThreadEntry::FLAG_BALANCED; // HTML does not need to be balanced on ::display()
$data['body'] = $_POST['response'];
$data['html'] = OsticketConfig::$commonParams['htmlFormat']; //html
$data['created'] = OsticketConfig::$commonParams['sqlFunctionNow']; //SqlFunction NOW();
$data['updated'] = OsticketConfig::$commonParams['defaultDateTime'];
// print_r($data); die();
$sql = 'INSERT INTO ' . THREAD_ENTRY_TABLE . ' (`id` ,`thread_id` ,`staff_id` ,`user_id` ,`type` ,`flags` ,`poster` ,`editor` ,`editor_type` ,`source` ,`title` ,`body` ,`format` ,`ip_address` ,`created` ,`updated`)
VALUES (NULL , ' . $data['id'] . ', ' . $data['staffId'] . ', ' . $data['userId'] . ', "' . $data['type'] . '", ' . $data['flags'] . ', "' . $data['poster'] . '", NULL , NULL , "API" , "" , "' . $data['body'] . '", "' . $data['html'] . '", "' . $data['ip_address'] . '", ' . $data['created'] . ', "' . $data['updated'] . '")';
if (!$res = db_query($sql)) {
$message = ['status' => 'failed', 'message' => 'SQL query not executed'];
}
$message = ['status' => 'success', 'message' => 'Reply posted succesfully'];
} else {
$message = ['status' => 'failed', 'errors' => $errors];
}
$result_code = 200;
$this->response($result_code, json_encode($message), $contentType = "application/json");
} catch (Throwable $e) {
$msg = $e->getMessage();
$result = array('tickets' => array(), 'status_code' => 'FAILURE', 'status_msg' => $msg);
$this->response(500, json_encode($result), $contentType = "application/json");
}
}
function validateParams($params) {
// print_r($params); die();
$errors = array();
if (empty($params['clientUserMail'])) {
$errors[] = 'client User Mail is missing';
}
if (empty($params['response'])) {
$errors[] = 'Message is missing';
}
if (!empty($params['ticketNumber'])) {
$id = Ticket::getIdByNumber($params['ticketNumber']);
if ($id <= 0) {
$errors[] = "Ticket not found";
} else {
//To check the ticket status, which may be closed
$ticket = Ticket::lookup($id);
$ticket_status = $ticket->getState();
if ($ticket_status == OsticketConfig::$commonParams['closedTicketStatus']) {
$errors[] = "You can't make reply, Ticket closed";
}
}
} else {
$errors[] = 'Ticket number is missing';
}
return $errors;
}
在文件中添加以下代码upload/include/class.config.php
.....
static $commonParams = array(
'ownerMessageType' => 'M',
'htmlFormat' => 'html',
'sqlFunctionNow' => 'NOW()',
'defaultDateTime' => '0000-00-00 00:00:00',
'closedTicketStatus' => 'closed'
);
我一直致力于 osTicket 以实现 API 客户回复。 我已经将来自客户的传入电子邮件配置为转换为新票证,然后我已经完成了启动邮件获取的 cron 作业代码。 我发现了一些用于工作人员回复的 API,我已经实现了 API。
我的问题是如何在 osTicket 中让客户回复 API 以及我应该从哪里开始。
如果有人准备好实施它,请给我 git 存储库 link。
请帮忙。
在文件upload/api/http.php
中添加以下代码建立路由。
......
$dispatcher = patterns('',
url_post("^/tickets/clientReply$",
array('api.tickets.php:TicketApiController','postClientReply')),
.......
然后,在文件upload/include/api.tickets.php
//client reply API
function postClientReply() {
try {
if (!($key = $this->requireApiKey()) || !$key->canCreateTickets())
return $this->exerr(401, __('API key not authorized'));
$errors = $this->validateParams($_POST);
// echo count($errors);
if (!count($errors)) {
$user = TicketUser::lookupByEmail($_POST['clientUserMail']);
$data['id'] = Ticket::getIdByNumber($_POST['ticketNumber']); //ticket id
$data['userId'] = $user->getId(); //user id
$data['staffId'] = 0;
$data['poster'] = ''; //$user->getUserName();
$data['ip_address'] = $_POST['ip_address']; //'::1';
$data['type'] = OsticketConfig::$commonParams['ownerMessageType']; //'M' - Message owner
$data['flags'] = ThreadEntry::FLAG_BALANCED; // HTML does not need to be balanced on ::display()
$data['body'] = $_POST['response'];
$data['html'] = OsticketConfig::$commonParams['htmlFormat']; //html
$data['created'] = OsticketConfig::$commonParams['sqlFunctionNow']; //SqlFunction NOW();
$data['updated'] = OsticketConfig::$commonParams['defaultDateTime'];
// print_r($data); die();
$sql = 'INSERT INTO ' . THREAD_ENTRY_TABLE . ' (`id` ,`thread_id` ,`staff_id` ,`user_id` ,`type` ,`flags` ,`poster` ,`editor` ,`editor_type` ,`source` ,`title` ,`body` ,`format` ,`ip_address` ,`created` ,`updated`)
VALUES (NULL , ' . $data['id'] . ', ' . $data['staffId'] . ', ' . $data['userId'] . ', "' . $data['type'] . '", ' . $data['flags'] . ', "' . $data['poster'] . '", NULL , NULL , "API" , "" , "' . $data['body'] . '", "' . $data['html'] . '", "' . $data['ip_address'] . '", ' . $data['created'] . ', "' . $data['updated'] . '")';
if (!$res = db_query($sql)) {
$message = ['status' => 'failed', 'message' => 'SQL query not executed'];
}
$message = ['status' => 'success', 'message' => 'Reply posted succesfully'];
} else {
$message = ['status' => 'failed', 'errors' => $errors];
}
$result_code = 200;
$this->response($result_code, json_encode($message), $contentType = "application/json");
} catch (Throwable $e) {
$msg = $e->getMessage();
$result = array('tickets' => array(), 'status_code' => 'FAILURE', 'status_msg' => $msg);
$this->response(500, json_encode($result), $contentType = "application/json");
}
}
function validateParams($params) {
// print_r($params); die();
$errors = array();
if (empty($params['clientUserMail'])) {
$errors[] = 'client User Mail is missing';
}
if (empty($params['response'])) {
$errors[] = 'Message is missing';
}
if (!empty($params['ticketNumber'])) {
$id = Ticket::getIdByNumber($params['ticketNumber']);
if ($id <= 0) {
$errors[] = "Ticket not found";
} else {
//To check the ticket status, which may be closed
$ticket = Ticket::lookup($id);
$ticket_status = $ticket->getState();
if ($ticket_status == OsticketConfig::$commonParams['closedTicketStatus']) {
$errors[] = "You can't make reply, Ticket closed";
}
}
} else {
$errors[] = 'Ticket number is missing';
}
return $errors;
}
在文件中添加以下代码upload/include/class.config.php
.....
static $commonParams = array(
'ownerMessageType' => 'M',
'htmlFormat' => 'html',
'sqlFunctionNow' => 'NOW()',
'defaultDateTime' => '0000-00-00 00:00:00',
'closedTicketStatus' => 'closed'
);