-
Notifications
You must be signed in to change notification settings - Fork 0
/
alexa-skill-creator.py
153 lines (114 loc) · 3.99 KB
/
alexa-skill-creator.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# coding=utf-8
# alexa-skill-creator
# By Aleksei Kuznetcov <aleksei.code@gmail.com>
#
# alexa-skill-creator
import logging
from datetime import datetime
from flask import Flask, json, render_template
from flask_ask import Ask, request, session, question, statement
__author__ = 'Aleksei Kuznetcov'
__email__ = 'aleksei.code@gmail.com'
app = Flask(__name__)
ask = Ask(app, '/')
logging.getLogger("flask_ask").setLevel(logging.DEBUG)
# Session starter
#
# This intent is fired automatically at the point of launch (= when the session starts).
# Use it to register a state machine for things you want to keep track of, such as what the last intent was, so as to be
# able to give contextual help.
@ask.on_session_started
def start_session():
"""
Fired at the start of the session, this is a great place to initialise state variables and the like.
"""
logging.debug("Session started at {}".format(datetime.now().isoformat()))
# Launch intent
#
# This intent is fired automatically at the point of launch.
# Use it as a way to introduce your Skill and say hello to the user. If you envisage your Skill to work using the
# one-shot paradigm (i.e. the invocation statement contains all the parameters that are required for returning the
# result
@ask.launch
def handle_launch():
"""
(QUESTION) Responds to the launch of the Skill with a welcome statement and a card.
Templates:
* Initial statement: 'welcome'
* Reprompt statement: 'welcome_re'
* Card title: 'alexa-skill-creator
* Card body: 'welcome_card'
"""
welcome_text = render_template('welcome')
welcome_re_text = render_template('welcome_re')
welcome_card_text = render_template('welcome_card')
return question(welcome_text).reprompt(welcome_re_text).standard_card(title="alexa-skill-creator",
text=welcome_card_text)
# Built-in intents
#
# These intents are built-in intents. Conveniently, built-in intents do not need you to define utterances, so you can
# use them straight out of the box. Depending on whether you wish to implement these in your application, you may keep
# or delete them/comment them out.
#
# More about built-in intents: http://d.pr/KKyx
@ask.intent('AMAZON.StopIntent')
def handle_stop():
"""
(STATEMENT) Handles the 'stop' built-in intention.
"""
farewell_text = render_template('stop_bye')
return statement(farewell_text)
@ask.intent('AMAZON.CancelIntent')
def handle_cancel():
"""
(STATEMENT) Handles the 'cancel' built-in intention.
"""
farewell_text = render_template('cancel_bye')
return statement(farewell_text)
@ask.intent('AMAZON.HelpIntent')
def handle_help():
"""
(QUESTION) Handles the 'help' built-in intention.
You can provide context-specific help here by rendering templates conditional on the help referrer.
"""
help_text = render_template('help_text')
return question(help_text)
@ask.intent('AMAZON.NoIntent')
def handle_no():
"""
(?) Handles the 'no' built-in intention.
"""
pass
@ask.intent('AMAZON.YesIntent')
def handle_yes():
"""
(?) Handles the 'yes' built-in intention.
"""
pass
@ask.intent('AMAZON.PreviousIntent')
def handle_back():
"""
(?) Handles the 'go back!' built-in intention.
"""
pass
@ask.intent('AMAZON.StartOverIntent')
def start_over():
"""
(QUESTION) Handles the 'start over!' built-in intention.
"""
pass
# Ending session
#
# This intention ends the session.
@ask.session_ended
def session_ended():
"""
Returns an empty for `session_ended`.
.. warning::
The status of this is somewhat controversial. The `official documentation`_ states that you cannot return a response
to ``SessionEndedRequest``. However, if it only returns a ``200/OK``, the quit utterance (which is a default test
utterance!) will return an error and the skill will not validate.
"""
return statement("")
if __name__ == '__main__':
app.run(debug=True)