基于Ajax的聊天機(jī)器人功能的實(shí)現(xiàn)
🤖️ 哈嘍!大家好呀。如果無聊就和機(jī)器人聊聊天吧
在初步進(jìn)入Ajax學(xué)習(xí) 就忍不住給大家分享今天的勞動成果啦
先來看看效果圖:
功能實(shí)現(xiàn):
- 點(diǎn)擊發(fā)送按鈕事件
- 將用戶輸入的內(nèi)容渲染到頁面中
- 點(diǎn)擊回車鍵將表單的內(nèi)容渲染到頁面中
- 獲取機(jī)器人的內(nèi)容 渲染到頁面中
- 播放機(jī)器人的內(nèi)容
先來看看項目的總體結(jié)構(gòu)
引入相關(guān)的文件:
html框架比較簡單
<div class="wrap"> <!-- 頭部 Header 區(qū)域 --> <div class="header"> <h3>小思同學(xué)</h3> <img src="img/person01.png" alt="icon" /> </div> <!-- 中間 聊天內(nèi)容區(qū)域 --> <div class="main"> <ul class="talk_list" style="top: 0px;" id="talk_list"> <li class="left_word"> <img src="img/person01.png" /> <span>嗨,最近想我沒有?</span> </li> <!-- <li class="right_word"> <img src="img/person02.png" /> <span>你好哦</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> <!-- 底部 消息編輯區(qū)域 --> <div class="footer"> <img src="img/person02.png" alt="icon" /> <input type="text" placeholder="說的什么吧..." class="input_txt" id="ipt" /> <input type="button" value="發(fā) 送" class="input_sub" id="btnSend" /> </div> </div> <audio src="" id="voice" autoplay style="display: none;"></audio>
<audio src="" id="voice" autoplay style="display: none;"></audio>
這里的音頻播放標(biāo)簽,一定要添加autoplay屬性,自動播放,不過不添加這個屬性,播放機(jī)器人的功能就不能實(shí)現(xiàn)喲
main.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('../img/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('../img/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; }
reset.css部分
body,ul,h1,h2,h3,h4,h5,h6{ margin: 0; padding: 0; } h1,h2,h3,h4,h5,h6{ font-size:100%; font-weight:normal; } a{ text-decoration:none; } ul{ list-style:none; } img{ border:0px; } /* 清除浮動,解決margin-top塌陷 */ .clearfix:before,.clearfix:after{ content:''; display:table; } .clearfix:after{ clear:both; } .clearfix{ zoom:1; } .fl{ float:left; } .fr{ float:right; }
接下來就是本項目的精華所在
首先為發(fā)送按鈕綁定點(diǎn)擊事件,trim()方法是去除表單里面的空字符,開始為表單內(nèi)容是否為空來一次判斷。
如果用戶輸入了內(nèi)容,將表單里面的內(nèi)容渲染到頁面,我相信大家都非常的熟練了
// 為發(fā)送按鈕綁定鼠標(biāo)點(diǎn)擊事件 $('#btnSend').on('click', function() { var text = $('#ipt').val().trim() if (text.length <= 0) { return $('#ipt').val('') //用戶輸入的內(nèi)容為空 } // 如果用戶輸入了聊天內(nèi)容,則將聊天內(nèi)容追加到頁面上顯示 $('#talk_list').append('<li class="right_word"><img src="img/person02.png" /> <span>' + text + '</span></li>') $('#ipt').val('')//文本為空 // 重置滾動條的位置 resetui() // 發(fā)起請求,獲取聊天內(nèi)容 getMsg(text) })
接下來就是機(jī)器人的回復(fù)內(nèi)容啦:
用一個getMsg函數(shù)封裝 傳遞放入?yún)?shù)就是用戶輸入的內(nèi)容
下一些Ajax的get獲取內(nèi)容,根據(jù)文檔提供的地址http://www.liulongbin.top:3006/api/robot
當(dāng) res.message === 'success' 表示獲取聊天信息成功 就接受聊天信息,將信息追加到頁面
// 獲取聊天機(jī)器人發(fā)送回來的消息 function getMsg(text) { $.ajax({ method: 'GET', url: ' http://www.liulongbin.top:3006/api/robot', data: { spoken: text }, success: function(res) { // console.log(res) if (res.message === 'success') { // 接收聊天消息 var msg = res.data.info.text $('#talk_list').append('<li class="left_word"><img src="img/person01.png" /> <span>' + msg + '</span></li>') // 重置滾動條的位置 resetui() // 調(diào)用 getVoice 函數(shù),把文本轉(zhuǎn)化為語音 getVoice(msg) } } }) }
然后就是將文本轉(zhuǎn)化為語音播放功能
同樣的封裝一個函數(shù)getVoice() 傳遞的參數(shù)是接受到的機(jī)器人的聊天消息msg
// 把文字轉(zhuǎn)化為語音進(jìn)行播放 function getVoice(text) { $.ajax({ method: 'GET', url: ' http://www.liulongbin.top:3006/api/synthesize', data: { text: text }, success: function(res) { // console.log(res) //下面的值可以通過console.log(res)輸出查看里面的屬性值 if (res.status === 200) { // 播放語音 路徑 $('#voice').attr('src', res.voiceUrl) } } }) }
最后一個功能就是用戶按回車也可以發(fā)送消息
// 為文本框綁定 keyup 事件 $('#ipt').on('keyup', function(e) { // console.log(e.keyCode) if (e.keyCode === 13) { // console.log('用戶彈起了回車鍵') $('#btnSend').click() } })
大家趕快去試試吧
到此這篇關(guān)于基于Ajax的聊天機(jī)器人的文章就介紹到這了,更多相關(guān)Ajax聊天機(jī)器人內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
jQuery的ajax傳參巧用JSON使用示例(附Json插件)
jQuery的ajax調(diào)用很方便,傳參的時候喜歡用Json的數(shù)據(jù)格式,使用示例代碼如下,感興趣的朋友可以參考下,希望對大家有所幫助2013-08-08利用AjaxSubmit()方法實(shí)現(xiàn)Form提交表單后回調(diào)功能
ajaxSubmit()方法是JQuery Form表單插件中的方法,使用時,需要在jsp或者h(yuǎn)tml頁面上,引入JQuery庫和Form插件。接下來通過本文給大家分享通過AjaxSubmit()方法實(shí)現(xiàn)Form提交表單后回調(diào)功能,感興趣的朋友跟隨腳本之家小編一起看看吧2018-05-05前端實(shí)現(xiàn)滑動按鈕AJAX與后端交互的示例代碼
這篇文章主要介紹了前端實(shí)現(xiàn)滑動按鈕AJAX與后端交互的示例代碼,前端代碼分為html代碼和css代碼,通過js事件觸發(fā),代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2022-02-02深入淺析AjaxFileUpload實(shí)現(xiàn)單個文件的 Ajax 文件上傳庫
jQuery.AjaxFileUpload.js是一款jQuery插件,用于通過ajax上傳文件。本文給大家介紹AjaxFileUpload實(shí)現(xiàn)單個文件的 Ajax 文件上傳庫,對此感興趣的朋友一起學(xué)習(xí)吧2016-04-04