Hello, I am new in rasa chatbot. I am trying to implement voice in my bot , I am using rasa web chat https://github.com/botfront/rasa-webchat and I have a python code that contains the voice. still searching for a solution ,how to link this python file with rasa webchat. any idea please ? this is the python code:
import requests from gtts import gTTS from playsound import playsound import speech_recognition as sr # import the library import os
bot_message = “” message=""
r = requests.post(‘http://localhost:5002/webhooks/rest/webhook’, json={“message”: “hello”})
print(“Bot says, “,end=’ ') for i in r.json(): bot_message = i[‘text’] print(f”{bot_message}”)
tts =gTTS(text=bot_message , lang=“en”) tts.save(“hello.mp3”) audio_file = os.path.dirname(file) + “hello.mp3” playsound(audio_file) #playsound=playsound(“hello.mp3”) os.remove(audio_file)
while bot_message != “goodbye” or bot_message!=‘thank you’: r = sr.Recognizer() # initialize recognizer with sr.Microphone() as source: # mention source it will be either Microphone or audio files. print(“Speak Anything :”) audio = r.listen(source) # listen to the source try: message = r.recognize_google(audio) # use recognizer to convert our audio into text part. print(“You said : {}”.format(message))
except:
print("Sorry could not recognize your voice") # In case of voice not recognized clearly
if len(message)==0:
continue
print("Sending message now...")
r = requests.post('http://localhost:5002/webhooks/rest/webhook', json={"message": message})
print("Bot says, ",end=' ')
for i in r.json():
bot_message = i['text']
print(f"{bot_message}")
tts =gTTS(text=bot_message , lang="ar")
tts.save("hello.mp3")
audio_file = os.path.dirname(__file__) + "hello.mp3"
playsound(audio_file)
#playsound=playsound("hello.mp3")
os.remove(audio_file)