この記事の下に、APIv3を使ってCSVファイルからLiveAgentに連絡先をインポートするためのサンプルスクリプトが添付されています。この機能はLiveAgentアプリケーションの標準機能としては提供されていませんが、スクリプトを活用することで実現できます。

CSVファイルの1行目には列ヘッダーが必要です。その列名を変数 COLUMNS のキーとして使用します。

スクリプトの最初のセクションで設定を行ってください。

/**************** 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 ******************/

設定が完了したら、コマンドラインで実行してください。

カスタムフィールドを使ったインポート

カスタムフィールドを含む連絡先をインポートしたい場合は、少し複雑なコードが必要です。まず、ヘッダー部分の設定にCSVデータとの対応付けのための配列を追加する必要があります。

// 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'
);

この部分の設定が完了したら、handle_special_columns 関数に以下のコードを追加する必要があります。

$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++;
}

ご利用いただける最終ファイル(importContactsCustomFields.php)が添付されています。

また、別途添付のファイル(import_Contacts_with_c_groups_.php)を使用すると、連絡先グループとともにLiveAgentへ連絡先をインポートすることができます。