How to import contacts from csv

Although we don't offer this function as part of the LiveAgent application, an example script capable of importing contacts from csv file to LiveAgent via API v3 is attached below this article.

Your csv file must contain column headers on the first line. You then use those column names in keys of variable COLUMNS.

Configure the script in the first section

/***************** CONFIGURATION START *****************/
// agent panel -> settings -> system -> API
const APIKEY = 'test';

// e.g. https://test.ladesk.com/api/v3
const API_URL = 'https://127.0.0.1/LiveAgent/api/v3';

// e.g. ./data.csv
const FILE_PATH = './contacts.csv';

// Col1;Col2;Col3;
const DELIMITER = ';';

// csv column name => api column name
// columns which is not mentioned will be ignored
// HACK_1: 'csv_column' => 'name'
//   it will devide name into firstname and lastname
//   'aaa bbb ccc' will be firstname => 'aaa', lastname => 'bbb ccc'
// HACK_2: 'csv_column' => 'company_name'
//   it will try to add contact into company if company already exists
//   if company doesn't exist, it will create that company
// HACK_3: list phones as phoneN where N is a number, it will be processed into the needed format
// e.g. 'Mobile' => 'phone1', 'Landline' => 'phone2'
const COLUMNS = array(
    'Company' => 'company_name',
    'First Name' => 'firstname',
    'Last Name' => 'lastname',
    'Email' => 'emails',
    'Mobile No.' => 'phone1',
    'Landline' => 'phone2',
    'City' => 'city'
);
/***************** CONFIGURATION END *******************/

and execute it on command line.

 

Import with custom fields

In case you want to import your contacts with some custom fields, you have to use a bit more complicated code. First, the setup in header will need one more array for matching your CSV data:

// HACK_4: list custom fields as customN where N is a number it will be processed into the needed format
// e.g. 'Marital status' => 'custom1', 'Loan' => 'custom2'

const COLUMNS = array(
    'Company' => 'company_name',
    'First Name' => 'firstname',
    'Last Name' => 'lastname',
    'Email' => 'emails',
    'Mobile No.' => 'phone1',
    'Landline' => 'phone2',
    'City' => 'city',
    'Marital status' => 'custom1', // <--------------------
    'Loan' => 'custom2' // <--------------------
);

// you also have to use COLUMNS_CUSTOM_FIELDS - define custom field's 'code' (from your LiveAgent custom fields) for each 'customN' value
const COLUMNS_CUSTOM_FIELDS = array(
    'custom1' => 'marital',
    'custom2' => 'loan'
);

When this part is done, the following code has to be added to the handle_special_columns function:

$row['custom_fields'] = array();
$i = 1;
while (isset($row['custom'.$i])) {
    $row['custom_fields'][] = (object)[
        'code' => COLUMNS_CUSTOM_FIELDS['custom'.$i],
        'value' => $row['custom'.$i]
    ];
    unset($row['custom'.$i]);
    $i++;
}

Attached is a final file for you to use (importContactsCustomFields.php)

The further attached file (import_Contacts_with_c_groups_.php), helps you to import contacts to LiveAgent with their respective contact groups.

×