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

238
LINES

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

Author comment

Began life as a copy of #1025979

download  show line numbers   

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

No comments. add comment

Snippet ID: #1026025
Snippet name: MMO Chat Bot Template As JavaScript
Eternal ID of this version: #1026025/59
Text MD5: 337295e77ff91cb6661e806c2222678e
Author: stefan
Category: javax / web chat bots
Type: Document
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-11-24 16:42:20
Source code size: 9060 bytes / 238 lines
Pitched / IR pitched: No / No
Views / Downloads: 220 / 8435
Version history: 58 change(s)
Referenced in: [show references]