如何在osTicket中实现客户回复API

时间:2019-01-26 11:23:00

标签: php osticket

我一直在致力于osTicket的实现,以实现客户回复的API。 我已经配置了将来自客户的传入电子邮件转换为新票证,然后遍历了激发邮件提取的cron作业代码。 我找到了一些用于员工回复的API,并且已经实现了该API。

我的问题是如何在osTicket中制作客户端回复API,我应该从哪里开始。

如果任何人都准备好实施,那么给我git存储库链接。

请帮助。

1 个答案:

答案 0 :(得分:0)

在文件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'
);