forked from LivePersonInc/node-agent-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
greeting-bot.js
97 lines (75 loc) · 3.08 KB
/
greeting-bot.js
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
87
88
89
90
91
92
93
94
95
96
97
'use strict';
const Agent = require('./../../lib/AgentSDK');
class GreetingBot extends Agent {
constructor(conf) {
super(conf);
this.conf = conf;
this.openConvs = {};
this.on('connected', this.onConnected.bind(this));
// Notification on changes in the open consversation list
this.on('cqm.ExConversationChangeNotification', this.onConversationNotification.bind(this));
this.on('error', err => console.log('got an error', err));
this.on('closed', data => {
// For production environments ensure that you implement reconnect logic according to
// liveperson's retry policy guidelines: https://developers.liveperson.com/guides-retry-policy.html
console.log('socket closed', data);
clearInterval(this._pingClock);
this.reconnect(); //regenerate token for reasons of authorization (data === 4401 || data === 4407)
});
}
onConnected() {
console.log('connected...');
this.setAgentState({ availability: 'AWAY' }); // Do not route me conversations, I'll join by myself.
this.subscribeExConversations({
'convState': ['OPEN'] // subscribes to all open conversation in the account.
});
this._pingClock = setInterval(this.getClock, 30000);
}
onConversationNotification(notificationBody) {
notificationBody.changes.forEach(change => {
if (change.type === 'UPSERT') {
// ignore if we've already joined
if (this.openConvs[change.result.convId]) return;
// add the conversation to the list
this.openConvs[change.result.convId] = change.result;
// determine if we've already joined the conversation
let isParticipant = this.getParticipantInfo(change.result.conversationDetails, this.agentId);
// if not, attempt a join
if (!isParticipant) {
this.joinConversation(change.result);
}
}
else if (change.type === 'DELETE') {
delete this.openConvs[change.result.convId];
console.log('conversation was closed.\n');
}
});
}
joinConversation(conversation) {
this.updateConversationField({
'conversationId': conversation.convId,
'conversationField': [{
'field': 'ParticipantsChange',
'type': 'ADD',
'role': 'MANAGER'
}]
}, () => {
this.onConversationJoin(conversation)
});
}
onConversationJoin(conversation) {
// send a greeting
this.publishEvent({
dialogId: conversation.convId,
event: {
type: 'ContentEvent',
contentType: 'text/plain',
message: 'welcome from bot'
}
});
}
getParticipantInfo(convDetails, participantId) {
return convDetails.participants.filter(p => p.id === participantId)[0];
}
}
module.exports = GreetingBot;