amazon alexa python code example
Example: alexa in python
import wolframalpha
import pyttsx3
import speech_recognition as sr
"""
here you need to have an API key on wolframealpha.com
"""
client = wolframalpha.Client(API-Key)
res = client.query("a")
r = sr.Recognizer()
r = sr.Recognizer()
engine = pyttsx3.init()
def talk(speak):
engine.say(str(speak))
engine.runAndWait()
run = True
while True:
try:
with sr.Microphone() as source2:
print("I am listening...")
r.adjust_for_ambient_noise(source2, duration=0.2)
audio2 = r.listen(source2)
MyText = r.recognize_google(audio2)
MyText = MyText.lower()
if MyText == "bye":
run = False
break
if run:
res = client.query(MyText)
print(next(res.results).text)
talk(next(res.results).text)
except:
engine.say("An error occurred...")
engine.runAndWait()
engine.say("Bye...")
engine.runAndWait()