-
Notifications
You must be signed in to change notification settings - Fork 0
/
embed.js
113 lines (99 loc) · 2.83 KB
/
embed.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
var Botkit = {
setCookie: function(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
},
getCookie: function(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
},
active: false,
activate: function() {
this.active = true;
if (this.container) {
this.container.className = 'active';
}
this.setCookie('botkit_messenger_active', this.active);
},
deactivate: function() {
this.active = false;
if (this.container) {
this.container.className = '';
}
this.setCookie('botkit_messenger_active', this.active);
},
toggle: function() {
if (this.active) {
this.deactivate();
} else {
this.activate();
}
},
trigger: function(event) {
this.chatClient.postMessage(event, '*');
},
receiveMessage: function(message) {
// message contains the following fields:
// message.data, message.origin, message.source
switch (message.data.name) {
case 'booted':
Botkit.trigger({
name: 'connect',
user: Botkit.current_user ? Botkit.current_user : null,
});
if (Botkit.getCookie('botkit_messenger_active') == 'true') {
Botkit.activate();
}
console.log('Embedded Botkit: Ready!');
break;
case 'connected':
// console.log('100% CONNECTED AND READY TO GO');
break;
}
},
triggerScript: function(script, thread) {
this.trigger({
type: 'event',
name: 'trigger',
script: script,
thread: thread,
});
},
identifyUser: function(user) {
// user should contain any of the following:
// id, email, name, first_name, last_name, full_name, gender, timezone, timezone_offset
this.current_user = user;
this.trigger({
type: 'event',
name: 'identify',
user: user,
});
},
boot: function(user) {
var that = this;
that.container = document.getElementById('embedded_messenger');
that.header = document.getElementById('messenger_header');
that.chatClient = document.getElementById('botkit_client').contentWindow;
if (user) {
that.current_user = user;
}
if (!that.chatClient) {
console.error('Cannot find Botkit chat client iframe. Make sure your iframe has the id #botkit_client');
}
window.addEventListener('message', that.receiveMessage, false);
return this;
}
}