-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
184 lines (149 loc) · 4.57 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import os
import json
import boto3
import requests
import io
import re
from cgi import FieldStorage
from jinja2 import Environment, FileSystemLoader
from datetime import datetime, timedelta
BUCKET_NAME = os.environ.get("BUCKET_NAME")
WEBHOOK_URL = os.environ.get("WEBHOOK_URL")
BASEPATH = os.environ.get("BASEPATH")
BLACKLIST = [
r'^.*\.ru>$',
]
def lambda_handler(event, context):
'''
lambda_handler
'''
print('event object in JSON: ')
print(json.dumps(event))
email = parse_multipart_form(event['headers'], event['body'])
if match_blacklist(email['from']) is True:
return {
'statusCode': 403,
'body': json.dumps('Filtered as spam!')
}
html_text = create_html(email)
s3_filepath = upload_to_s3(html_text)
slack_notifier(email, s3_filepath)
return {
'statusCode': 201,
'body': json.dumps('Success!')
}
def match_blacklist(frmmail):
flag = False
for banned in BLACKLIST:
pattern = re.compile(banned)
if pattern.match(frmmail) is not None:
flag = True
return flag
def parse_multipart_form(headers, body):
'''
Content-Type: multipart/form-data のコンテンツをパースする
'''
fp = io.BytesIO(body.encode('utf-8'))
environ = {'REQUEST_METHOD': 'POST'}
headers = {
'content-type': headers['Content-Type'],
'content-length': len(body.encode('utf-8'))
}
fs = FieldStorage(fp=fp, environ=environ, headers=headers)
email = {}
for f in fs.list:
# print(
# "---\nname: ", f.name,
# "\nfilename: ", f.filename,
# "\ntype: ", f.type,
# "\nvalue: ", f.value,
# "\n"
# )
email[f.name] = f.value
return email
def create_html(email):
'''
構造化された email オブジェクトを使って、 閲覧用 HTML を生成する
'''
# specify jinja2 template file
env = Environment(loader=FileSystemLoader('./', encoding='utf8'))
template = env.get_template('template_email_html.j2')
# render with given data
html_text = template.render({'email': email})
return html_text
def upload_to_s3(message):
'''
html に変換されたメッセージを S3 bucket に保存する
'''
s3 = boto3.client('s3')
directory = (datetime.now() + timedelta(microseconds=0)).isoformat()
filepath = '{dir}/index.html'.format(dir=directory)
response = s3.put_object(
Body=message,
Bucket=BUCKET_NAME,
Key=filepath,
ContentType='text/html'
)
print('s3_upload uploaded to: {dir}'.format(dir=directory))
print('s3_upload response in JSON: ')
print(json.dumps(response))
return filepath
def slack_notifier(email, s3_filepath):
'''
受信したメールのサマリを slack に通知する
'''
print('slack_notifier input: ')
print(json.dumps(email))
# message url uploaded to S3
message_url = BASEPATH + '/' + s3_filepath
# time the email has been sent
# _t1 = email['headers'].split('\n')
# _t2 = _t1[0].split(', ')
# time = _t2[1]
# email body
body = ''
if 'text' in email:
body = email['text']
elif 'html' in email:
body = '(Check the HTML message at {url} )'.format(url=message_url)
# compose incoming webhook POST body
request_body = {
"text": "email to {to}: {url}".format(to=email['to'], url=message_url),
"attachments": [
{
"title": "{subject}".format(subject=email['subject']),
"fields": [
# {
# "title": "Time",
# "value": time,
# "short": True
# },
{
"title": "From",
"value": email['from'],
"short": True
},
{
"title": "To",
"value": email['to'],
"short": True
},
{
"title": "Body",
"value": body,
"short": False
}
]
}
]
}
# make POST request
_r = requests.post(WEBHOOK_URL, data=json.dumps(request_body))
print('slack_notifier response in JSON: ')
response = {
'status_code': _r.status_code,
'headers': dict(_r.headers),
'body': _r.text
}
print(json.dumps(response))
return