0

我一直在研究 osTicket 来实现客户回复的 API。我已经配置了来自客户的传入电子邮件,这些电子邮件被转换为新票,然后我浏览了启动邮件获取的 cron 作业代码。我发现了一些用于员工回复的 API,并且我已经实现了该 API。

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

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

请帮忙。

4

1 回答 1

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'
);
于 2019-02-05T13:06:14.740 回答