Home > Uncategorized > Using @livechat #api to get car details for website visitors

Using @livechat #api to get car details for website visitors

logo

 

Livechat is a great system for chatting live to your website visitors. But sometimes time can be wasted ascertaining pertinent details from the customer that could be looked up automatically as the chat starts.

In this example, a car parts company wanted to know exactly what type of car the person as soon as the chat started.- Using our API at http://www.regcheck.org.uk

So, the first thing to do is to set up a pre-chat survey (under settings), adding a Question saying “What is your registration number”

prechat

Once this is enabled, the chat window should appear as shown above. In this example, it’s important that the question is the third in the list, but you can modify the webhook code below to change that.

Next set up a webhook, under settings > integrations. Here it’s important to ensure that visitor and pre_chat_survey are clicked, and chat starts event is selected. The target url should point to your website, and a PHP file called livechathook.php

webhook

Now, here’s some code, for the file livechathook.php

<?php

ini_set(‘display_errors’,true);
error_reporting(-1);

$client = new soapclient(‘http://www.regcheck.org.uk/api/reg.asmx?WSDL ‘, array(‘trace’ => 1));
// Set this username to your RegCheck username
$username = ‘your-username’; // Replace this!

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

file_put_contents(‘raw.txt’, $data);

try {
$json = json_decode($data);
$survey = $json->pre_chat_survey;
$question2 =$survey[2];
$answer2 =$question2->answer;
file_put_contents(‘reg.txt’, print_r($answer2, true));

// create an array of parameters
$param = array(
‘RegistrationNumber’ => $answer2,
‘username’ => $username);

$result = $client->Check($param);
$car = ‘Not found’;
file_put_contents(‘soapresponse.txt’, print_r($client->__getLastResponse(),true));
if (is_soap_fault($result)) {
file_put_contents(‘regerror.txt’,print_r($result,true));
} else {
file_put_contents(‘regdetails.txt’,print_r($result,true));
$regJson = $result->CheckResult->vehicleJson;
$djson = json_decode($regJson);
$car = $djson->Description;
}

$fields = array();
$fields[] = (object)array(
‘name’ => ‘Car’,
‘value’ => $car
);

$curlFields = http_build_query(array(
‘license_id’ => $json->license_id,
‘token’ => $json->token,
‘id’ => ‘my-integration’,
‘fields’ => $fields
));

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘https://api.livechatinc.com/visitors/ ‘ . $json->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’));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);

$CurlError = curl_error($ch);
file_put_contents(‘curlerror.txt’, print_r($CurlError, true));

curl_close($ch);

file_put_contents(‘curl.txt’, print_r($result, true));

}
catch(Exception $e) {
file_put_contents(‘error.txt’, $e->getMessage());
}

?>

The end result of this, is when you are chatting with a user, you now know the exact make and model of the car they are driving.

car lookup integration

It should be noted, that the above example assumes the user is from the UK, and you will have to change the $client->Check call to $client->CheckUSA or $client->CheckAustralia for other countries (* And you need to collect state information for USA and Australia too).

So, you might be interested to know how this works?

The webhook from LiveChatInc sends the following Json;

{
“event_type”:“chat_started”,
“event_unique_id”:“f35b043d61184384762ccf47fb761e5e”,
“token”:00d0a8995cb9eefa3bb2e6b91a812996,
“license_id”:7738991,
“visitor”:{
“id”:S1467215931.f4e3dfcee9,
“name”:“John Mairs”,
“email”:“John@mairs.com”,
“country”:“United Kingdom”,
“city”:“”,
“language”:“en”,
“page_current”:http://livechat.webtropy.com/&#8221;,
“timezone”:“”
},
“chat”:{
“id”:“O9CL40K8AT”,
“started_timestamp”:1467284861,
“url”:http://livechat.webtropy.com/&#8221;,
“messages”:[
{
“user_type”:“agent”,
“author_name”:“Fiach Reid”,
“agent_id”:“xxxxx@gmail.com”,
“text”:“Hello. How may I help you?”,
“timestamp”:1467284861
}
],
“attachments”:[

],
“events”:[

],
“agents”:[
{
“name”:“Fiach Reid”,
“login”:“fiach.reid@gmail.com”
}
],
“tags”:[

],
“groups”:[
0
]
},
“pre_chat_survey”:[
{
“id”:“146721615150601218”,
“type”:“name”,
“label”:“Name:”,
“answer”:“John Mairs”
},
{
“id”:“146721615150606648”,
“type”:“email”,
“label”:“E-mail:”,
“answer”:“John@mairs.com”
},
{
“id”:“146721615150604606”,
“type”:“question”,
“label”:“What is your registration number”,
“answer”:UKZ2955
}
]
}

The parts of interest are the response to the question – the registration number, the token, license and visitor id are required to feed information back to LiveChatInc.

In the code above, you can see that the car registration number is stored in $answer2, and this is then sent to the regcheck webservice, to determine the car details. If this succeeds, then the car description is stored in $car – The $fields object is then created with the car details stored in an associative array, this is then sent back to the LiveChatInc API to be associated with the current visitor.

The code above contains quite a bit of logging, which can be used for debugging purposes, but should be removed before going live.

Raw.Txt – The Json sent to the Webhook
reg.txt – The Visitors registration number
soapresponse.txt – XML returned from the webservice
regerror.txt – Any error returned from the webservice
regdetails.txt – The data returned from the webservice
curlerror.txt – Any error in calling the API
curl.txt – The result of calling the API
error.txt – Any PHP errors.

 

 

 

 

Advertisements
Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: