python alexa code example

Example 1: Alexa skill development using python tutorial

171sb = SkillBuilder()
172
173sb.add_request_handler(LaunchRequestHandler())
174sb.add_request_handler(JokeIntentHandler())
175sb.add_request_handler(HelloWorldIntentHandler())
176sb.add_request_handler(HelpIntentHandler())
177sb.add_request_handler(CancelOrStopIntentHandler())
178sb.add_request_handler(SessionEndedRequestHandler())
179
180# Make sure IntentReflectorHandler is last so it
181# Doesn't override your custom intent handlers
182sb.add_request_handler(IntentReflectorHandler())
183
184sb.add_exception_handler(CatchAllExceptionHandler())
185
186...

Example 2: 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()