alexa amazon python code example

Example: alexa in python

import wolframalpha
# pip install wolframalpha
import pyttsx3
# Pip install pyttsx3
import speech_recognition as sr
# pip install speech_rocognition



#API key
"""
here you need to have an API key on wolframealpha.com
"""
client = wolframalpha.Client(API-Key)

# Result
res = client.query("a")
r = sr.Recognizer()

# recognition bot
r = sr.Recognizer()

# init pyttsx3
engine = pyttsx3.init()


# def
def talk(speak):
    engine.say(str(speak))
    engine.runAndWait()


# Main
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()