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

285
LINES

< > BotCompany Repo | #1027690 // Cruddie Chat Bot Template As JavaScript [input field instead of textarea, LIVE]

Document

// runnable: function($)
function dynamicallyLoadJQuery(runnable) {
  if (document.jQuery != null)
    jQuery(document).ready(runnable);
  else {
    var script = document.createElement("SCRIPT");
    script.src = 'https://code.jquery.com/jquery-1.10.2.js';
    script.type = 'text/javascript';
    script.onload = function() {
      console.log("jquery loaded");
      jQuery(document).ready(runnable);
    };
    document.getElementsByTagName("head")[0].appendChild(script);
  }
}

var chatBot_autoOpen = #AUTOOPEN#;
var chatBot_n = #N#;
var chatBot_interval = 1000;
var chatBot_nInitial = chatBot_n;
var chatBot_showActions = false;
var chatBot_language = "";
var chatBot_started = false;
var chatBot_cookie = localStorage.getItem('cookie');

var urlParams = new URLSearchParams(window.location.search);
const botEnable = urlParams.get('bot');
if (#BOT_ON# || botEnable == "1") {

console.log("cookie 1: " + chatBot_cookie);
if (!chatBot_cookie) {
  chatBot_cookie = Math.random().toString(36).substr(2, 9);
  localStorage.setItem('cookie', chatBot_cookie);
  console.log("cookie 2: " + chatBot_cookie);
}

// workaround to get $ from wordpress version of jQuery
dynamicallyLoadJQuery(function ($) {
  console.log("loading 2");
  
  function dynamicallyLoadScript(url) {
    var script = document.createElement("script");
    script.src = url;
    document.head.appendChild(script);
  }
  
  function loadStyleSheet(url, onLoad) {
    var link = document.createElement('link');
    link.setAttribute("rel", "stylesheet");
    link.setAttribute("type", "text/css");
    link.onload = onLoad;
    link.setAttribute("href", url);
    document.head.appendChild(link);
  }
  
  // bootstrap js
  
  dynamicallyLoadScript('https://botcompany.de/1014036/raw/1025981?contentType=text/javascript');
  
  var shouldOpenBot = false, styleSheetLoaded = false;
  
  // bot styles
  loadStyleSheet("https://botcompany.de/1014036/raw/#CSS_ID#?contentType=text/css", function() {
    if (!styleSheetLoaded) {
      console.log("style sheet loaded");
      styleSheetLoaded = true;
      if (shouldOpenBot) chatBot_actualOpen();
    }
  });

  // scripts for bot
  
  function chatBot_open() {
    shouldOpenBot = true;
    if (styleSheetLoaded) chatBot_actualOpen();
  }
  
  function chatBot_actualOpen() {
    console.log("Opening chat bot");
    $('#qnimate').addClass('popup-box-on');
    $('.opener-box').hide();
    $("#chatBot_scrollme").scrollTop(1E10);
    $('#status_message').focus();
    //chatBotShown//
    if (!chatBot_started) {
      chatBot_started = true;
      try {
        if (window.GTranslateGetCurrentLang)
          chatBot_language = window.GTranslateGetCurrentLang();
        console.log("lang: " + chatBot_language);
      } catch (err) {
        console.log(err);
      }

      chatBot_start();
    }
  }
  
  $(function(){
    $(".chatOpenAction").click(function () {
      chatBot_open();
      return false;
    });
      
    $(".chatCloseAction").click(function () {
      $('.opener-box').show();
      $('#qnimate').removeClass('popup-box-on');
      return false;
    });
  });
  
  function chatBot_showAction(action) {
    if (chatBot_showActions) {
      $("#actionMsg").html(action);
      $("#actionMsg").show();
    }
    console.log(action);
  }
    
  window.chatBot_appendAction = function(action) {
    if (chatBot_showActions) {
      $("#actionMsg").append(" | " + action);
      $("#actionMsg").show();
    }
  };
  
  window.chatBot_hideAction = function() {
    $("#actionMsg").hide();
    $("#actionMsg").html("");
  };
  
  window.chatBot_start = function() {
    url = "#INCREMENTALURL#";
    if (url != '' && url != ("#INC" + "REMENTALURL#")) {
      url += chatBot_n + "&cookie=" + chatBot_cookie + "&rand=" + Math.random();
      if (chatBot_language) url += "&language_default=" + chatBot_language;
      chatBot_showAction("Loading " + url);
      $.get(url, function(src) {
        chatBot_showAction("Loaded " + src.length + " chars");
        #ONMSGHTML#
        var match = src.match(/\d+/);
        if (match != null) {
          var newN = parseInt(match[0]);
          if (src.match(/NEW DIALOG -->/))
            $(".direct-chat-messages").html(src);
          else {
            // hide old buttons
            $(".direct-chat-msg:has(.direct-chat-buttons)").hide();
            $(".chatbot-choice-button, .chat-button-span").hide();
            $(".direct-chat-messages").append(src);
          }
          chatBot_n = newN;
          $("#chatBot_scrollme").scrollTop(1E10);
          chatBot_showAction("Appended " + src.length);
          console.log(src);
        } else
          chatBot_showAction("chatBot_n=" + chatBot_n + " (initial=" + chatBot_nInitial + ")");
        chatBot_appendAction("Rescheduling");
        //var interval = src == '' ? chatBot_interval*10 : chatBot_interval; // slowdown when bug
        var interval = chatBot_interval;
        setTimeout(chatBot_start, interval);
        chatBot_appendAction("Rescheduled");
      }, 'text')
        .fail(function() {
          chatBot_showAction("Rescheduling after fail");
          setTimeout(chatBot_start, chatBot_interval);
        });
    }
  };

  // set input text and placeholder
  window.chatBot_setInput = function(text, placeholder) {
    if (placeholder == '') placeholder = "Type a message...";
    $('#status_message').attr('placeholder', placeholder).val(text);
  };
  
  window.submitAMsg = function(msg) {
    if (msg == "") return;
    status_message.value = msg;
    submitMsg();
  };
  
  window.submitMsg = function() {
    /*if (status_message.value == 'new dialog')
      document.forms['msgform'].submit();
    else*/ {
      url = '#MSGURL#' + encodeURIComponent(status_message.value) + "&cookie=" + chatBot_cookie + "&rand=" + Math.random();
      if (chatBot_language) url += "&language_default=" + chatBot_language;
      chatBot_showAction('Submitting ' + url);
      $.get(url);
      status_message.value = '';
    }
  };
  
  console.log("defined functions");
  
  $('body').append(`
  
  <div id="actionMsg"></div> <!-- debug div -->

  <!-- chat bot opener div -->

<div class="opener-box notranslate">
  <div class="opener-head">
    <div class="popup-head-left pull-left chatOpenAction"><!--<img src="#BOTIMG#" alt="Bot Image">--> $HEADING</div>
    <div class="popup-head-right pull-right">
    <button data-widget="remove" class="chat-header-button pull-right chatOpenAction" type="button"><i class="glyphicon glyphicon-off"></i></button>
    </div>
  </div>
</div>

<!-- chat bot main form/div -->

<form name="msgform">

<div class="popup-box chat-popup notranslate" id="qnimate">
          <div class="popup-head">
        <div class="popup-head-left pull-left" style="width: 300px"><!--<img src="#BOTIMG#" alt="Bot Image">--> $HEADING</div>
            <div class="popup-head-right pull-right">
            <!--
            <div class="btn-group">
                      <button class="chat-header-button" data-toggle="dropdown" type="button" aria-expanded="false">
                     <i class="glyphicon glyphicon-cog"></i> </button>
                    <ul role="menu" class="dropdown-menu pull-right">
                    <li><a href="#">Media</a></li>
                    <li><a href="#">Block</a></li>
                    <li><a href="#">Clear Chat</a></li>
                    <li><a href="#">Email Chat</a></li>
                    </ul>
            </div>
            -->
            
            <button data-widget="remove" class="chatCloseAction chat-header-button pull-right" type="button"><i class="glyphicon glyphicon-off"></i></button>
                      </div>
        </div>
      <div id="chatBot_scrollme" class="popup-messages">
        
      <div class="direct-chat-messages">
      <!-- MSGS HERE -->
      </div>
      <div class="popup-messages-footer">
      
      <div class="popup-messages-send" onClick="submitMsg(); return false;" title="Send message">
                <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;">
                  <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)">
                    <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>
                  </g>
                </svg>
              </div>

      <input type="text" id="status_message" placeholder="Type a message..." name="message" onkeypress="if (event.keyCode == 13) { submitMsg(); return false; }" style="width: 350px; margin-left: 10px; padding: 3px;">
      
      <div class="btn-footer">
      <!--
      <button class="bg_none"><i class="glyphicon glyphicon-film"></i> </button>
      <button class="bg_none"><i class="glyphicon glyphicon-camera"></i> </button>
            <button class="bg_none"><i class="glyphicon glyphicon-paperclip"></i> </button>
      -->
      <!--
      <button class="bg_none pull-right"><i class="glyphicon glyphicon-thumbs-up"></i> </button>
      -->
      </div>
      </div>
    </div>
    
</form>

<!-- end of chat bot form/div -->

  `);
  
  console.log("done init");
  // onLoad
  if (chatBot_autoOpen) {
    console.log("auto-opening chat bot");
    //window.addEventListener('DOMContentLoaded', chatBot_open, false);
    chatBot_open();
  }
  
  //MORESTUFF//
  
  // end of bot scripts
});

} // if botEnable

Author comment

Began life as a copy of #1027638

download  show line numbers   

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

No comments. add comment

Snippet ID: #1027690
Snippet name: Cruddie Chat Bot Template As JavaScript [input field instead of textarea, LIVE]
Eternal ID of this version: #1027690/10
Text MD5: c9f79c4ed8265b2612e1a36fc8292456
Author: stefan
Category: javax / web chat bots
Type: Document
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-03-31 00:56:18
Source code size: 10595 bytes / 285 lines
Pitched / IR pitched: No / No
Views / Downloads: 190 / 815416
Version history: 9 change(s)
Referenced in: #1027610 - Cruddie standalone [old]
#1027691 - Cruddie [adding awesomplete, old]
#1027705 - Cruddie [backup before "all public" notice]
#1027728 - Cruddie Chat Bot Template As JavaScript [backup before switching to $.ajax]
#1027734 - Cruddie [backup before changing cookie handling]
#1028679 - Cruddie Spike [with VAT magic for Chrome]
#1028961 - cruddie.botcompany.de [LIVE]