Export Conversations to CSV file

Let's imagine you want to export conversations of specific department for certain date range to csv file.
LiveAgent user interface doesn't offer the option to export tickets into csv, because it can potentially generate Gigs of data, which you will not be able to download using browser.

The best way to export such amount of data is to use REST API calls.

Here is short example how to export all data from tickets, which passed certain criteria:

<?php

//URL TO YOUR LIVEAGENT INSTALLATION
const LIVEAGENT_API_URL = 'https://www.example.com/api/';

//YOUR API KEY YOU CAN FIND IN MENU Configuration -> System -> Api
const API_KEY = 'efafeb2d6522964ba137870b3a5124d7';



// Helper function to make API call request
function sendRequest($apiCommand, $params = array()) {
    $params['apikey'] = API_KEY;
    $paramsStr = '';
    foreach ($params as $param => $value) {
        $paramsStr .= $param . '=' . urlencode($value) . '&';
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, LIVEAGENT_API_URL . $apiCommand . '?' . rtrim($paramsStr, '&'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $curl_response = curl_exec($ch);
    if ($curl_response === false) {
        $info = curl_error($ch);
        curl_close($ch);
        die("error occured during curl exec. Additioanl info: " . var_export($info));
    }
    curl_close($ch);
    return json_decode($curl_response);
}

//open CSV file for writing
$fp = fopen('tickets.csv', 'w');

// get list of all conversations from selected department and date range
$conversations = sendRequest('conversations', array(
        'datefrom' => '2015-09-20 00:00:00',
        'dateto' => '2015-09-21 00:00:00',
        'department' => 'd564fd60'      //DEPARMENT ID
));


if (isset($conversations->response->conversations)) {
    foreach ($conversations->response->conversations as $conversation) {
        $conversationMessages = sendRequest('conversations/' . $conversation->conversationid . '/messages');

        if (isset($conversationMessages->response->groups)) {
            foreach ($conversationMessages->response->groups as $group) {
                $messagesStr = '';
                if (isset($group->messages)) {
                    foreach ($group->messages as $message) {
                        $messagesStr .= $message->userid . ' (' . $message->datecreated . '): ' . $message->message . "\n";
                    }
                }

                fputcsv($fp, array(
                        $conversation->conversationid,
                        $conversation->datecreated,
                        $conversation->departmentname,
                        $conversation->ownername,
                        $conversation->owneremail,
                        $conversation->publicurlcode,
                        $group->datecreated,
                        $group->datefinished,
                        $messagesStr
                ));
            }
        }
    }
}

fclose($fp);
?>

 

This is not by any mean complete export solution, it is just example how to get ticket data from LiveAgent. The way you will format those data is up to your imagination.

You can play with this example and e.g. skip from export certain types of messages (e.g. exclude system messages) or store data in different file format (e.g. write it to PDF or XML file)

×