-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathLiveChatAPI.js
More file actions
86 lines (82 loc) · 2.57 KB
/
LiveChatAPI.js
File metadata and controls
86 lines (82 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var botId = "st-0954eedb-889c-5491-9f92-f5671760c942";
var botName = "LiveChat";
var sdk = require("./lib/sdk");
var Promise = require('bluebird');
var template = require('url-template');
var { makeHttpCall } = require("./makeHttpCall");
const qs = require('qs');
var initUrl = "https://api.livechatinc.com/visitors/{visitorId}/chat/start";
function initChat(visitorId, data){
var url = template.parse(initUrl).expand({visitorId: visitorId});
let formData = qs.stringify(data);
let headers = {
'content-type': 'application/x-www-form-urlencoded', // Set automatically
'X-API-Version': 2,
'Accept': 'application/json'
};
return makeHttpCall(
'post',
url,
formData,
headers
)
.then(function(res){
return res.data;
})
.catch(function(err){
return Promise.reject(err);
});
}
var sendMsgUrl = "https://api.livechatinc.com/visitors/{visitorId}/chat/send_message";
function sendMsg(visitorId, data){
var url = template.parse(sendMsgUrl).expand({visitorId: visitorId});
let formData = qs.stringify(data);
let headers = {
'content-type': 'application/x-www-form-urlencoded', // Set automatically
'X-API-Version': 2
};
var options = {
method: 'POST',
uri: url,
form: data,
headers: {
'content-type': 'application/x-www-form-urlencoded', // Set automatically
'X-API-Version': 2
}
};
return makeHttpCall(
'post',
url,
formData,
headers
)
.then(function(res){
return res.data;
})
.catch(function(err){
return Promise.reject(err);
});
}
var getMsgUrl = "https://api.livechatinc.com/visitors/{visitorId}/chat/get_pending_messages?licence_id={licence_id}&secured_session_id={secured_session_id}&last_message_id={last_message_id}";
function getPendingMessages(visitorId, ssid,last_message_id, licence_id){
var url = template.parse(getMsgUrl).expand({visitorId: visitorId, secured_session_id: ssid, licence_id: licence_id, last_message_id: last_message_id});
let headers = {
'content-type': 'application/x-www-form-urlencoded', // Set automatically
'X-API-Version': 2
};
return makeHttpCall(
'get',
url,
null,
headers
).
then(function(res){
return res.data;
})
.catch(function(err){
return Promise.reject(err);
});
}
module.exports.initChat = initChat;
module.exports.sendMsg = sendMsg;
module.exports.getPendingMessages = getPendingMessages;