Esempio n. 1
0
def lambda_handler(event, context):
    """ Route the incoming request based on type (LaunchRequest, IntentRequest,
    etc.) The JSON body of the request is provided in the event parameter.
    """
    logging.basicConfig(level=getattr(config, 'log_level', 'INFO'),
                        stream=sys.stderr)

    # This `if` prevents other Skills from using this Lambda
    if event['session']['application']['applicationId'] != config.APP_ID:
        raise ValueError("Invalid Application ID")

    try:
        if event['request']['type'] == "IntentRequest":
            return handle.intent(event['request'], event['session'])
        elif event['request']['type'] == "LaunchRequest":
            return reply.build(
                "",  # TODO: Write this
                is_end=False)
        elif event['request']['type'] == "SessionEndedRequest":
            return reply.build("Goodbye!", is_end=True)
        else:
            # I don't think there's any other kinds of requests.
            return reply.build(
                "",  # TODO: Write this
                is_end=False)
    except ReadTimeout:
        log.exception('Timeout accessing Rainforest cloud.')
        return reply.build("I couldn't access the Rainforest Cloud.",
                           is_end=True)
    except Exception as err:  # NOQA
        log.exception('Unhandled exception for event\n%s\n' % str(event))
        return reply.build("Sorry, something went wrong.",
                           persist=event['session'].get('attributes', {}),
                           is_end=True)
Esempio n. 2
0
def check_price(intent, session):
    price = _poller().get_price()['Price']
    card_text = "$%.2f per kilowatt-hour" % (price / 100)
    return reply.build("It's %s %s." % (str(price)[:-2], str(price)[-2:]),
                       card_title="Your Electicity Price",
                       card_text='\n'.join(card_text),
                       is_end=True)
Esempio n. 3
0
def check_demand(intent, session):
    demand = _poller().get_instantaneous_demand()['Demand']

    utterance = "Current demand is "
    if demand < 1:
        utterance += "%d Watts." % (demand * 1000)
    else:
        utterance += "%d kilowatts." % demand
    return reply.build(utterance, is_end=True)
Esempio n. 4
0
def intent(req, session):
    """Identify and handle IntentRequest objects

    Parameters
    ----------
    req : dict
        JSON following the Alexa "IntentRequest" schema
    session : dict
        JSON following the Alexa "Session" schema

    Returns
    -------
    dict
        JSON following the Alexa reply schema
    """
    intent = req['intent']
    if session.setdefault('attributes', {}) is None:
        # Ensure that there's always a dictionary under "attributes".
        session['attributes'] = {}

    # Dispatch each Intent to the correct handler.
    if intent['name'] == 'CheckDemandIntent':
        return check_demand(intent, session)
    elif intent['name'] == 'CheckPriceIntent':
        return check_price(intent, session)
    elif intent['name'] == 'CheckSummationIntent':
        return check_consumption(intent, session)
    elif intent['name'] in ['AMAZON.StopIntent', 'AMAZON.CancelIntent']:
        return reply.build("Okay, exiting.", is_end=True)
    elif intent['name'] == 'AMAZON.HelpIntent':
        return reply.build(
            "This sentence is helpful.",  # TODO
            is_end=False)
    else:
        return reply.build("I didn't understand that. Try again?",
                           persist=session['attributes'],
                           is_end=False)
Esempio n. 5
0
def check_consumption(intent, session):
    return reply.build("This code not yet written.", is_end=True)