jQuery Ajax chatbot implementation case study

jQuery Ajax chatbot implementation case study

Chatbots can save a lot of manual work and can be used in many situations, such as customer service, weather response, etc. This article will introduce jQuery Ajax chatbots in detail, as follows:

'

Implementation steps:

1. Sort out the code structure of the case

a. Sort out the UI layout of the page

b. Extract the business code into chat.js

c. Understand the function of resetui(): reset the position of the scroll bar

<link rel="stylesheet" href="css/reset.css" rel="external nofollow" />
<link rel="stylesheet" href="css/index.css" rel="external nofollow" />
<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/jquery-ui.min.js"></script>
<script src="js/jquery.mousewheel.js"></script>
        <div class="wrap">
      <!--Header area-->
      <div class="header">
        <h3>Xiao Xiang classmate</h3>
        <img src="images/person01.png" alt="icon" />
      </div>
 
      <!-- Chat content area-->
      <div class="main">
        <ul class="talk_list" style="top: 0px" id="talk_list">
          <li class="left_word">
            <img src="images/person01.png" /> <span>Hi, have you missed me recently? </span>
          </li>
          <!-- <li class="right_word">
            <img src="images/person02.png" /> <span>Hello</span>
          </li> -->
        </ul>
        <div class="drag_bar" style="display: none;">
            <div
              class="drager ui-draggable ui-draggable-handle"
              style="display: none; height: 412.628px;"
            ></div>
          </div>
      </div>
 
      <!-- Play audio -->
      <audio src="" id="voice" autoplay style="display: none;"></audio>
 
      <!-- Message editing area-->
      <div class="footer">
          <img src="images/person02.png" alt="">
          <input type="text" placeholder="Say something..." class="input_txt" id="ipt" />
          <input type="button" value="Send" class="input_sub" id="btnSend">
      </div>
    </div>
 
    <!-- Implement page scrolling -->
    <script src="js/scroll.js"></script>
    <script src="js/chat.js"></script>

index.css,

body {
    font-family: 'Microsoft YaHei';
}
 
.wrap {
    position: fixed;
    width: 450px;
    left: 50%;
    margin-left: -225px;
    top: 20px;
    bottom: 20px;
    border: 1px solid #ebebeb;
    background-color: #fff;
    border-radius: 10px;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.1);
    overflow: hidden;
}
 
.header {
    height: 55px;
    background: linear-gradient(90deg, rgba(246, 60, 47, 0.6), rgba(128, 58, 242, 0.6));
    overflow: hidden;
}
 
.header h3 {
    color: #faf3fc;
    line-height: 55px;
    font-weight: normal;
    float: left;
    letter-spacing: 2px;
    margin-left: 25px;
    font-size: 18px;
    text-shadow: 0px 0px 5px #944846;
}
 
.header img {
    float: right;
    margin: 7px 25px 0 0;
    border-radius: 20px;
    box-shadow: 0 0 5px #f7f2fe;
}
 
.main {
    position: absolute;
    left: 0;
    right: 0;
    top: 55px;
    bottom: 55px;
    background-color: #f4f3f3;
    box-sizing: border-box;
    padding: 10px 0;
    overflow: hidden;
}
 
.talk_list {
    position: absolute;
    width: 100%;
    left: 0px;
    top: 0px;
}
 
.talk_list li {
    overflow: hidden;
    margin: 20px 0px 30px;
}
 
.talk_list .left_word img {
    float: left;
    margin-left: 20px;
}
 
.talk_list .left_word span {
    float: left;
    background-color: #fe9697;
    padding: 10px 15px;
    max-width: 290px;
    border-radius: 12px;
    font-size: 16px;
    color: #fff;
    margin-left: 13px;
    position: relative;
    line-height: 24px;
}
 
.talk_list .left_word span:before {
    content: '';
    position: absolute;
    left: -8px;
    top: 3px;
    width: 13px;
    height: 12px;
    background: url('../images/corner01.png') no-repeat;
}
 
.talk_list .right_word img {
    float: right;
    margin-right: 20px;
}
 
.talk_list .right_word span {
    float: right;
    background-color: #fff;
    padding: 10px 15px;
    max-width: 290px;
    border-radius: 12px;
    font-size: 16px;
    color: #000;
    margin-right: 13px;
    position: relative;
    line-height: 24px;
}
 
.talk_list .right_word span:before {
    content: '';
    position: absolute;
    right: -8px;
    top: 3px;
    width: 13px;
    height: 12px;
    background: url('../images/corner02.png') no-repeat;
}
 
.drag_bar {
    position: absolute;
    right: 0px;
    top: 0px;
    background-color: #fff;
    height: 100%;
    width: 6px;
    box-sizing: border-box;
    border-bottom: 1px solid #f4f3f3;
}
 
.drager {
    position: absolute;
    left: 0px;
    top: 0px;
    background-color: #cdcdcd;
    height: 100px;
    width: 6px;
    border-radius: 3px;
    cursor: pointer;
}
 
.footer {
    width: 100%;
    height: 55px;
    left: 0px;
    bottom: 0px;
    background-color: #fff;
    position: absolute;
}
 
.footer img {
    float: left;
    margin: 8px 0 0 20px;
}
 
.input_txt {
    float: left;
    width: 270px;
    height: 37px;
    border: 0px;
    background-color: #f4f3f3;
    margin: 9px 0 0 20px;
    border-radius: 8px;
    padding: 0px;
    outline: none;
    text-indent: 15px;
}
 
.input_sub {
    float: left;
    width: 70px;
    height: 37px;
    border: 0px;
    background-color: #fe9697;
    margin: 9px 0 0 15px;
    border-radius: 8px;
    padding: 0px;
    outline: none;
    color: #fff;
    cursor: pointer;
}

The effect is as follows:

2. Render the user input to the chat window

chat.js,

    //Reset the scroll bar position resetui();
 
    //Bind the mouse click event to the send button$("#btnSend").on('click', function () {
        let text = $("#ipt").val().trim(); //Content to be sent// Determine whether the content to be sent is empty if (text.length <= 0) {
            return $("#ipt").val('');
        }
 
        //If the user enters chat content, append the chat content to the page for display $("#talk_list").append(`<li class="right_word"><img src="images/person02.png" /> <span>${text}</span></li>`);
        
        //Clear the input box after sending$("#ipt").val('');
        
        //Reset the scroll bar position resetui();
    }); 

3. Initiate a request to obtain chat messages

chat.js,

    //Define a function to request data from the server -- get the data sent back by the chatbot function getMsg(text){
        $.ajax({
            method: 'GET',
            url: 'http://www.liulongbin.top:3006/api/robot',
            data: {// Submit the message sent by the user to the server spoke: text
            },
            success: function(res){
                // console.log(res);
 
                //Judge whether the request is successful data.info.text
                if(res.message === "success"){
                    //Receive the chat message returned by the server let msg = res.data.info.text;
                    // console.log(msg);
 
                    // Render the chat message returned by the server to the chat interface $("#talk_list").append(`<li class="left_word"><img src="images/person01.png" /> <span>${msg}</span></li>`);
                    //Reset the scroll bar position resetui();
                }
            }
        })
    } 

4. Convert the robot’s chat content into voice

5. Play audio via <audio>

    //Convert the robot's chat content into voice function getVoice(text){
        $.ajax({
            method: 'GET',
            url: 'http://www.liulongbin.top:3006/api/synthesize',
            data: {
                text: text
            },
            success: function(res){
                // console.log(res);
 
                //Judge whether the request is successful if (res.status === 200) {
                    //Play voice$("#voice").attr("src",res.voiceUrl);
                }
            }
        })
    } 

6. Use the Enter key to send the message

    //Bind events to the text input box -- automatically send chat content when the Enter key is pressed and released $("#ipt").on('keyup',function(e){
        // console.log(e.keyCode); // 13
 
        //Determine whether the user pressed the Enter key if(e.keyCode === 13){
            //Call the click function of the button element to send its content $("#btnSend").click();
        }
    }) 

This is the end of this article about the complete case of implementing Ajax chatbot with jQuery. For more relevant jQuery Ajax chatbot content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • jQuery chatbot implementation

<<:  MySQL query_cache_type parameter and usage details

>>:  Html comments Symbols for marking text comments in Html

Recommend

Detailed explanation of Vue mixin

Table of contents Local Mixin Global Mixins Summa...

Some notes on modifying the innodb_data_file_path parameter of MySQL

Preface innodb_data_file_path is used to specify ...

Angular framework detailed explanation of view abstract definition

Preface As a front-end framework designed "f...

In-depth analysis of MySQL execution plans

Preface In the previous interview process, when a...

Why is the MySQL auto-increment primary key not continuous?

Table of contents 1. Introduction 2. Self-increme...

Analysis of SQL integrity constraint statements in database

Integrity constraints Integrity constraints are f...

Detailed explanation of the use of state in React's three major attributes

Table of contents Class Component Functional Comp...

MySQL SHOW PROCESSLIST assists in the entire process of troubleshooting

1. SHOW PROCESSLIST command SHOW PROCESSLIST show...

Web developers are concerned about the coexistence of IE7 and IE8

I installed IE8 today. When I went to the Microso...

How to install Docker and configure Alibaba Cloud Image Accelerator

Docker Installation There is no need to talk abou...

The qualities and abilities a web designer should have

Web design is an emerging marginal industry that c...