Created
February 20, 2018 16:18
-
-
Save mr-rigden/04e083d98d041f9a79096228d6b299a8 to your computer and use it in GitHub Desktop.
Dolly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################## | |
# Builders | |
############################## | |
def build_PlainSpeech(body): | |
speech = {} | |
speech['type'] = 'PlainText' | |
speech['text'] = body | |
return speech | |
def build_response(message, session_attributes={}): | |
response = {} | |
response['version'] = '1.0' | |
response['sessionAttributes'] = session_attributes | |
response['response'] = message | |
return response | |
def build_SimpleCard(title, body): | |
card = {} | |
card['type'] = 'Simple' | |
card['title'] = title | |
card['content'] = body | |
return card | |
############################## | |
# Responses | |
############################## | |
def conversation(title, body, session_attributes): | |
speechlet = {} | |
speechlet['outputSpeech'] = build_PlainSpeech(body) | |
speechlet['card'] = build_SimpleCard(title, body) | |
speechlet['shouldEndSession'] = False | |
return build_response(speechlet, session_attributes=session_attributes) | |
def statement(title, body): | |
speechlet = {} | |
speechlet['outputSpeech'] = build_PlainSpeech(body) | |
speechlet['card'] = build_SimpleCard(title, body) | |
speechlet['shouldEndSession'] = True | |
return build_response(speechlet) | |
def continue_dialog(): | |
message = {} | |
message['shouldEndSession'] = False | |
message['directives'] = [{'type': 'Dialog.Delegate'}] | |
return build_response(message) | |
############################## | |
# Custom Intents | |
############################## | |
def sing_intent(event, context): | |
song = "Daisy, Daisy. Give me your answer, do. I'm half crazy all for the love of you" | |
return statement("daisy_bell_intent", song) | |
def counter_intent(event, context): | |
if 'attributes' not in event['session']: | |
event['session']['attributes'] = {} | |
if "counter" in event['session']['attributes']: | |
event['session']['attributes']['counter'] += 1 | |
else: | |
event['session']['attributes']['counter'] = 1 | |
return conversation("counter_intent", | |
event['session']['attributes']['counter'], | |
event['session']['attributes']) | |
def trip_intent(event, context): | |
dialog_state = event['request']['dialogState'] | |
if dialog_state in ("STARTED", "IN_PROGRESS"): | |
return continue_dialog() | |
elif dialog_state == "COMPLETED": | |
return statement("trip_intent", "Have a good trip") | |
else: | |
return statement("trip_intent", "No dialog") | |
############################## | |
# Required Intents | |
############################## | |
def cancel_intent(): | |
return statement("CancelIntent", "You want to cancel") | |
def help_intent(): | |
return statement("CancelIntent", "You want help") | |
def stop_intent(): | |
return statement("StopIntent", "You want to stop") | |
############################## | |
# On Launch | |
############################## | |
def on_launch(event, context): | |
return statement("on_launch", "This is just a template") | |
############################## | |
# Routing | |
############################## | |
def intent_router(event, context): | |
intent = event['request']['intent']['name'] | |
# Custom Intents | |
if intent == "CounterIntent": | |
return counter_intent(event, context) | |
if intent == "SingIntent": | |
return sing_intent(event, context) | |
if intent == "TripIntent": | |
return trip_intent(event, context) | |
# Required Intents | |
if intent == "AMAZON.CancelIntent": | |
return cancel_intent() | |
if intent == "AMAZON.HelpIntent": | |
return help_intent() | |
if intent == "AMAZON.StopIntent": | |
return stop_intent() | |
############################## | |
# Program Entry | |
############################## | |
def lambda_handler(event, context): | |
if event['request']['type'] == "LaunchRequest": | |
return on_launch(event, context) | |
elif event['request']['type'] == "IntentRequest": | |
return intent_router(event, context) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment