Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Pete Hoffswell
Published © CC BY

Amazon Echo Alexa ThingSpeak Data Checker

Do you find yourself checking the last entry of your ThingSpeak data trend all the time? Don't you wish you could just ask Alexa?

IntermediateProtip2 hours6,978
Amazon Echo Alexa ThingSpeak Data Checker

Things used in this project

Story

Read more

Code

Interaction Model - Intent Schema

JSON
This is the Intent Schema for sending intents up to Amazon Lambda. You can see there's only really one intent, SpeedTestIntent.
{
  "intents": [
    {
      "intent": "SpeedTestIntent"
    },
    {
      "intent": "AMAZON.HelpIntent"
    }
    ,
    {
      "intent": "AMAZON.StopIntent"
    }
  ]
}

Interaction Model - Sample Utterances

snippets
What can you say to Alexa, and what will it do? Pretty easy, with only one Intent:
SpeedTestIntent what's my internet speed
SpeedTestIntent what is my internet speed
SpeedTestIntent What's my last speed
SpeedTestIntent What was my last speed
SpeedTestIntent last speed
SpeedTestIntent What was my last download
SpeedTestIntent check speed

Lambda Code

Python
Here's your lambda Code
"""
This skill returns data from a thinkspeak channel.  In my case, this is a regular speed test.

Ask alexa "what's my internet speed" to get the last download speed.

May, 2016
pete@hoffswell.com

"""

from __future__ import print_function
import urllib

# Change these elements to point to your data
channel = 91147
field = 2
#

link = "https://api.thingspeak.com/channels/" + \
    str(channel) + \
    "/fields/" + \
    str(field) + \
    "/last"

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.
    """
    print("event.session.application.applicationId=" +
          event['session']['application']['applicationId'])

    if event['session']['new']:
        on_session_started({'requestId': event['request']['requestId']},
                           event['session'])

    if event['request']['type'] == "LaunchRequest":
        return on_launch(event['request'], event['session'])
    elif event['request']['type'] == "IntentRequest":
        return on_intent(event['request'], event['session'])
    elif event['request']['type'] == "SessionEndedRequest":
        return on_session_ended(event['request'], event['session'])


def on_session_started(session_started_request, session):
    """ Called when the session starts """

    print("on_session_started requestId=" + session_started_request['requestId']
          + ", sessionId=" + session['sessionId'])


def on_launch(launch_request, session):
    """ Called when the user launches the skill without specifying what they
    want
    """

    print("on_launch requestId=" + launch_request['requestId'] +
          ", sessionId=" + session['sessionId'])
    # Dispatch to your skill's launch
    return get_welcome_response()


def on_intent(intent_request, session):
    """ Called when the user specifies an intent for this skill """

    print("on_intent requestId=" + intent_request['requestId'] +
          ", sessionId=" + session['sessionId'])

    intent = intent_request['intent']
    intent_name = intent_request['intent']['name']

    # Dispatch to your skill's intent handlers
    if intent_name == "SpeedTestIntent":
        return run_speed_test(intent, session, link)
    elif intent_name == "AMAZON.HelpIntent":
        return get_welcome_response()
    elif intent_name == "AMAZON.CancelIntent" or intent_name == "AMAZON.StopIntent":
        return handle_session_end_request()
    else:
        raise ValueError("Invalid intent")


def on_session_ended(session_ended_request, session):
    """ Called when the user ends the session.

    Is not called when the skill returns should_end_session=true
    """
    print("on_session_ended requestId=" + session_ended_request['requestId'] +
          ", sessionId=" + session['sessionId'])

# --------------- Functions that control the skill's behavior ------------------

def get_welcome_response():

    session_attributes = {}
    card_title = "Welcome"
    speech_output = "Welcome to Speed Test. " \
                    "I can tell you the results of the last speed test at Thing Speak. " \
                    "Just ask me to check speed"
    # If the user either does not reply to the welcome message or says something
    # that is not understood, they will be prompted again with this text.
    reprompt_text = "Ask me, what is my internet speed. "
    should_end_session = False
    return build_response(session_attributes, build_speechlet_response(
        card_title, speech_output, reprompt_text, should_end_session))

def handle_session_end_request():
    card_title = "Session Ended"
    speech_output = "I hope that speed is fast enough for you. " \
                    "Have a nice day! "
    # Setting this to true ends the session and exits the skill.
    should_end_session = True
    return build_response({}, build_speechlet_response(
        card_title, speech_output, None, should_end_session))

# Run the Speed Test
def run_speed_test(intent, session, link):
    session_attributes = {}
    reprompt_text = None
    should_end_session = False
    
    f = urllib.urlopen(link) # Get your data
    result = f.read()
    
    speech_output = "Your last speed was " + \
                    result + \
                    " megabits per second."
                    
    ## Comment on the awesomeness
    if result > 60:
        comment = " That sounds good, I guess."
    elif result > 30:
        comment = " It seems somewhat lacking to me."
    elif result > 10:
        comment = " Wow, that stinks!"
    elif result < 11 :
        comment = " That's terrible!"
    else:
        comment = " I don't really know what to tell you about that."
       
    speech_output = speech_output + comment
    
    return build_response(session_attributes, build_speechlet_response(
        intent['name'], speech_output, reprompt_text, should_end_session))

# --------------- Helpers that build all of the responses ----------------------

def build_speechlet_response(title, output, reprompt_text, should_end_session):
    return {
        'outputSpeech': {
            'type': 'PlainText',
            'text': output
        },
        'card': {
            'type': 'Simple',
            'title': 'SessionSpeechlet - ' + title,
            'content': 'SessionSpeechlet - ' + output
        },
        'reprompt': {
            'outputSpeech': {
                'type': 'PlainText',
                'text': reprompt_text
            }
        },
        'shouldEndSession': should_end_session
    }


def build_response(session_attributes, speechlet_response):
    return {
        'version': '1.0',
        'sessionAttributes': session_attributes,
        'response': speechlet_response
    }

Credits

Pete Hoffswell

Pete Hoffswell

10 projects • 63 followers
Network Engineer for All the People. Internet advocate. Microcontroller Fan. Maker.

Comments