-
Notifications
You must be signed in to change notification settings - Fork 1
/
lambda_function.py
59 lines (48 loc) · 1.98 KB
/
lambda_function.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
import csv
import re
from typing import List
def lambda_handler(event, context):
to_number = event['To']
from_number = event['From']
body = event['Body']
body = body.strip('+')
body = re.sub('[!@#$.]', '', body)
if not to_number:
return "The function needs a 'To' number"
elif not from_number:
return "The function needs a 'From' number"
elif not body:
return "The function needs a 'Body' message to send."
message = body.split('+')
for row in getData('data.csv'):
for word in message:
word = word.lower()
if word in row['Keywords']:
return rowtomessage(row)
return "insufficient info"
def notblank(value: str):
return value == ''
def rowtomessage(row: List[str]):
#used to keep track of whether both phone num and office info are available
firstClauseComplete = False
message = 'The ' + row['Resource']
if row['Phone Number'] != '':
message = message + ' phone number is ' + row['Phone Number']
firstClauseComplete = True
if row['Office Hours'] != '' and row['Office Location'] != '':
#if the phone number was already present, the office loc is the second part of the sentence.
if firstClauseComplete:
message = message + ' , and it '
message = message + 'is open ' + row['Office Hours'] + ' in the ' + row['Office Location'] + '.'
message = message + row['Pertinent Info']
if row['Website'] != '':
message = message + '\nWebsite: ' + row['Website']
if row['Email'] != '':
message = message + '\nEmail: ' + row['Email']
return message
def getData(filename: str):
with open(filename, newline='') as data:
cols = ['Resource', 'Website', 'Email', 'Phone Number', 'Office Location', 'Office Hours', 'Pertinent Info']
datareader = csv.DictReader(data, restkey='Keywords', fieldnames=cols, delimiter=',')
for row in datareader:
yield row