如何使“speechsynthesistence”选择“bot reply”和“user text”?

2024-05-18 12:34:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个工作的python聊天机器人,使用flask框架实现到一个网页。我正在尝试使用“speechsynthesisterance”进行语音输入/输出。如何使合成器检测到bot应答

**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')说出实际的bot回复,而不是hello world


Tags: namedividappgetvarbotfunction

热门问题