Skip to content

Instantly share code, notes, and snippets.

@little-einstien
Last active April 29, 2019 07:14
Show Gist options
  • Save little-einstien/7616cbed41ba716ae4ee98e355455b91 to your computer and use it in GitHub Desktop.
Save little-einstien/7616cbed41ba716ae4ee98e355455b91 to your computer and use it in GitHub Desktop.
"""
This code sample is a part of a simple demo to show beginners how to create a skill (app) for the Amazon Echo using AWS Lambda and the Alexa Skills Kit.
"""
from __future__ import print_function
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'])
"""
Uncomment this if statement and populate with your skill's application ID to
prevent someone else from configuring a skill that sends requests to this
function.
"""
#This event fill come when alexa connected to lambda.
if event['session']['new']:
on_session_started({'requestId': event['request']['requestId']},
event['session'])
"""
This function will be called when you launch your skill.
So if you want to customize the welcome message go to get_welcome_response function
change this line --> speech_output = "Welcom to the Jarvis."
just edit the string whatever message you want.
"""
if event['request']['type'] == "LaunchRequest":
return on_launch(event['request'], event['session'])
if event['request']['type'] == "IntentRequest":
return on_intent(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
"""
# Dispatch to your skill's launch
return get_welcome_response()
"""
Whenver you ask something to alexa this fucnction will be called
"""
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
"""
put you condition here to respond to your intent
"""
if intent_name == "iron_man_intro": # change intent name here whatever your intent name is. You can if elif ladder for multiple intents
return handle_iron_man_intro_request() #Either create your new function or just go to this function and edit the speech_output value
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'])
# add cleanup logic here
# --------------- Functions that control the skill's behavior ------------------
def handle_iron_man_intro_request():
session_attributes = {}
card_title = "Welcome"
speech_output = "Iron Man is a fictional superhero appearing in American comic books published by Marvel Comics."
reprompt_text = speech_output
should_end_session = False
return build_response(session_attributes, build_speechlet_response(
card_title, speech_output, reprompt_text, should_end_session))
def get_welcome_response():
session_attributes = {}
card_title = "Welcome"
speech_output = "Welcom to the Jarvis." #Change here for your custom welcome message
# If the user either does not reply to the welcome message or says something
# that is not understood, they will be prompted again with the same text.
reprompt_text = speech_output
should_end_session = False
return build_response(session_attributes, build_speechlet_response(
card_title, 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment