-
Notifications
You must be signed in to change notification settings - Fork 4
/
frontend.js
168 lines (150 loc) · 6.19 KB
/
frontend.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
$(function () {
"use strict";
window.chatwoot = {};
// update the inbox identifier
chatwoot.inboxIdentifier = "bXKr2F2asDjztoVipLF9Lt8v";
chatwoot.chatwootAPIUrl = "http://localhost:3000/public/api/v1/"
// for better performance - to avoid searching in DOM
var content = $('#content');
var input = $('#input');
var status = $('#status');
var xhttp = new XMLHttpRequest();
// if user is running mozilla then use it's built-in WebSocket
window.WebSocket = window.WebSocket || window.MozWebSocket;
// if browser doesn't support WebSocket, just show some notification and exit
if (!window.WebSocket) {
content.html($('<p>', { text: 'Sorry, but your browser doesn\'t '
+ 'support WebSockets.'} ));
input.hide();
$('span').hide();
return;
}
// open connection
var connection = new WebSocket('ws://localhost:3000/cable');
connection.onopen = function () {
// check whether we have a pubsub token and contact identifier or else set one
setUpContact();
setUpConversation();
// first we want users to subscribe to the chatwoot webhooks
connection.send(JSON.stringify({command:"subscribe", identifier: "{\"channel\":\"RoomChannel\",\"pubsub_token\":\""+chatwoot.contactPubsubToken+"\"}" }));
input.removeAttr('disabled');
status.text('Send Message:');
};
connection.onerror = function (error) {
// just in there were some problems with connection...
content.html($('<p>', { text: 'Sorry, but there\'s some problem with your '
+ 'connection or the server is down.' } ));
};
// most important part - incoming messages
connection.onmessage = function (message) {
// try to parse JSON message. Because we know that the server always returns
// JSON this should work without any problem but we should make sure that
// the massage is not chunked or otherwise damaged.
try {
var json = JSON.parse(message.data);
} catch (e) {
console.log('This doesn\'t look like a valid JSON: ', message.data);
return;
}
if (json.type === 'welcome') {
// lets ignore the welcome
} else if (json.type === 'ping') {
// lets ignore the pings
} else if (json.type === 'confirm_subscription') {
// lets ignore the confirmation
}else if (json.message.event === 'message.created') {
console.log('here comes message', json);
if(json.message.data.message_type === 1)
{
addMessage(json.message.data.sender.name, json.message.data.content);
}
} else {
console.log('Hmm..., I\'ve never seen JSON like this: ', json);
}
};
/**
* Send mesage when user presses Enter key
*/
input.keydown(function(e) {
if (e.keyCode === 13) {
var msg = $(this).val();
if (!msg) {
return;
}
// send the message to chatwoot
//connection.send(msg);
sendMessage(msg);
addMessage("me", msg)
$(this).val('');
}
});
/**
* This method is optional. If the server wasn't able to respond to the
* in 3 seconds then show some error message to notify the user that
* something is wrong.
*/
setInterval(function() {
if (connection.readyState !== 1) {
status.text('Error');
input.attr('disabled', 'disabled').val('Unable to communicate '
+ 'with the WebSocket server.');
}
}, 3000);
/**
* Add message to the chat window
*/
function addMessage(author, message) {
content.append('<p><span>' + author + '</span> @ ' + ':' + message + '</p>');
content.scrollTop(1000000);
}
function setUpContact() {
if(getCookie('contactIdentifier')){
chatwoot.contactIdentifier = getCookie('contactIdentifier');
chatwoot.contactPubsubToken = getCookie('contactPubsubToken')
}else{
xhttp.open("POST", chatwoot.chatwootAPIUrl + "inboxes/"+chatwoot.inboxIdentifier+"/contacts");
xhttp.send();
var contactPubsubToken = JSON.parse(xhttp.responseText).pubsub_token;
var contactIdentifier = JSON.parse(xhttp.responseText).source_id;
setCookie('contactIdentifier',contactIdentifier,30);
setCookie('contactPubsubToken',contactPubsubToken,30);
}
}
function setUpConversation() {
if(getCookie('contactConverstion')){
chatwoot.contactConverstion = getCookie('contactConverstion');
}else{
xhttp.open("POST", chatwoot.chatwootAPIUrl + "inboxes/"+chatwoot.inboxIdentifier+"/contacts/"+chatwoot.contactIdentifier+"/conversations", false);
xhttp.send();
var contactConverstion = JSON.parse(xhttp.responseText).id;
setCookie('contactConverstion',contactConverstion,30);
}
}
function sendMessage(msg){
xhttp.open("POST", chatwoot.chatwootAPIUrl + "inboxes/"+chatwoot.inboxIdentifier+"/contacts/"+chatwoot.contactIdentifier+"/conversations/"+chatwoot.contactConverstion+"/messages", false);
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhttp.send(JSON.stringify({content: msg}));
}
function setCookie(name,value,days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}
});