代码之家  ›  专栏  ›  技术社区  ›  MjM

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

  •  -1
  • MjM  · 技术社区  · 5 年前

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

    我的问题是如何在osticket中创建一个客户端回复API,以及从何处开始。

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

    请帮忙。

    1 回复  |  直到 5 年前
        1
  •  0
  •   MjM    5 年前

    在文件中添加以下代码 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'
    );