Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

284
LINES

< > BotCompany Repo | #1026225 // IPTV Chat Bot Template As JavaScript

Document

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('chatbot-cookie');
25  
26  
const urlParams = new URLSearchParams(window.location.search);
27  
const botEnable = urlParams.get('bot');
28  
if (#BOT_ON# || botEnable == "1") {
29  
30  
console.log("cookie 1: " + chatBot_cookie);
31  
if (!chatBot_cookie) {
32  
  chatBot_cookie = Math.random().toString(36).substr(2, 9);
33  
  localStorage.setItem('chatbot-cookie', chatBot_cookie);
34  
  console.log("cookie 2: " + chatBot_cookie);
35  
}
36  
37  
// workaround to get $ from wordpress version of jQuery
38  
dynamicallyLoadJQuery(function ($) {
39  
  console.log("loading 2");
40  
  
41  
  function dynamicallyLoadScript(url) {
42  
    var script = document.createElement("script");
43  
    script.src = url;
44  
    document.head.appendChild(script);
45  
  }
46  
  
47  
  function loadStyleSheet(url, onLoad) {
48  
    var link = document.createElement('link');
49  
    link.setAttribute("rel", "stylesheet");
50  
    link.setAttribute("type", "text/css");
51  
    link.onload = onLoad;
52  
    link.setAttribute("href", url);
53  
    document.head.appendChild(link);
54  
  }
55  
  
56  
  // bootstrap js
57  
  
58  
  dynamicallyLoadScript('https://botcompany.de/1014036/raw/1025981?contentType=text/javascript');
59  
  
60  
  var shouldOpenBot = false, styleSheetLoaded = false;
61  
  
62  
  // bot styles
63  
  loadStyleSheet("https://botcompany.de/1014036/raw/#CSS_ID#?contentType=text/css", function() {
64  
    if (!styleSheetLoaded) {
65  
      console.log("style sheet loaded");
66  
      styleSheetLoaded = true;
67  
      if (shouldOpenBot) chatBot_actualOpen();
68  
    }
69  
  });
70  
71  
  // scripts for bot
72  
  
73  
  function chatBot_open() {
74  
    shouldOpenBot = true;
75  
    if (styleSheetLoaded) chatBot_actualOpen();
76  
  }
77  
  
78  
  function chatBot_actualOpen() {
79  
    console.log("Opening chat bot");
80  
    $('#qnimate').addClass('popup-box-on');
81  
    $('.opener-box').hide();
82  
    $("#chatBot_scrollme").scrollTop(1E10);
83  
    $('#status_message').focus();
84  
    if (!chatBot_started) {
85  
      chatBot_started = true;
86  
      try {
87  
        if (window.GTranslateGetCurrentLang)
88  
          chatBot_language = window.GTranslateGetCurrentLang();
89  
        console.log("lang: " + chatBot_language);
90  
      } catch (err) {
91  
        console.log(err);
92  
      }
93  
94  
      chatBot_start();
95  
    }
96  
  }
97  
  
98  
  $(function(){
99  
    $(".chatOpenAction").click(function () {
100  
      chatBot_open();
101  
      return false;
102  
    });
103  
      
104  
    $(".chatCloseAction").click(function () {
105  
      $('.opener-box').show();
106  
      $('#qnimate').removeClass('popup-box-on');
107  
      return false;
108  
    });
109  
  });
110  
  
111  
  function chatBot_showAction(action) {
112  
    if (chatBot_showActions) {
113  
      $("#actionMsg").html(action);
114  
      $("#actionMsg").show();
115  
    }
116  
    console.log(action);
117  
  }
118  
    
119  
  window.chatBot_appendAction = function(action) {
120  
    if (chatBot_showActions) {
121  
      $("#actionMsg").append(" | " + action);
122  
      $("#actionMsg").show();
123  
    }
124  
  };
125  
  
126  
  window.chatBot_hideAction = function() {
127  
    $("#actionMsg").hide();
128  
    $("#actionMsg").html("");
129  
  };
130  
  
131  
  window.chatBot_start = function() {
132  
    url = "#INCREMENTALURL#";
133  
    if (url != '' && url != ("#INC" + "REMENTALURL#")) {
134  
      url += chatBot_n + "&cookie=" + chatBot_cookie + "&rand=" + Math.random();
135  
      if (chatBot_language) url += "&language_default=" + chatBot_language;
136  
      chatBot_showAction("Loading " + url);
137  
      $.get(url, function(src) {
138  
        chatBot_showAction("Loaded " + src.length + " chars");
139  
        var match = src.match(/\d+/);
140  
        if (match != null) {
141  
          var newN = parseInt(match[0]);
142  
          if (src.match(/NEW DIALOG -->/))
143  
            $(".direct-chat-messages").html(src);
144  
          else {
145  
            // hide old buttons
146  
            $(".direct-chat-msg:has(.direct-chat-buttons)").hide();
147  
            $(".chatbot-choice-button, .chat-button-span").hide();
148  
            $(".direct-chat-messages").append(src);
149  
          }
150  
          chatBot_n = newN;
151  
          $("#chatBot_scrollme").scrollTop(1E10);
152  
          chatBot_showAction("Appended " + src.length);
153  
          console.log(src);
154  
        } else
155  
          chatBot_showAction("chatBot_n=" + chatBot_n + " (initial=" + chatBot_nInitial + ")");
156  
        chatBot_appendAction("Rescheduling");
157  
        //var interval = src == '' ? chatBot_interval*10 : chatBot_interval; // slowdown when bug
158  
        var interval = chatBot_interval;
159  
        setTimeout(chatBot_start, interval);
160  
        chatBot_appendAction("Rescheduled");
161  
      }, 'text')
162  
        .fail(function() {
163  
          chatBot_showAction("Rescheduling after fail");
164  
          setTimeout(chatBot_start, chatBot_interval);
165  
        });
166  
    }
167  
  };
168  
169  
  // also focuses input field  
170  
  window.chatBot_setInput = function(text, placeholder) {
171  
    if (placeholder == '') placeholder = "Type a message...";
172  
    $('#status_message').attr('placeholder', placeholder)
173  
      .val(text)
174  
      .select()
175  
      .focus();
176  
  };
177  
  
178  
  window.submitAMsg = function(msg) {
179  
    if (msg == "") return;
180  
    status_message.value = msg;
181  
    submitMsg();
182  
  };
183  
  
184  
  window.submitMsg = function() {
185  
    /*if (status_message.value == 'new dialog')
186  
      document.forms['msgform'].submit();
187  
    else*/ {
188  
      url = '#MSGURL#' + encodeURIComponent(status_message.value) + "&cookie=" + chatBot_cookie + "&rand=" + Math.random();
189  
      if (chatBot_language) url += "&language_default=" + chatBot_language;
190  
      chatBot_showAction('Submitting ' + url);
191  
      $.get(url);
192  
      status_message.value = '';
193  
    }
194  
  };
195  
  
196  
  console.log("defined functions");
197  
  
198  
  $('body').append(`
199  
  
200  
  <div id="actionMsg"></div> <!-- debug div -->
201  
202  
  <!-- chat bot opener div -->
203  
204  
<div class="opener-box notranslate">
205  
  <div class="opener-head">
206  
    <div class="popup-head-left pull-left chatOpenAction"><!--<img src="#BOTIMG#" alt="Bot Image">--> $HEADING</div>
207  
    <div class="popup-head-right pull-right">
208  
    <button data-widget="remove" class="chat-header-button pull-right chatOpenAction" type="button"><i class="glyphicon glyphicon-off"></i></button>
209  
    </div>
210  
  </div>
211  
</div>
212  
213  
<!-- chat bot main form/div -->
214  
215  
<form name="msgform">
216  
217  
<div class="popup-box chat-popup notranslate" id="qnimate">
218  
          <div class="popup-head">
219  
        <div class="chatCloseAction popup-head-left pull-left"><!--<img src="#BOTIMG#" alt="Bot Image">--> $HEADING</div>
220  
            <div class="popup-head-right pull-right">
221  
            <!--
222  
            <div class="btn-group">
223  
                      <button class="chat-header-button" data-toggle="dropdown" type="button" aria-expanded="false">
224  
                     <i class="glyphicon glyphicon-cog"></i> </button>
225  
                    <ul role="menu" class="dropdown-menu pull-right">
226  
                    <li><a href="#">Media</a></li>
227  
                    <li><a href="#">Block</a></li>
228  
                    <li><a href="#">Clear Chat</a></li>
229  
                    <li><a href="#">Email Chat</a></li>
230  
                    </ul>
231  
            </div>
232  
            -->
233  
            
234  
            <button data-widget="remove" class="chatCloseAction chat-header-button pull-right" type="button"><i class="glyphicon glyphicon-off"></i></button>
235  
                      </div>
236  
        </div>
237  
      <div id="chatBot_scrollme" class="popup-messages">
238  
        
239  
      <div class="direct-chat-messages">
240  
      <!-- MSGS HERE -->
241  
      </div>
242  
      <div class="popup-messages-footer">
243  
      
244  
      <div class="popup-messages-send" onClick="submitMsg(); return false;" title="Send message">
245  
                <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;">
246  
                  <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)">
247  
                    <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>
248  
                  </g>
249  
                </svg>
250  
              </div>
251  
252  
      <textarea id="status_message" placeholder="Type a message..." rows="10" cols="30" name="message" onkeydown="if (event.keyCode == 13) { submitMsg(); return false; }"></textarea>
253  
      
254  
      <div class="btn-footer">
255  
      <!--
256  
      <button class="bg_none"><i class="glyphicon glyphicon-film"></i> </button>
257  
      <button class="bg_none"><i class="glyphicon glyphicon-camera"></i> </button>
258  
            <button class="bg_none"><i class="glyphicon glyphicon-paperclip"></i> </button>
259  
      -->
260  
      <!--
261  
      <button class="bg_none pull-right"><i class="glyphicon glyphicon-thumbs-up"></i> </button>
262  
      -->
263  
      </div>
264  
      </div>
265  
    </div>
266  
    
267  
</form>
268  
269  
<!-- end of chat bot form/div -->
270  
271  
  `);
272  
  
273  
  console.log("done init");
274  
  // onLoad
275  
  if (chatBot_autoOpen) {
276  
    console.log("auto-opening chat bot");
277  
    //window.addEventListener('DOMContentLoaded', chatBot_open, false);
278  
    chatBot_open();
279  
  }
280  
  
281  
  // end of bot scripts
282  
});
283  
284  
} // if botEnable

Author comment

Began life as a copy of #1026219

download  show line numbers   

Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1026225
Snippet name: IPTV Chat Bot Template As JavaScript
Eternal ID of this version: #1026225/21
Text MD5: bd3f3f8d3ee2ec65f53c58bf9c830470
Author: stefan
Category: javax / web chat bots
Type: Document
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-03-27 21:25:13
Source code size: 10547 bytes / 284 lines
Pitched / IR pitched: No / No
Views / Downloads: 263 / 2146
Version history: 20 change(s)
Referenced in: [show references]