基于Ajax的聊天機(jī)器人功能的實(shí)現(xiàn)
🤖️ 哈嘍!大家好呀。如果無(wú)聊就和機(jī)器人聊聊天吧
在初步進(jìn)入Ajax學(xué)習(xí) 就忍不住給大家分享今天的勞動(dòng)成果啦
先來(lái)看看效果圖:
功能實(shí)現(xiàn):
- 點(diǎn)擊發(fā)送按鈕事件
- 將用戶輸入的內(nèi)容渲染到頁(yè)面中
- 點(diǎn)擊回車(chē)鍵將表單的內(nèi)容渲染到頁(yè)面中
- 獲取機(jī)器人的內(nèi)容 渲染到頁(yè)面中
- 播放機(jī)器人的內(nèi)容
先來(lái)看看項(xiàng)目的總體結(jié)構(gòu)
引入相關(guān)的文件:
html框架比較簡(jiǎn)單
<div> <!-- 頭部 Header 區(qū)域 --> <div> <h3>小思同學(xué)</h3> <img src="img/person01.png" /> </div> <!-- 中間 聊天內(nèi)容區(qū)域 --> <div> <ul id="talk_list"><li> <img src="img/person01.png" /> <span>嗨,最近想我沒(méi)有?</span></li><!-- <li> <img src="img/person02.png" /> <span>你好哦</span> </li> --> </ul> <div><div></div> </div> </div> <!-- 底部 消息編輯區(qū)域 --> <div> <img src="img/person02.png" /> <input type="text" placeholder="說(shuō)的什么吧..." id="ipt" /> <input type="button" value="發(fā) 送" id="btnSend" /> </div> </div> <audio src="" id="voice" autoplay></audio>
<audio src="" id="voice" autoplay></audio>
這里的音頻播放標(biāo)簽,一定要添加autoplay屬性,自動(dòng)播放,不過(guò)不添加這個(gè)屬性,播放機(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;} /* 清除浮動(dòng),解決margin-top塌陷 */.clearfix:before,.clearfix:after{ content:""; display:table; }.clearfix:after{ clear:both;}.clearfix{ zoom:1;} .fl{ float:left;}.fr{ float:right;}
接下來(lái)就是本項(xiàng)目的精華所在
首先為發(fā)送按鈕綁定點(diǎn)擊事件,trim()方法是去除表單里面的空字符,開(kāi)始為表單內(nèi)容是否為空來(lái)一次判斷。
如果用戶輸入了內(nèi)容,將表單里面的內(nèi)容渲染到頁(yè)面,我相信大家都非常的熟練了
// 為發(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)容追加到頁(yè)面上顯示 $("#talk_list").append("<li><img src="img/person02.png" /> <span>" + text + "</span></li>") $("#ipt").val("")//文本為空 // 重置滾動(dòng)條的位置 resetui() // 發(fā)起請(qǐng)求,獲取聊天內(nèi)容 getMsg(text) })
接下來(lái)就是機(jī)器人的回復(fù)內(nèi)容啦:
用一個(gè)getMsg函數(shù)封裝 傳遞放入?yún)?shù)就是用戶輸入的內(nèi)容
下一些Ajax的get獲取內(nèi)容,根據(jù)文檔提供的地址http://www.liulongbin.top:3006/api/robot
當(dāng) res.message === "success" 表示獲取聊天信息成功 就接受聊天信息,將信息追加到頁(yè)面
// 獲取聊天機(jī)器人發(fā)送回來(lái)的消息 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><img src="img/person01.png" /> <span>" + msg + "</span></li>") // 重置滾動(dòng)條的位置 resetui() // 調(diào)用 getVoice 函數(shù),把文本轉(zhuǎn)化為語(yǔ)音 getVoice(msg)} } }) }
然后就是將文本轉(zhuǎn)化為語(yǔ)音播放功能
同樣的封裝一個(gè)函數(shù)getVoice() 傳遞的參數(shù)是接受到的機(jī)器人的聊天消息msg
// 把文字轉(zhuǎn)化為語(yǔ)音進(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)//下面的值可以通過(guò)console.log(res)輸出查看里面的屬性值if (res.status === 200) { // 播放語(yǔ)音 路徑 $("#voice").attr("src", res.voiceUrl)} } }) }
最后一個(gè)功能就是用戶按回車(chē)也可以發(fā)送消息
// 為文本框綁定 keyup 事件 $("#ipt").on("keyup", function(e) { // console.log(e.keyCode) if (e.keyCode === 13) { // console.log("用戶彈起了回車(chē)鍵") $("#btnSend").click() } })
大家趕快去試試吧
到此這篇關(guān)于基于Ajax的聊天機(jī)器人的文章就介紹到這了,更多相關(guān)Ajax聊天機(jī)器人內(nèi)容請(qǐng)搜索以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持!
相關(guān)文章:
1. PHP橋接模式Bridge Pattern的優(yōu)點(diǎn)與實(shí)現(xiàn)過(guò)程2. asp.net core項(xiàng)目授權(quán)流程詳解3. html中的form不提交(排除)某些input 原創(chuàng)4. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼5. CSS3中Transition屬性詳解以及示例分享6. bootstrap select2 動(dòng)態(tài)從后臺(tái)Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼7. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式8. 開(kāi)發(fā)效率翻倍的Web API使用技巧9. jsp文件下載功能實(shí)現(xiàn)代碼10. ASP常用日期格式化函數(shù) FormatDate()
