Search code examples
phposticket

How to implement Customer reply API in osTicket


I have been working on osTicket to implement an API for customer reply. I have configured that incoming emails from customers which are transformed to new ticket then I had walked through the cron job code that inetiates mail fetch. I found some of APIs that used for staff reply and I have implemented that API.

My question is how to make a client reply API in osTicket and where should I start.

If anyone all ready implemented it then give me the git repository link.

Please help.


Solution

  • Add the following code in file upload/api/http.php to establish routing.

    ......
    $dispatcher = patterns('',
      url_post("^/tickets/clientReply$", 
      array('api.tickets.php:TicketApiController','postClientReply')),
      .......
    

    Then, add the following code in file 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;
    }
    

    Add the following code in file upload/include/class.config.php

    .....
    static $commonParams = array(
        'ownerMessageType' => 'M',
        'htmlFormat' => 'html',
        'sqlFunctionNow' => 'NOW()',
        'defaultDateTime' => '0000-00-00 00:00:00',
        'closedTicketStatus' => 'closed'
    );