-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
146 lines (112 loc) · 3.79 KB
/
main.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
import base64
import email
import json
import logging as log
import os
import re
from email.utils import parsedate_to_datetime
from imaplib import IMAP4_SSL
import slack
def send_messages_to_slack(msg):
log.basicConfig(level=log.DEBUG)
# log message
log.debug(msg)
category = msg["category"]
if "body" not in msg:
log.info("ignoring message without body key")
return
body = msg["body"]
_from = ""
if "from" in msg:
_from = msg["from"]
subject = ""
if "subject" in msg:
subject = msg["subject"]
# convert category map
SLACK_CATEGORY_MAP = os.environ["SLACK_CATEGORY_MAP"]
category_map = json.loads(base64.b64decode(SLACK_CATEGORY_MAP))
if category not in category_map:
log.info("ignoring not routed category {}".format(category))
return
channels = category_map[msg["category"]]
slack_message = ""
if _from != "":
slack_message += "*De:* {}".format(_from)
if subject != "":
slack_message += "\n*Assunto:* {}".format(subject)
slack_message += "\n```{}```".format(body)
SLACK_API_TOKEN = os.environ["SLACK_API_TOKEN"]
client = slack.WebClient(token=SLACK_API_TOKEN)
for channel in channels:
response = client.chat_postMessage(
icon_emoji=":capivara:",
channel=channel,
text=slack_message)
assert response["ok"]
def run(*args):
log.basicConfig(level=log.DEBUG)
log.debug(args)
# setup client
SERVER = os.environ["IMAP_SERVER"]
PORT = os.environ["IMAP_PORT"]
USER = os.environ["IMAP_USER"]
PASSWORD = os.environ["IMAP_PASSWORD"]
# connect to server
server = IMAP4_SSL(SERVER, port=PORT)
log.info("connected to the server")
# login
server.login(USER, PASSWORD)
log.info("authenticated")
# set SELECT state
server.select()
log.info("set to SELECTED state")
# list items on server
typ, data = server.search(None, '(UNSEEN)')
log.info("got list of emails")
messages_to_send = []
split = data[0].split()
for num in split:
log.info("processing message {} from {}".format(num.decode("utf-8"), len(split)))
_, data = server.fetch(num, '(RFC822)')
message = email.message_from_bytes(data[0][1])
date = parsedate_to_datetime(message.get("Date"))
# body
body = None
if message.is_multipart():
msg = message.get_payload()
while type(msg) == list:
temp = msg[0].get_payload(decode=True)
if temp is None:
msg = msg[0].get_payload()
else:
msg = temp
body = msg.decode("utf-8", "ignore")
else:
body = message.get_payload(decode=True).decode("utf-8")
# if successful extract body
if body is not None and body != "":
clean_html = re.compile('<.*?>|&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-f]{1,6});')
body = re.sub(clean_html, '', body)
email_message = {
"id": num,
"message_id": message.get("Message-Id"),
"from": message.get("From"),
"subject": message.get("Subject"),
"date": date,
"body": body,
"category": "misc",
}
# category
if "vaga" in email_message["subject"].lower():
email_message["category"] = "job"
messages_to_send.append(email_message)
if len(messages_to_send) == 0:
log.info("no new message")
return
for email_message in messages_to_send:
send_messages_to_slack(email_message)
server.uid('STORE', num, '+FLAGS', '(\Seen)')
server.close()
server.logout()
if __name__ == "__main__":
run()