Clook Life

LiveChat webhook script for WHMCS

Some may have noticed that we have been using a new live chat system on our website to handle any sales questions from site visitors.  After testing a lot of different systems (which is for another blog post!) we settled on LiveChat.

One of the great features of this excellent chat system is that of Webhooks whereby information from a chat can be sent to a third party script or application to gather additional data to put into the staff chat console.

For any clients (or non clients) using LiveChat along with the WHMCS client management system I thought I’d put the instructions here on how to integrate the two by grabbing client details from WHMCS to show in the LiveChat window.

1.  Make sure the chat “Pre-chat survey” is enabled in LiveChat to ask for an email address from the visitor when starting a new chat.

2.  Add a new webhook:

livechat1

 

3.  Put the following script into a file of the same name and uploaded to the same location entered in the web hook config.  Make sure to modify the variable at the top called ‘$path_to_whmcs’ which should contain the path to your WHMCS root folder.

<?php
//
// Webhook script for use with LiveChat (livechatinc.com) 
// to search WHMCS database for email address entered by 
// visitor and import into agent chat window
//
// Link: https://www.clook.net/blog/livechat-webhook-script-for-whmcs/
//

// path to whmcs install folder (required)
$path_to_whmcs = '/home/username/public_html/whmcs';

// url to 64x64px whmcs logo image - WITHOUT http(s):// (optional)
$logo_image = '';

///////////////////////////////////////////////

// read the webhook sent by LiveChat
$data = file_get_contents('php://input');
$data = json_decode($data);

// make sure the "chat_started" event occured
if ($data->event_type === 'chat_started' AND $data->visitor->email != '')
{
	// create fields array
	$fields = array();
	
	// grab email address passed from livechatinc.com
	$email = filter_var($data->visitor->email, FILTER_SANITIZE_EMAIL);
	
	// whmcs include
	require($path_to_whmcs."/init.php");
	
	$table = "tblclients";
	$dbfields = "id,firstname,lastname,companyname";
	$where = array("email"=>$email);
	$result = select_query($table,$dbfields,$where);
	$dbdata = mysql_fetch_array($result);
	if(mysql_num_rows($result) > 0)
	{
		//
		// direct client match
		//
		$whmcsdata['profile'] = $CONFIG['SystemURL']."/admin/clientssummary.php?userid=".$dbdata['id'];
		$whmcsdata['name'] = $dbdata['firstname']." ".$dbdata['lastname'];
		if($dbdata['companyname']) { $whmcsdata['name'] .= " @ ".$dbdata['companyname']; }
		
		$fields = array();
		$fields[] = (object)array(
			'name' => 'Direct Client',
			'value' => $whmcsdata['name']
		);
		$fields[] = (object)array(
			'name' => 'WHMCS Link',
			'value' => $whmcsdata['profile']
		);
	}
	else
	{
		//
		// search contacts
		//
		$cnt=0;
		$table = "tblcontacts";
		$dbfields = "userid,firstname,lastname,companyname";
		$where = array("email"=>$email);
		$result = select_query($table,$dbfields,$where);
		while ($dbdata = mysql_fetch_array($result)) {
			$whmcsdata['profile'] = $CONFIG['SystemURL']."/admin/clientssummary.php?userid=".$dbdata['userid'];
			$whmcsdata['name'] = $dbdata['firstname']." ".$dbdata['lastname'];
			if($dbdata['companyname']) { $whmcsdata['name'] .= " @ ".$dbdata['companyname']; }
			$cnt++;
			
			$fields[] = (object)array(
				'name' => 'Auth Contact '.$cnt,
				'value' => $whmcsdata['name']
			);
			$fields[] = (object)array(
				'name' => 'WHMCS Link',
				'value' => $whmcsdata['profile']
			);
		}
	}

	$curlFields = http_build_query(array(
		'license_id' => $data->license_id,
		'token' => $data->token,
		'id' => 'whmcs-lookup',
	
		// Do not enter "http" prefix in the icon URL.
		// Your server must be able to serve the icon
		// using both https:// and http:// protocols.
		'icon' => $logo_image,
	
		'fields' => $fields
	));
	
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, 'https://api.livechatinc.com/visitors/'.$data->visitor->id.'/details');
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $curlFields);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-API-Version: 2'));
	$result = curl_exec($ch);
	curl_close($ch);
}
?>

 

4.  When a new chat comes in, if the visitors email address is found in WHMCS either as a direct client or additional contact these details will be shown to the staff member handling the chat.

livechat2

 

Enjoy!

Finally, please note that we don’t guarantee this script in any way or provide any support for it.  I ask that any questions or comments are made to this blog post below where I will try to help.

Clook Logo

Get regular tips, tricks and updates!

Signup today for our awesome newsletter and recieve monthly emails with all sorts of helpful articles on hosting and web development.