DEV Community

Discussion on: How to make HTTP Requests from an Alexa Skill to Get Data from an External API using Python 3

Collapse
 
w4krl profile image
Karl Berger • Edited

As a complete newbie, I don't know where to begin with your example. I have an Alexa Developer Console account and created a skill with the Custom Model and Alexa-Hosted Python. The next screen forces me to choose a template from Hello World Skill, Fact Skill, Quiz Game, High-Low Game, or Sauce Boss. None seem appropriate. I tried adding your example to Hello World but it didn't work. It says "Sorry, I had trouble doing what you asked. Please try again." Once it said "Here's something I found on the web. According to wikipedia.org: Chuck says that the fact she doesn't like olives is the only thing he knows about her that is real."

Here is my modification to the Hello World code:

import requests

def getResponseFromAPI():
url = 'api.chucknorris.io/jokes/random'
response = requests.get(url)
print(response.status_code)

json_data = response.json()
joke = json_data['value']
return joke

class LaunchRequestHandler(AbstractRequestHandler):
"""Handler for Skill Launch."""
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool

    return ask_utils.is_request_type("LaunchRequest")(handler_input)

def handle(self, handler_input):
# type: (HandlerInput) -> Response
logging.info("LaunchRequest was trigerred")

joke = getResponseFromAPI()
return (
        handler_input.response_builder
        .speak(f"{joke}. Would you like to hear another Chuck Norris joke?")
        .ask("Would you like to hear another joke?")
        .response
    )

class ChuckNorrisIntentHandler(AbstractRequestHandler):
"""Handler for Chuck Norris Intent."""
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return ask_utils.is_intent_name("ChuckNorrisIntent")(handler_input)

def handle(self, handler_input):
    # type: (HandlerInput) -> Response
    speak_output = "Hi from Chuck Norris!"

    return (
        handler_input.response_builder
            .speak(speak_output)
            # .ask("add a reprompt if you want to keep the session open for the user to respond")
            .response
    )