-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsms.py
More file actions
61 lines (41 loc) · 1.5 KB
/
sms.py
File metadata and controls
61 lines (41 loc) · 1.5 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
from datetime import datetime, timedelta
from urllib.parse import parse_qsl
import unittest
from pytz import timezone
from utils import get_now, parse_minutes, parse_start_end_times, set_auto_buzz_config, add_auto_buzz_time, get_auto_buzz_times
from exceptions import BuzzerException, NotANumberException
def format_time(t):
return t.strftime('%-I %p')
def handle_sms(text):
now = get_now()
try:
try:
minutes = parse_minutes(text)
add_auto_buzz_time(now, now + timedelta(minutes=minutes))
set_auto_buzz_config(minutes)
message = f'Door will be open for the next {minutes} minutes'
except NotANumberException:
start, end = parse_start_end_times(text, now)
add_auto_buzz_time(start, end)
message = f'Door will be open between {format_time(start)} and {format_time(end)}'
except BuzzerException as ex:
message = str(ex)
return message
def lambda_handler(event, context):
params = dict(parse_qsl(event['body']))
text = params['Body']
message = handle_sms(text)
body = '<Response>'
body += '<Message>' + message + '</Message>'
body += '</Response>'
response = {
'statusCode': 200,
'headers': {'Content-Type': 'text/xml'},
'body': body
}
return response
class HandleSMSTestCase(unittest.TestCase):
def test_(self):
print(handle_sms('6-6'))
# print(handle_sms('10'))
print(get_auto_buzz_times())