JavaScript API tutorial

From LIVECHAT Developer Zone Wiki

Jump to: navigation, search

Note: This section applies only to LIVECHAT Enterprise Edition

JavaScript API helps you to provide system custom attributes of your customers visiting your website. This is used to present more information about your visitors in the agent's console window. You can customize monitoring code and chat button code by adding more parameters with custom data about particular customer. For security reasons we recommend to sign the extended attributes using the public-key cryptography. This is used to ensure authenticity of the visitor details.

Important note: Signing the parameters is optional

Image:customdata.jpg

Interface

Standard chat button code has the following format of the url address used to open the chat with an agent:

http(s)://<server_address>/licence/<licence_number>/open_chat.cgi?<parameters>

In example, url address for the licence number 12345 looks as follows:

http(s)://livechat.mydomain.com/licence/12345/open_chat.cgi?lang=en&groups=10,5,0

You can modify this source code by adding more paremeters, this will enable you to pass more information about the visitors to the agent's application console:

  • params - placeholder for custom extended attributes (URLEncoded, UTF-8), an agent will have this attributes presented in an additional column on the visitors' tab

Optional:

  • sign – digital signature of the params parameter.

Sample url to open the chat with digitally signed paremeters (important parameters: params & sign):

http://system.livechat.pl/licence/123456/open_chat.cgi?lang=pl&groups=10,5,
0&params=time%3D1129812087080%26id%3Dhttp%3A%2F%2Fxdsl1158.wroclaw.dialog.n
et.pl%2Flicence%2F100100%2Fscript.cgi%26login%3Dtest1%26name%3DKlient+Pi
otrka+S&sign=134736c909dc98a10735b5b0a6ffa2449805a44d3880c8014b1e99cc5d52bc
f332e8692cbc48db6e045e205996ddecf67ffa6eb5e39755efeb2e359b1d06228d70e69ae00
5ab5b5c349069f288dd4a795be37da327ca3994a3e071e01d037de8b280a7f5447509e35282
21c5205132a358bd9373534cd7ac9e80149338127eb0

Sample script

In this section you will learn how to write sample perl script to generate monitoring code with digitally signed paremeters. This code uses public-key cryptography and openssl library to digitally sign the parameters of monitoring code for the licence no. 1520 (checkout you licence number and update the source code before using it).

Source code file (livechat_sign.pl):

#!/usr/bin/perl

use strict;

#sample parameters
my $PARAMETERS = "Name=John Brown;Age=32;";

#temporary files
my $TMP_params = "/tmp/params.tmp";
my $TMP_sign   = "/tmp/sign.tmp";

#include private key file
my $KEY = "/home/users/livechat/sign/lc_licence.key";

#create params file
open(FILE, ">$TMP_params");
print FILE $PARAMETERS;
close (FILE);

#generate sign
`openssl dgst -sign $KEY $TMP_params >$TMP_sign`;

#read sing to variable
open(FILE, "<$TMP_sign");
binmode(FILE);
read(FILE, my $SIGN, -s $TMP_sign, 0);
close(FILE);

#remove temporary files
system("rm $TMP_params $TMP_sign");

#prepare variables for html
$SIGN = join "", unpack "H*", $SIGN;
my $params_URLencode = &URLEncode($PARAMETERS);

print <<EOF;
Content-type: text/html\n\n

<!-- BEGIN LIVECHAT button tag. See also www.livechatinc.com -->
<table border='0' cellspacing='0'  cellpadding='0'><tr><td align='center'>
<img src="http://server.livechatinc.net/licence/1520/button.cgi?lang=en&groups=13"
style="cursor:pointer;cursor:hand" onclick=
"window.open('http://server.livechatinc.net/licence/1520/open_chat.cgi?params=$params_URLencode&sign=$SIGN&groups=13'+
'&s=1&lang=en&dc='+escape(document.cookie+';l='+document.location+';r='+
document.referer+';s='+typeof lc_session),'Czat_1520',
'width=604,height=247,resizable=no,scrollbars=no,status=1');"/></td></tr>
<tr><td  align='center'><a href='http://www.livechatinc.com' style='text-decoration:none'
target='_blank'><font face='Tahoma' size='1' color='#333333'><span style='font-size: 10px'>
powered by <font color='#475780'>LIVECHAT</font></td></tr></table>
<!-- END LIVECHAT button tag. See also www.livechatinc.com -->
EOF

sub URLEncode {
    my $theURL = $_[0];
    $theURL =~ s/([\W])/"%" . uc(sprintf("%2.2x",ord($1)))/eg;
    return $theURL;
}

Cryptographic keys

Sample private key file lc_licence.key:

-----BEGIN RSA PRIVATE KEY-----
MIICXQIBAAKBgQC46rKENzF/BvKq12BohM6ifkmRTY3YIL1VTGsjV1oFfdZRcsIh
vc+NWgkPRyZE6E4EORuGPm3VccOLOVQOU3cKyVAKNDF+pKkecTlmnJrmd5X3iqEQ
V9Sa/1xjGqp3y/oK7tBCIS4Xrp+skUlKUfVo2pO0xxHL7NGUlDWBg6TGPQI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