-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskill.py
79 lines (60 loc) · 2.31 KB
/
skill.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from cryptoprice import get_price
##############################
# 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
def statement_builder(title, body):
speechlet = {}
speechlet['outputSpeech'] = build_PlainSpeech(body)
speechlet['card'] = build_SimpleCard(title, body)
speechlet['shouldEndSession'] = True
return build_response(speechlet)
##############################
# Intent Definitions
##############################
def intent_router(event, context):
intent = event['request']['intent']['name']
# custom intent
if intent == "CryptoPriceIntent":
return price_intent(event, context)
# required intents
if intent == "AMAZON.CancelIntent":
return statement_builder("Cancel intent", "This is a cancellation")
if intent == "AMAZON.HelpIntent":
return statement_builder("Cancel intent", "This was cancelled")
if intent == "AMAZON.StopIntent":
return statement_builder("Stop intent", "You was stopped")
def price_intent(event, context):
slots = event['request']['intent']['slots']
token = slots['cryptocurrency'].get('value', None)
try:
price = get_price(token)
except KeyError as ke:
return statement_builder("Price intent", "invalid currency")
except Exception as e:
return statement_builder("Price intent", "something went wrong: {0}".format(e))
return statement_builder("Price intent", "The price of {0} is ${1}".format(token, price))
##############################
# Lambda Handler
##############################
def lambda_handler(event, context):
if event['request']['type'] == "LaunchRequest":
return statement_builder("Welcome to Crypto Price", "Welcome to crypto price! You can start by asking me for a crypto token's value")
elif event['request']['type'] == "IntentRequest":
return intent_router(event, context)