Advertisement
Guest User

index.html

a guest
Apr 7th, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script type="text/javascript">
  2.  
  3.     getMessageText = function () {
  4.         var $message_input;
  5.         $message_input = $('input.msg');
  6.         return $message_input.val();
  7.         };
  8.  
  9.         $('send_message').click(function (e) {
  10.         msg = getMessageText();
  11.         if(msg){
  12.         showUserMessage(msg);
  13.         sayToBot(msg);
  14.         $('.message_input').val('');
  15.         }
  16.         });
  17.     }
  18.  
  19.     function showUserMessage(msg_input)
  20.     {
  21.        var msg = msg_input + '</p><div class="clearfix"></div>';
  22.        $(msg).appendTo("textarea.chat").hide().fadeIn(1000);
  23.     }
  24.  
  25.     function showBotMessage(response)
  26.     {
  27.         setTimeout(function () {
  28.         if (response.length < 1) {
  29.             var fallbackMsg = "I am facing some issues, please try again later!!!";
  30.  
  31.             var BotResponse = fallbackMsg + '</p><div class="clearfix"></div>';
  32.  
  33.             $(BotResponse).appendTo("textarea.chat").hide().fadeIn(1000);
  34.         }
  35.         else{
  36.             for (i = 0; i < response.length; i++) {
  37.  
  38.                 //check if the response contains "text"
  39.                 if (response[i].hasOwnProperty("text")) {
  40.                     BotResponse =  response[i].text + '</p><div class="clearfix"></div>';
  41.                     $(BotResponse).appendTo("textarea.chat").hide().fadeIn(1000);
  42.                 }
  43.         }
  44.         }
  45.     }, 500);
  46.     }
  47.  
  48.     function sayToBot(text){
  49.     document.getElementById("msg_input").placeholder = "Type your messages here..."
  50.     $.post("/chat",
  51.     {
  52.         text:text,
  53.     },
  54.     function(jsondata, status){
  55.         if(jsondata["status"]=="success"){
  56.             response=jsondata["response"];
  57.             response_orig = jsondata["response_orig"]
  58.             console.log("==>",jsondata["response_orig"])
  59.         if(response){showBotMessage(response_orig);}
  60.          if(response){showBotMessage(response);}
  61.         }
  62.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement