1 | // runnable: function($) |
2 | function dynamicallyLoadJQuery(runnable) { |
3 | if (document.jQuery != null) |
4 | jQuery(document).ready(runnable); |
5 | else { |
6 | var script = document.createElement("SCRIPT"); |
7 | script.src = 'https://code.jquery.com/jquery-1.10.2.js'; |
8 | script.type = 'text/javascript'; |
9 | script.onload = function() { |
10 | console.log("jquery loaded"); |
11 | jQuery(document).ready(runnable); |
12 | }; |
13 | document.getElementsByTagName("head")[0].appendChild(script); |
14 | } |
15 | } |
16 | |
17 | var chatBot_autoOpen = #AUTOOPEN#; |
18 | var chatBot_n = #N#; |
19 | var chatBot_interval = 1000; |
20 | var chatBot_nInitial = chatBot_n; |
21 | var chatBot_showActions = false; |
22 | var chatBot_language = ""; |
23 | var chatBot_started = false; |
24 | var chatBot_cookie = localStorage.getItem('cookie'); |
25 | var chatBot_longPollTimeout = 75000; |
26 | |
27 | var urlParams = new URLSearchParams(window.location.search); |
28 | const botEnable = urlParams.get('bot'); |
29 | if (#BOT_ON# || botEnable == "1") { |
30 | |
31 | console.log("cookie 1: " + chatBot_cookie); |
32 | if (!chatBot_cookie) { |
33 | chatBot_cookie = Math.random().toString(36).substr(2, 9); |
34 | localStorage.setItem('cookie', chatBot_cookie); |
35 | console.log("cookie 2: " + chatBot_cookie); |
36 | } |
37 | |
38 | // workaround to get $ from wordpress version of jQuery |
39 | dynamicallyLoadJQuery(function ($) { |
40 | console.log("loading 2"); |
41 | |
42 | function dynamicallyLoadScript(url) { |
43 | var script = document.createElement("script"); |
44 | script.src = url; |
45 | document.head.appendChild(script); |
46 | } |
47 | |
48 | function loadStyleSheet(url, onLoad) { |
49 | var link = document.createElement('link'); |
50 | link.setAttribute("rel", "stylesheet"); |
51 | link.setAttribute("type", "text/css"); |
52 | link.onload = onLoad; |
53 | link.setAttribute("href", url); |
54 | document.head.appendChild(link); |
55 | } |
56 | |
57 | // bootstrap js |
58 | |
59 | dynamicallyLoadScript('https://botcompany.de/1014036/raw/1025981?contentType=text/javascript'); |
60 | |
61 | var shouldOpenBot = false, styleSheetLoaded = false; |
62 | |
63 | // bot styles |
64 | loadStyleSheet("https://botcompany.de/1014036/raw/#CSS_ID#?contentType=text/css", function() { |
65 | if (!styleSheetLoaded) { |
66 | console.log("style sheet loaded"); |
67 | styleSheetLoaded = true; |
68 | if (shouldOpenBot) chatBot_actualOpen(); |
69 | } |
70 | }); |
71 | |
72 | // scripts for bot |
73 | |
74 | function chatBot_open() { |
75 | shouldOpenBot = true; |
76 | if (styleSheetLoaded) chatBot_actualOpen(); |
77 | } |
78 | |
79 | function chatBot_actualOpen() { |
80 | console.log("Opening chat bot"); |
81 | $('#qnimate').addClass('popup-box-on'); |
82 | $('.opener-box').hide(); |
83 | $("#chatBot_scrollme").scrollTop(1E10); |
84 | $('#status_message').focus(); |
85 | //chatBotShown// |
86 | if (!chatBot_started) { |
87 | chatBot_started = true; |
88 | try { |
89 | if (window.GTranslateGetCurrentLang) |
90 | chatBot_language = window.GTranslateGetCurrentLang(); |
91 | console.log("lang: " + chatBot_language); |
92 | } catch (err) { |
93 | console.log(err); |
94 | } |
95 | |
96 | chatBot_start(); |
97 | } |
98 | } |
99 | |
100 | $(function(){ |
101 | $(".chatOpenAction").click(function () { |
102 | chatBot_open(); |
103 | return false; |
104 | }); |
105 | |
106 | $(".chatCloseAction").click(function () { |
107 | $('.opener-box').show(); |
108 | $('#qnimate').removeClass('popup-box-on'); |
109 | return false; |
110 | }); |
111 | }); |
112 | |
113 | function chatBot_showAction(action) { |
114 | if (chatBot_showActions) { |
115 | $("#actionMsg").html(action); |
116 | $("#actionMsg").show(); |
117 | } |
118 | console.log(action); |
119 | } |
120 | |
121 | window.chatBot_appendAction = function(action) { |
122 | if (chatBot_showActions) { |
123 | $("#actionMsg").append(" | " + action); |
124 | $("#actionMsg").show(); |
125 | } |
126 | }; |
127 | |
128 | window.chatBot_hideAction = function() { |
129 | $("#actionMsg").hide(); |
130 | $("#actionMsg").html(""); |
131 | }; |
132 | |
133 | window.chatBot_start = function() { |
134 | url = "#INCREMENTALURL#"; |
135 | if (url != '' && url != ("#INC" + "REMENTALURL#")) { |
136 | url += chatBot_n + "&cookie=" + chatBot_cookie + "&rand=" + Math.random(); |
137 | if (chatBot_language) url += "&language_default=" + chatBot_language; |
138 | chatBot_showAction("Loading " + url); |
139 | |
140 | $.ajax({url: url, success: function(src) { |
141 | chatBot_showAction("Loaded " + src.length + " chars"); |
142 | #ONMSGHTML# |
143 | var match = src.match(/\d+/); |
144 | if (match != null) { |
145 | var newN = parseInt(match[0]); |
146 | if (src.match(/NEW DIALOG -->/)) |
147 | $(".direct-chat-messages").html(src); |
148 | else { |
149 | // hide old buttons |
150 | $(".direct-chat-msg:has(.direct-chat-buttons)").hide(); |
151 | $(".chatbot-choice-button, .chat-button-span").hide(); |
152 | $(".direct-chat-messages").append(src); |
153 | } |
154 | chatBot_n = newN; |
155 | $("#chatBot_scrollme").scrollTop(1E10); |
156 | chatBot_showAction("Appended " + src.length); |
157 | console.log(src); |
158 | } else |
159 | chatBot_showAction("chatBot_n=" + chatBot_n + " (initial=" + chatBot_nInitial + ")"); |
160 | chatBot_appendAction("Rescheduling"); |
161 | //var interval = src == '' ? chatBot_interval*10 : chatBot_interval; // slowdown when bug |
162 | var interval = chatBot_interval; |
163 | setTimeout(chatBot_start, interval); |
164 | chatBot_appendAction("Rescheduled"); |
165 | }, dataType: 'text', timeout: chatBot_longPollTimeout}) |
166 | .fail(function() { |
167 | chatBot_showAction("Rescheduling after fail"); |
168 | setTimeout(chatBot_start, chatBot_interval); |
169 | }); |
170 | } |
171 | }; |
172 | |
173 | // set input text and placeholder |
174 | window.chatBot_setInput = function(text, placeholder) { |
175 | if (placeholder == '') placeholder = "Type a message..."; |
176 | $('#status_message').attr('placeholder', placeholder).val(text); |
177 | }; |
178 | |
179 | window.submitAMsg = function(msg) { |
180 | if (msg == "") return; |
181 | status_message.value = msg; |
182 | submitMsg(); |
183 | }; |
184 | |
185 | window.submitMsg = function() { |
186 | /*if (status_message.value == 'new dialog') |
187 | document.forms['msgform'].submit(); |
188 | else*/ { |
189 | url = '#MSGURL#' + encodeURIComponent(status_message.value) + "&cookie=" + chatBot_cookie + "&rand=" + Math.random(); |
190 | if (chatBot_language) url += "&language_default=" + chatBot_language; |
191 | chatBot_showAction('Submitting ' + url); |
192 | $.get(url); |
193 | status_message.value = ''; |
194 | } |
195 | }; |
196 | |
197 | console.log("defined functions"); |
198 | |
199 | $('body').append(` |
200 | |
201 | <div id="actionMsg"></div> <!-- debug div --> |
202 | |
203 | <!-- chat bot opener div --> |
204 | |
205 | <div class="opener-box notranslate"> |
206 | <div class="opener-head"> |
207 | <div class="popup-head-left pull-left chatOpenAction"><!--<img src="#BOTIMG#" alt="Bot Image">--> $HEADING</div> |
208 | <div class="popup-head-right pull-right"> |
209 | <button data-widget="remove" class="chat-header-button pull-right chatOpenAction" type="button"><i class="glyphicon glyphicon-off"></i></button> |
210 | </div> |
211 | </div> |
212 | </div> |
213 | |
214 | <!-- chat bot main form/div --> |
215 | |
216 | <form name="msgform"> |
217 | |
218 | <div class="popup-box chat-popup notranslate" id="qnimate"> |
219 | <div class="popup-head"> |
220 | <div class="popup-head-left pull-left" style="width: 330px"><!--<img src="#BOTIMG#" alt="Bot Image">--> $HEADING</div> |
221 | <div class="popup-head-right pull-right"> |
222 | <!-- |
223 | <div class="btn-group"> |
224 | <button class="chat-header-button" data-toggle="dropdown" type="button" aria-expanded="false"> |
225 | <i class="glyphicon glyphicon-cog"></i> </button> |
226 | <ul role="menu" class="dropdown-menu pull-right"> |
227 | <li><a href="#">Media</a></li> |
228 | <li><a href="#">Block</a></li> |
229 | <li><a href="#">Clear Chat</a></li> |
230 | <li><a href="#">Email Chat</a></li> |
231 | </ul> |
232 | </div> |
233 | --> |
234 | |
235 | <button data-widget="remove" class="chatCloseAction chat-header-button pull-right" type="button"><i class="glyphicon glyphicon-off"></i></button> |
236 | </div> |
237 | </div> |
238 | <div id="chatBot_scrollme" class="popup-messages"> |
239 | |
240 | <div class="direct-chat-messages"> |
241 | <!-- MSGS HERE --> |
242 | </div> |
243 | <div class="popup-messages-footer"> |
244 | |
245 | <div class="popup-messages-send" onClick="submitMsg(); return false;" title="Send message"> |
246 | <svg width="57px" height="54px" viewBox="1496 193 57 54" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="width: 18px; height: 18px;"> |
247 | <g id="Group-9-Copy-3" stroke="none" stroke-width="1" fill="none" fill-rule="evenodd" transform="translate(1523.000000, 220.000000) rotate(-270.000000) translate(-1523.000000, -220.000000) translate(1499.000000, 193.000000)"> |
248 | <path d="M5.42994667,44.5306122 L16.5955554,44.5306122 L21.049938,20.423658 C21.6518463,17.1661523 26.3121212,17.1441362 26.9447801,20.3958097 L31.6405465,44.5306122 L42.5313185,44.5306122 L23.9806326,7.0871633 L5.42994667,44.5306122 Z M22.0420732,48.0757124 C21.779222,49.4982538 20.5386331,50.5306122 19.0920112,50.5306122 L1.59009899,50.5306122 C-1.20169244,50.5306122 -2.87079654,47.7697069 -1.64625638,45.2980459 L20.8461928,-0.101616237 C22.1967178,-2.8275701 25.7710778,-2.81438868 27.1150723,-0.101616237 L49.6075215,45.2980459 C50.8414042,47.7885641 49.1422456,50.5306122 46.3613062,50.5306122 L29.1679835,50.5306122 C27.7320366,50.5306122 26.4974445,49.5130766 26.2232033,48.1035608 L24.0760553,37.0678766 L22.0420732,48.0757124 Z" id="sendicon" fill="#96AAB4" fill-rule="nonzero"></path> |
249 | </g> |
250 | </svg> |
251 | </div> |
252 | |
253 | <textarea id="status_message" placeholder="Type a message..." rows="10" cols="30" name="message" onkeydown="if (event.keyCode == 13) { submitMsg(); return false; }"></textarea> |
254 | |
255 | <div class="btn-footer"> |
256 | <!-- |
257 | <button class="bg_none"><i class="glyphicon glyphicon-film"></i> </button> |
258 | <button class="bg_none"><i class="glyphicon glyphicon-camera"></i> </button> |
259 | <button class="bg_none"><i class="glyphicon glyphicon-paperclip"></i> </button> |
260 | --> |
261 | <!-- |
262 | <button class="bg_none pull-right"><i class="glyphicon glyphicon-thumbs-up"></i> </button> |
263 | --> |
264 | </div> |
265 | </div> |
266 | </div> |
267 | |
268 | </form> |
269 | |
270 | <!-- end of chat bot form/div --> |
271 | |
272 | `); |
273 | |
274 | console.log("done init"); |
275 | // onLoad |
276 | if (chatBot_autoOpen) { |
277 | console.log("auto-opening chat bot"); |
278 | //window.addEventListener('DOMContentLoaded', chatBot_open, false); |
279 | chatBot_open(); |
280 | } |
281 | |
282 | //MORESTUFF// |
283 | |
284 | // end of bot scripts |
285 | }); |
286 | |
287 | } // if botEnable |
Began life as a copy of #1026225
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1027638 |
Snippet name: | Cruddie Chat Bot Template As JavaScript |
Eternal ID of this version: | #1027638/14 |
Text MD5: | 477e042c7eedf039e0f446bfae157c21 |
Author: | stefan |
Category: | javax / web chat bots |
Type: | Document |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-04-05 12:08:47 |
Source code size: | 10668 bytes / 287 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 324 / 459 |
Version history: | 13 change(s) |
Referenced in: | [show references] |