-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
75 lines (60 loc) · 2.12 KB
/
helpers.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
"""Helper functions to be used app wide."""
import logging, sys
from HTMLParser import HTMLParser
import settings
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)
class MLStripper(HTMLParser):
def __init__(self):
self.reset()
self.fed = []
def handle_data(self, d):
self.fed.append(d)
def get_data(self):
return ''.join(self.fed)
def strip_tags(html):
s = MLStripper()
s.feed(html)
return s.get_data()
def function_log(function_name, request, session):
logging.info("{function_name} requestId={request_id}, sessionId={session_id}".format(
function_name=function_name,
request_id=request.get('requestId', None),
session_id=session.get('sessionId', None)
))
class AppCheck(object):
def __init__(self, function):
self.function = function
def __call__(self, request, session):
self.incoming_app_id = session['application']['applicationId']
# Prevent someone else from configuring a skill that sends requests to this
if not self.check_correct_app_id(self.incoming_app_id):
raise ValueError("Invalid Application ID {app_id}".format(app_id=self.incoming_app_id))
return self.function(request, session)
def check_correct_app_id(self, id):
"""Ensure the incoming id matches our app id"""
return self.incoming_app_id == settings.APP_ID
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
}