我有一个可用的python聊天机器人,使用flask框架实现了该网页。我正在尝试使用“ SpeechSynthesisUtterance”进行语音输入/输出。如何使合成器检测到机器人回复?
**FLASK**
app = Flask(__name__)
@app.route("/")
def home():
return render_template("index.html")
@app.route("/get")
def get_bot_response():
userText = request.args.get('msg')
return str(bot.get_response(userText))
if __name__ == "__main__":
app.run(debug=False,port=5000)
**HTML**
<div id="chatbox">
<p class="botText"><span>Hi! I'm ChatBot.</span></p>
<p class="botText"><span>How Can I help You?.</span></p>
</div>
<div class="row" id="userInput">
<input id="textInput" type="text" name="msg" placeholder="Message">
<input id="buttonInput" type="submit" value="Send">
</div>
**SCRIPT**
<script>
function getBotResponse() {
var rawText = $("#textInput").val();
var userHtml = '<p class="userText"><span>' + rawText + '</span></p>';
$("#textInput").val("");
$("#chatbox").append(userHtml);
document.getElementById('userInput').scrollIntoView({block: 'start', behavior: 'smooth'});
$.get("/get", { msg: rawText }).done(function(data) {
var botHtml = '<p class="botText"><span>' + data + '</span></p>';
$("#chatbox").append(botHtml);
document.getElementById('userInput').scrollIntoView({block: 'start', behavior: 'smooth'});
});
var bot = new SpeechSynthesisUtterance('Hello World');
window.speechSynthesis.speak(bot);
}
$("#textInput").keypress(function(e) {
if(e.which == 13) {
getBotResponse();
}
});
$("#buttonInput").click(function() {
getBotResponse();
})
</script>
我希望var bot = new SpeechSynthesisUtterance('Hello World')
会说出实际的漫游器回复,而不是hello world
。