-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda-function.py
More file actions
158 lines (125 loc) · 5.49 KB
/
lambda-function.py
File metadata and controls
158 lines (125 loc) · 5.49 KB
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
154
155
156
157
158
import json
import dateutil.parser
import datetime
import time
import os
import math
import random
import logging
from response_builders import elicit_slot, confirm_intent, close, delegate, build_response_card, build_validation_result
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
""" --- Helpers to build responses which match the structure of the necessary dialog actions --- """
""" --- VAlidatioan and action functions --- """
def validate_schedule_meeting(meeting_date, meeting_time, meeting_duration, participant, meeting_title):
#TODO
"""
if meeting_date <= yesterday:
ask for another date
if meeting_date >= 3 monthd ahead:
ask for another date
if meeting_date == today and meeting_time < current time:
ask for another date
check if time slot is free in calendar, return free timeslots (response card) that day
check participants is an employee
if meeting_title is not String:
ask for meeting title
"""
return build_validation_result(True, None, None)
def book_meeting(intent_request):
"""
Performs dialog management and fulfillment for scheduling a meeting.
"""
meeting_date = intent_request['currentIntent']['slots']['meetingDate']
meeting_time = intent_request['currentIntent']['slots']['meetingTime']
meeting_duration = intent_request['currentIntent']['slots']['meetingDuration']
participant = intent_request['currentIntent']['slots']['participant']
meeting_title = intent_request['currentIntent']['slots']['meetingTitle']
source = intent_request['invocationSource']
output_session_attributes = intent_request['sessionAttributes'] if intent_request['sessionAttributes'] is not None else {}
if source == 'DialogCodeHook':
# Perform basic validation on the supplied input slots.
slots = intent_request['currentIntent']['slots']
validation_result = validate_schedule_meeting(meeting_date, meeting_time, meeting_duration, participant, meeting_title)
if not validation_result['isValid']:
slots[validation_result['violatedSlot']] = None
return elicit_slot(
output_session_attributes,
intent_request['currentIntent']['name'],
slots,
validation_result['violatedSlot'],
validation_result['message']
)
if not meeting_date:
return elicit_slot(
output_session_attributes,
intent_request['currentIntent']['name'],
intent_request['currentIntent']['slots'],
'meetingDate',
{'contentType': 'PlainText', 'content': 'What date?'}
)
if meeting_date and not meeting_time:
return elicit_slot(
output_session_attributes,
intent_request['currentIntent']['name'],
intent_request['currentIntent']['slots'],
'meetingTime',
{'contentType': 'PlainText', 'content': 'What time?'}
)
if meeting_date and meeting_time and not meeting_duration:
return elicit_slot(
output_session_attributes,
intent_request['currentIntent']['name'],
intent_request['currentIntent']['slots'],
'meetingDuration',
{'contentType': 'PlainText', 'content': 'For how long?'}
)
if meeting_date and meeting_time and meeting_duration and not participant:
return elicit_slot(
output_session_attributes,
intent_request['currentIntent']['name'],
intent_request['currentIntent']['slots'],
'participant',
{'contentType': 'PlainText', 'content': 'Who with?'}
)
if meeting_date and meeting_time and participant and meeting_duration and not meeting_title:
return elicit_slot(
output_session_attributes,
intent_request['currentIntent']['name'],
intent_request['currentIntent']['slots'],
'meetingTitle',
{'contentType': 'PlainText', 'content': 'Meeting Title?'}
)
return delegate(output_session_attributes, slots)
logger.debug('Correct'
'We will book this for you')
return close(
output_session_attributes,
'Fulfilled',
{
'contentType': 'PlainText',
'content': 'Okay, I have scheduled your meeting. We will see you at {} on {}'.format(meeting_time, meeting_date)
}
)
""" --- Intents --- """
def dispatch(intent_request):
"""
Called when the user specifies an intent for this bot.
"""
logger.debug('dispatch userId={}, intentName={}'.format(intent_request['userId'], intent_request['currentIntent']['name']))
intent_name = intent_request['currentIntent']['name']
# Dispatch to your bot's intent handlers
if intent_name == 'BookMeeting':
return book_meeting(intent_request)
raise Exception('Intent with name ' + intent_name + ' not supported')
""" --- Main handler --- """
def lambda_handler(event, context):
"""
Route the incoming request based on intent.
The JSON body of the request is provided in the event slot.
"""
# By default, treat the user request as coming from the America/New_York time zone.
os.environ['TZ'] = 'America/New_York'
time.tzset()
logger.debug('event.bot.name={}'.format(event['bot']['name']))
return dispatch(event)