1 | console.log("starting chat bot template"); |
2 | |
3 | // don't load again if already loaded |
4 | /*if (typeof dynamicallyLoadJQuery !== "undefined") { |
5 | console.log("chat bot already loaded: " + typeof dynamicallyLoadJQuery); |
6 | } else*/ { |
7 | |
8 | // runnable: function($) |
9 | function dynamicallyLoadJQuery(runnable) { |
10 | console.log("document.jQuery: " + document.jQuery); |
11 | console.log("$: " + (typeof $ === 'undefined' ? "none" : $)); |
12 | if (document.jQuery != null) |
13 | jQuery(document).ready(runnable); |
14 | else if (typeof $ !== 'undefined') |
15 | $(document).ready(runnable); |
16 | else { |
17 | console.log("need to load jquery"); |
18 | var script = document.createElement("SCRIPT"); |
19 | script.src = 'https://code.jquery.com/jquery-1.10.2.js'; |
20 | script.type = 'text/javascript'; |
21 | script.onload = function() { |
22 | console.log("jquery loaded"); |
23 | jQuery(document).ready(runnable); |
24 | }; |
25 | document.getElementsByTagName("head")[0].appendChild(script); |
26 | } |
27 | } |
28 | |
29 | var chatBot_autoOpen = #AUTOOPEN#; |
30 | var chatBot_n = #N#; |
31 | var chatBot_interval = 1000; |
32 | var chatBot_nInitial = chatBot_n; |
33 | var chatBot_showActions = false; |
34 | var chatBot_language = ""; |
35 | var chatBot_started = false; |
36 | var chatBot_cookie = localStorage.getItem('chatbot-cookie'); |
37 | var chatBot_notificationsOn = true; |
38 | var chatBot_justStarted = true; |
39 | |
40 | // polyfill for URLSearchParams |
41 | |
42 | (function (w) { |
43 | w.URLSearchParams = w.URLSearchParams || function (searchString) { |
44 | var self = this; |
45 | self.searchString = searchString; |
46 | self.get = function (name) { |
47 | var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(self.searchString); |
48 | if (results == null) { |
49 | return null; |
50 | } |
51 | else { |
52 | return decodeURI(results[1]) || 0; |
53 | } |
54 | }; |
55 | } |
56 | })(window); |
57 | |
58 | const urlParams = new URLSearchParams(window.location.search); |
59 | const botEnable = urlParams.get('bot'); |
60 | var botConfig = botConfig || urlParams.get('_botConfig'); |
61 | |
62 | // allow cookie override in URL |
63 | var cookie2 = urlParams.get('cookie'); |
64 | if (urlParams.get("_newBotCookie") == "1") { |
65 | chatBot_cookie = "conv-" + Math.random().toString(36).substr(2, 9); |
66 | console.log("New cookie made >> " + chatBot_cookie); |
67 | } |
68 | |
69 | if (cookie2) { |
70 | chatBot_cookie = cookie2; |
71 | console.log("Cookie override >> " + cookie2); |
72 | } |
73 | |
74 | if (#BOT_ON# || botEnable == "1") { |
75 | |
76 | console.log("cookie 1: " + chatBot_cookie); |
77 | if (!chatBot_cookie) { |
78 | chatBot_cookie = Math.random().toString(36).substr(2, 9); |
79 | localStorage.setItem('chatbot-cookie', chatBot_cookie); |
80 | console.log("cookie 2: " + chatBot_cookie); |
81 | } |
82 | |
83 | // workaround to get $ from wordpress version of jQuery |
84 | dynamicallyLoadJQuery(function ($) { |
85 | // from here on we have JQuery |
86 | console.log("loading 2"); |
87 | |
88 | // helper for inserting emojis |
89 | |
90 | $.fn.insertIntoTextArea = function(textToInsert) { |
91 | return this.each(function () { |
92 | var txt = $(this); |
93 | var cursorPosStart = txt.prop('selectionStart'); |
94 | var cursorPosEnd = txt.prop('selectionEnd'); |
95 | var v = txt.val(); |
96 | var textBefore = v.substring(0, cursorPosStart); |
97 | var textAfter = v.substring(cursorPosEnd, v.length); |
98 | txt.val(textBefore + textToInsert + textAfter); |
99 | txt.prop('selectionStart', cursorPosStart + textToInsert.length); |
100 | txt.prop('selectionEnd', cursorPosStart + textToInsert.length); |
101 | txt.focus(); |
102 | }); |
103 | }; |
104 | |
105 | function dynamicallyLoadScript(url, onload, asModule) { |
106 | var script = document.createElement("script"); |
107 | script.src = url; |
108 | script.onload = onload; |
109 | if (asModule) script.type = "module"; |
110 | document.head.appendChild(script); |
111 | } |
112 | |
113 | function dynamicallyLoadModule(code) { |
114 | var script = document.createElement("script"); |
115 | script.type = "module"; |
116 | script.appendChild(document.createTextNode(code)); |
117 | document.head.appendChild(script); |
118 | } |
119 | |
120 | function loadStyleSheet(url, onLoad) { |
121 | var link = document.createElement('link'); |
122 | link.setAttribute("rel", "stylesheet"); |
123 | link.setAttribute("type", "text/css"); |
124 | link.onload = onLoad; |
125 | link.setAttribute("href", url); |
126 | document.head.appendChild(link); |
127 | } |
128 | |
129 | // load fonts & style sheets |
130 | |
131 | loadStyleSheet("https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css"); |
132 | loadStyleSheet("https://fonts.googleapis.com/css2?family=Roboto:wght@100;300;400;500;700;900&display=swap"); |
133 | |
134 | // intTelInput.min.css |
135 | loadStyleSheet("https://botcompany.de/serve/1029819?ct=text/css"); |
136 | |
137 | // intlTelInput.min.js |
138 | dynamicallyLoadScript("https://botcompany.de/serve/1029820?ct=text/javascript", function() { |
139 | window.telInput = document.querySelector("#chat_telephone"); |
140 | console.log("telInput: " + telInput); |
141 | window.telHandler = window.intlTelInput(window.telInput, { |
142 | utilsScript: "https://botcompany.de/serve/1029822?ct=text/javascript", |
143 | preferredCountries: ["in", "gb"], |
144 | separateDialCode: true, |
145 | initialCountry: "#COUNTRY#" |
146 | }); |
147 | }); |
148 | |
149 | /*dynamicallyLoadScript("https://gazelle.rocks/emoji-picker/index.js", function() { |
150 | console.log("emoji picker loaded"); |
151 | |
152 | dynamicallyLoadModule(` |
153 | import { EmojiButton } from "https://gazelle.rocks/emoji-picker/index.js"; |
154 | |
155 | const picker = new EmojiButton(); |
156 | const trigger = document.querySelector('#bot-emoji-trigger'); |
157 | |
158 | picker.on('emoji', selection => { |
159 | console.log(selection.emoji); |
160 | $("#chat_message").insertIntoTextArea(selection.emoji); |
161 | setTimeout(function() { |
162 | $("#chat_message").focus(); |
163 | }, 250); |
164 | }); |
165 | |
166 | trigger.addEventListener('click', () => picker.togglePicker(trigger)); |
167 | `); |
168 | }, true);*/ |
169 | |
170 | loadStyleSheet("https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/css/all.min.css"); |
171 | loadStyleSheet("https://fonts.googleapis.com/css2?family=Roboto:wght@100;300;400;500;700;900&display=swap"); |
172 | |
173 | var shouldOpenBot = false, styleSheetLoaded = false; |
174 | |
175 | // bot styles |
176 | loadStyleSheet("https://pays5.com/text/#CSS_ID#?ct=text%2Fcss", function() { |
177 | if (!styleSheetLoaded) { |
178 | console.log("style sheet loaded"); |
179 | styleSheetLoaded = true; |
180 | $('.chatbot').show(); // show opener |
181 | if (shouldOpenBot) chatBot_actualOpen(); |
182 | if (typeof chatBotLoadedCallback !== 'undefined') |
183 | chatBotLoadedCallback(); |
184 | } |
185 | }); |
186 | |
187 | // scripts for bot |
188 | |
189 | window.chatBot_isOpen = function() { |
190 | const chatCloseIcon = document.querySelector(".chat-btn .close-icon"); |
191 | return !chatCloseIcon.classList.contains("hide"); |
192 | } |
193 | |
194 | window.chatBot_toggle = function() { |
195 | const chatCommentIcon = document.querySelector(".chat-btn .comment-icon"); |
196 | const chatCloseIcon = document.querySelector(".chat-btn .close-icon"); |
197 | |
198 | var openerText; |
199 | if (!chatBot_isOpen()) { |
200 | chatCommentIcon.classList.add("hide"); |
201 | chatCloseIcon.classList.remove("hide"); |
202 | chatBot_open(); |
203 | openerText = "Send Message"; |
204 | } else { |
205 | chatCloseIcon.classList.add("hide"); |
206 | chatCommentIcon .classList.remove("hide"); |
207 | $('.chat').removeClass('is-visible'); |
208 | openerText = "Open Chat Bot"; |
209 | } |
210 | $('.chat-btn').attr("title", openerText); |
211 | } |
212 | |
213 | window.chatBot_open = function() { |
214 | shouldOpenBot = true; |
215 | if (styleSheetLoaded) chatBot_actualOpen(); |
216 | } |
217 | |
218 | function chatBot_actualOpen() { |
219 | console.log("Opening chat bot"); |
220 | $('.chat').addClass('is-visible'); |
221 | $('#chat_message').focus(); |
222 | if (!chatBot_started) { |
223 | chatBot_started = true; |
224 | try { |
225 | if (window.GTranslateGetCurrentLang) |
226 | chatBot_language = window.GTranslateGetCurrentLang(); |
227 | console.log("lang: " + chatBot_language); |
228 | } catch (err) { |
229 | console.log(err); |
230 | } |
231 | |
232 | chatBot_start(); |
233 | } |
234 | } |
235 | |
236 | function chatBot_showAction(action) { |
237 | console.log(action); |
238 | } |
239 | |
240 | window.chatBot_appendAction = function(action) { |
241 | }; |
242 | |
243 | window.chatBot_hideAction = function() { |
244 | }; |
245 | |
246 | function standardParams() { |
247 | return "&cookie=" + chatBot_cookie + "&rand=" + Math.random() + (!botConfig ? "" : "&_botConfig=" + encodeURIComponent(botConfig)); |
248 | } |
249 | |
250 | window.chatBot_start = function() { |
251 | var url = "#INCREMENTALURL#"; |
252 | if (url != '' && url != ("#INC" + "REMENTALURL#")) { |
253 | url += chatBot_n + standardParams(); |
254 | if (chatBot_language) url += "&language_default=" + chatBot_language; |
255 | if (chatBot_justStarted) { |
256 | url += "&message=!new+dialog"; |
257 | chatBot_justStarted = false; |
258 | } |
259 | chatBot_showAction("Loading " + url); |
260 | $.get(url, function(src) { |
261 | chatBot_showAction("Loaded " + src.length + " chars"); |
262 | var match = src.match(/\d+/); |
263 | if (match != null) { |
264 | var newN = parseInt(match[0]); |
265 | if (src.match(/NEW DIALOG -->/)) { |
266 | //var noNotify = src.match(/NO NOTIFY NEW DIALOG -->/); |
267 | /*console.log("noNotify=" + noNotify); |
268 | if (noNotify) |
269 | $("#chat_converse").addClass('notransition');*/ |
270 | $("#chat_converse").html(src); |
271 | /*if (noNotify) { |
272 | $("#chat_converse").offsetHeight; // Trigger a reflow |
273 | $("#chat_converse").removeClass('notransition'); // Re-enable transition |
274 | }*/ |
275 | } else { |
276 | // hide old buttons and special input fields |
277 | $(".chat_buttons, .chat-button-span, .chatbot-choice-button").hide(); |
278 | $("#chat_telephone, .iti").hide(); |
279 | $("#chat_message").show(); |
280 | // now append new stuff |
281 | $("#chat_converse").append(src); |
282 | } |
283 | var oldN = chatBot_n; |
284 | chatBot_n = newN; |
285 | $("#chat_converse").scrollTop(1000000); |
286 | chatBot_showAction("Appended " + src.length); |
287 | console.log(src); |
288 | } else |
289 | chatBot_showAction("chatBot_n=" + chatBot_n + " (initial=" + chatBot_nInitial + ")"); |
290 | chatBot_appendAction("Rescheduling"); |
291 | //var interval = src == '' ? chatBot_interval*10 : chatBot_interval; // slowdown when bug |
292 | var interval = chatBot_interval; |
293 | setTimeout(chatBot_start, interval); |
294 | chatBot_appendAction("Rescheduled"); |
295 | }, 'text') |
296 | .fail(function() { |
297 | chatBot_showAction("Rescheduling after fail"); |
298 | setTimeout(chatBot_start, chatBot_interval); |
299 | }); |
300 | } |
301 | }; |
302 | |
303 | // also focuses input field |
304 | window.chatBot_setInput = function(text, placeholder) { |
305 | if (placeholder == '') placeholder = "Type a message..."; |
306 | $('#chat_message') |
307 | .attr('placeholder', placeholder); |
308 | if (text) |
309 | $('#chat_message').val(text).select().focus(); |
310 | }; |
311 | |
312 | window.allowEmptyMsg = false; |
313 | |
314 | window.submitAMsg = function(msg) { |
315 | if (!window.allowEmptyMsg && msg == "") return; |
316 | chat_message.value = msg; |
317 | submitMsg(); |
318 | }; |
319 | |
320 | window.submitMsg = function() { |
321 | var msg = chat_message.value; |
322 | |
323 | if ($("#chat_telephone").is(":visible") && !/^!/.test(msg)) |
324 | msg = window.telHandler.getNumber(); |
325 | |
326 | var url = '#MSGURL#' + encodeURIComponent(msg) + standardParams(); |
327 | if (chatBot_language) url += "&language_default=" + chatBot_language; |
328 | chatBot_showAction('Submitting ' + url); |
329 | $.get(url); |
330 | chat_message.value = ''; |
331 | }; |
332 | |
333 | window.playChatNotification = function() { |
334 | if (window.chatNotificationWav == null) { |
335 | console.log("Loading notification wav"); |
336 | window.chatNotificationWav = new Audio("#NOTIFICATIONSOUND#"); |
337 | if ("#WORKERMODE" != "true") |
338 | window.chatNotificationWav.volume = 0.5; |
339 | } |
340 | console.log("Playing notification mp3"); |
341 | window.chatNotificationWav.play(); |
342 | }; |
343 | |
344 | var originalTitle; |
345 | |
346 | window.setTitleStatus = function(status) { |
347 | if (originalTitle == null) |
348 | originalTitle = document.title; |
349 | if (!document.hasFocus() || document.activeElement !== document.getElementById('chat_message')) { |
350 | if (status) status = status + " "; |
351 | } else status = ""; |
352 | document.title = status + originalTitle; |
353 | }; |
354 | |
355 | function resetTitle() { |
356 | window.setTitleStatus(""); |
357 | } |
358 | |
359 | window.onfocus = resetTitle; |
360 | $('#chat_message').on("focus", resetTitle); |
361 | |
362 | var sendTyping_sent = 0, sendTyping_interval = 5000; |
363 | window.sendTyping = function() { |
364 | var time = Date.now(); |
365 | if (time > sendTyping_sent+sendTyping_interval) { |
366 | sendTyping_sent = time; |
367 | var url = "#TYPINGURL#" + standardParams(); |
368 | console.log("Loading " + url); |
369 | $.get(url); |
370 | } |
371 | }; |
372 | |
373 | window.notiToggle = function() { |
374 | submitAMsg('!toggle notifications'); |
375 | var text = $("#chatBot_notiToggleText"); |
376 | text.text("Turn " + (/Turn on/.test(text.text()) ? "off" : "on") + " notifications"); |
377 | }; |
378 | |
379 | var typingCounter = 0, showTyping_interval = 3000; |
380 | window.showTyping = function() { |
381 | console.log("showTyping " + typingCounter); |
382 | typingCounter++; |
383 | if (typingCounter == 1) |
384 | $('#otherSideTyping').css('display', 'block'); |
385 | setTimeout(function() { |
386 | console.log("showTyping end " + typingCounter); |
387 | if (--typingCounter <= 0) |
388 | $('#otherSideTyping').css('display', 'none'); |
389 | }, showTyping_interval); |
390 | }; |
391 | |
392 | window.chat_keyDown = function(event) { |
393 | if (event.keyCode == 13) { submitMsg(); return false; } |
394 | else if (event.keyCode >= 32 && event.keyCode < 128) sendTyping(); |
395 | }; |
396 | |
397 | console.log("defined functions"); |
398 | |
399 | $('body').append(` |
400 | |
401 | <div aria-live="polite" aria-atomic="true" aria-relevant="all" id="screenreadertrick"></div> |
402 | <div class="chatbot" style="display: none"> |
403 | <div class="chat-box" role="region" aria-labelledby="tchat-label"> |
404 | <h1 id="tchat-label" class="sr-only" tabindex="-1">Chat window</h1> |
405 | <div class="chat"> |
406 | <div class="main-header"> |
407 | <div class="chat_header"> |
408 | <div class="four-people" style=" |
409 | "> |
410 | <div class="users user-1"></div> |
411 | <div class="users user-2"></div> |
412 | <div class="users user-3"></div> |
413 | <div class="users user-4"></div> |
414 | |
415 | </div> |
416 | <img src="#BOTIMG#" class="header-user"> |
417 | <h2>$HEADING</h2> |
418 | <div class="buttons"> |
419 | <!--<button class="fas fa-ellipsis-v"></button>--> |
420 | <label class="head-dropdown"> |
421 | <div class="head-button"> |
422 | <i class="fas fa-ellipsis-v"></i> |
423 | </div> |
424 | |
425 | <input type="checkbox" class="head-input" id="test"> |
426 | |
427 | <ul class="head-menu"> |
428 | <li onclick="notiToggle()"><i class="far fa-bell-slash"></i><span id="chatBot_notiToggleText">Turn off notifications</span></li> |
429 | <li onclick="submitAMsg('!rate conversation')"><i class="fas fa-star"></i>Rate this conversation</li> |
430 | </ul> |
431 | |
432 | </label> |
433 | <button id="" class="fas fa-chevron-down" title="Close Chat Bot" onclick="chatBot_toggle()"></button> |
434 | </div> |
435 | </div> |
436 | <div class="header-bottom" style="display: none"> |
437 | <span class="online">We're online! </span> |
438 | </div> |
439 | </div> |
440 | <div id="chat_converse" class="chat_conversion chat_converse" tabindex="0" aria-live="polite"> |
441 | </div> |
442 | <div id="otherSideTyping" aria-hidden="true" style="display: none"> |
443 | <h4 class="sr-only">#OTHERSIDE# typing</h4> |
444 | <span class="joining"></span> |
445 | <span class="dot"></span> |
446 | <span class="dot"></span> |
447 | <span class="dot"></span> |
448 | </div> |
449 | <div class="chat-bottom"> |
450 | <!--<a class="camera-icon"><i class="fas fa-camera"></i></a>--> |
451 | <!--<a class="send-icon" title="Send this message" aria-label="Send"><i class="fas fa-paper-plane"></i></a>--> |
452 | |
453 | <textarea id="chat_message" class="chat_field chat_message" title="Message to send" aria-label="message to send" placeholder="Type a message..." rows="10" cols="30" name="message" onkeydown="return chat_keyDown(event);"></textarea> |
454 | <input type="tel" id="chat_telephone" style="display: none" onkeydown="return chat_keyDown(event);"> |
455 | |
456 | <div class="footer-bottom"> |
457 | <ul> |
458 | <!-- <li> |
459 | <a href="#" onclick="submitAMsg('!new dialog'); return false;" class="play-icon" title="Restart Chat Bot"><i class="fa fa-play"></i></a> |
460 | </li>--> |
461 | <!--<li> |
462 | <a href="#" id="bot-emoji-trigger"><i class="far fa-smile-beam"></i></a> |
463 | </li> --> |
464 | |
465 | </ul> |
466 | </div> |
467 | </div> |
468 | </div> |
469 | <a href="#" id="chat-btn" class="chat-btn" title="Open Chat Bot" onclick="if (chatBot_isOpen()) submitMsg(); else chatBot_toggle(); return false;"> |
470 | <div class="comment-icon"> |
471 | <i class="fas fa-comment-dots"></i> |
472 | <i class="fas fa-pencil-alt"></i> |
473 | </div> |
474 | <div class="close-icon hide"><i class="fas fa-paper-plane"></i></div> |
475 | </a> |
476 | |
477 | </div> |
478 | </div> |
479 | `); |
480 | |
481 | // design javascripts |
482 | |
483 | /*$('.chat-icon').click(function() { |
484 | chatBot_open(); |
485 | });*/ |
486 | |
487 | // for skip link |
488 | $("a[href='#chatbot']").click(chatBot_open); |
489 | |
490 | // design javascripts end |
491 | |
492 | //MOREINIT |
493 | |
494 | console.log("done init"); |
495 | // onLoad |
496 | if (chatBot_autoOpen) { |
497 | console.log("auto-opening chat bot"); |
498 | //window.addEventListener('DOMContentLoaded', chatBot_open, false); |
499 | chatBot_toggle(); |
500 | } |
501 | |
502 | // end of bot scripts |
503 | }); |
504 | |
505 | } // if botEnable |
506 | } // if not already loaded |
Began life as a copy of #1030086
Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt
No comments. add comment
Snippet ID: | #1030430 |
Snippet name: | Pays5 Chat Bot Template As JavaScript [works except for emoji picker in Firefox] |
Eternal ID of this version: | #1030430/16 |
Text MD5: | b9d138fa113a5359d4c553ae9e74a3a2 |
Author: | stefan |
Category: | javax / web chat bots |
Type: | Document |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-01-05 03:16:22 |
Source code size: | 18071 bytes / 506 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 279 / 2027 |
Version history: | 15 change(s) |
Referenced in: | [show references] |