Echo Dot and Alexa
After playing around enough with my new Amazon Echo Dot, I set myself to do something cool using Alexa Skill Kit (ASK) a new voice-driven capabilities for Alexa. Most part of my work involve Amazon Web Service(AWS) , it has lot of services and a voice based assistant to manage the resources can come handy. Hence this idea for a weekend project.
How this Works
Alexa Skill Set
Coding a new skill set is pretty easy. Login to the Amazon Developer Portal to create a custom skill set using Alexa Skill Kit
First step is skill information, custom skill type and its assigned an unique Application Id. Give your skill a cool name and an Invocation name. Invocation name is what you use to activate your skill. "Alexa, ask my assistant to start server .."
Interactive Model Builder makes it very easy to define an Intent, Slots and Sample Utterance. An intent represents an action that fulfills a user’s spoken request. startInstanceIntent in this project is used to start an EC2 Instance. A slot represents list of values used by the skill. Custom slot instance of a custom slot type INSTANCE_NAME contains values dev, qa, prod, uat. Sample Utterance is structures text that links the intent to likely spoken words. For example "start server dev". Save and build your model. Next step is configuration, we need an AWS Lambda ARN service endpoint.
AWS Lambda
Yeah, the latest buzz word in cloud computing - serverless architecture. AWS lambda lets you run code without provisioning or managing servers. Login to AWS console and create Lambda function. Select configure triggers tab and choose Alexa Skills Kit from the dropdown. Name your function and choose language in runtime section. in my case it is python 2.7
Below code goes in the function section, first things first we import boto3 package and define lambda_handler to route the control to functions on_launch, on_intent etc.
from __future__ import print_function
import boto3
def lambda_handler(event, context):
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'])
In the on_intent function, we map intents. Based on intent name, call corresponding python function.
def on_intent(intent_request, session):
print("on_intent requestId=" + intent_request['requestId'] +
", sessionId=" + session['sessionId'])
intent = intent_request['intent']
intent_name = intent_request['intent']['name']
if intent_name == "startInstanceIntent":
return start_ec2(intent, session)
elif intent_name == "stopInstanceIntent":
return stop_ec2(intent, session)
elif intent_name == "getStatusIntent":
return getStatus(intent, session)
elif intent_name == "AMAZON.CancelIntent" or intent_name == "AMAZON.StopIntent":
return handle_session_end_request()
else:
raise ValueError("Invalid intent")
Define start_ec2 function, based on the Instance slot value the corresponding ec2 instance will start. speech_output is the text alexa returns to the user.
def start_ec2(intent, session):
card_title = "Starting"
session_attributes = {}
instance_value = intent['slots']['Instance']['value']
instanceValue = instance_value.lower()
ec2 = boto3.client('ec2', region_name='us-east-1')
response = ec2.describe_instances()
insId = []
for reservation in response["Reservations"]:
for instance in reservation["Instances"]:
for i in instance["Tags"]:
if(i["Value"]==instanceValue):
insId.append(instance["InstanceId"])
ec2.start_instances(InstanceIds=insId)
speech_output = "Starting instance"+ instance_value + ". Thank you for using AWS Assistant"
should_end_session = True
return build_response(session_attributes, build_speechlet_response(
card_title, speech_output, None, should_end_session))
Lastly we will write the helper functions that will be used to build out the JSON responses for the Echo. Save and text your lambda function and get the ARN name to be provided in the alexa skill kit configuration.
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
}
Provide the Lambda ARN and test your skill with service simulator. Type in the sample utterance text and you should be able to see the Lambda response. If you have amazon echo device linked the same account, you should be able to test this skill directly on the device.
Ain't that cool?
Demo
Comments
Please log in or sign up to comment.