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

260
LINES

< > BotCompany Repo | #1026219 // BotCompany.de Chat Bot Template As JavaScript

Document

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

Author comment

Began life as a copy of #1026025

download  show line numbers   

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

No comments. add comment

Snippet ID: #1026219
Snippet name: BotCompany.de Chat Bot Template As JavaScript
Eternal ID of this version: #1026219/6
Text MD5: ba180db61a3baa0d2665128bf39eb919
Author: stefan
Category: javax / web chat bots
Type: Document
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-12-05 17:47:25
Source code size: 9657 bytes / 260 lines
Pitched / IR pitched: No / No
Views / Downloads: 146 / 11737
Version history: 5 change(s)
Referenced in: [show references]