-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathapp.py
More file actions
149 lines (124 loc) · 4.97 KB
/
Copy pathapp.py
File metadata and controls
149 lines (124 loc) · 4.97 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
# -*- coding: utf-8 -*-
import os
import json
import requests
from flask import Flask, render_template, redirect, request, Response
app = Flask(__name__)
def validate(params):
if params["event"]["type"] != "message":
print("Event type is ", params["event"]["type"], "but not `message`")
app_id = params["api_app_id"] == os.environ["APP_ID"]
token = params["token"] == os.environ["VERIFICATION_TOKEN"]
team = params["team_id"] == os.environ["TEAM_ID"]
channel = params["event"]["channel"] == os.environ["USLACKBOT_CHANNEL"]
user = params["event"].get("user", "") == "USLACKBOT"
subtype = params["event"]["subtype"] == "file_share"
if app_id and token and team and channel and user and subtype:
return True
else:
if not app_id:
print("env: APP_ID is not right!")
if not token:
print("env: TOKEN is not right!")
if not team:
print("env: TEAM_ID is not right!")
if not channel:
print("env: USLACKBOT channel is not right!")
if not user:
print("User is not right! user: ", params["event"].get("user", ""))
if not subtype:
print("Email subtype not right subtype: ",
params["event"]["subtype"])
return False
@app.route("/", methods=['GET', 'POST'])
def main():
if request.method == "GET":
return redirect("https://github.com/kossiitkgp/email-to-slack")
elif request.method == "POST":
print("New Email recieved\n Parameters")
params = request.get_json(force=True)
print(json.dumps(params))
print("\n\n===HEADERS====\n")
print(request.headers)
"""
Enable this to verify the URL while installing the app
"""
if 'challenge' in params:
data = {
'challenge': params.get('challenge'),
}
resp = Response(
response=json.dumps(data),
status=200,
mimetype='application/json'
)
resp.headers['Content-type'] = 'application/json'
return resp
if validate(params):
email = params["event"]["files"][0]
if f"CHECKED_{email['id']}" in os.environ or "X-Slack-Retry-Num" in request.headers:
# This email has already been processed
return Response(response="Duplicate", status=409)
email_provider = "http://gmail.com/"
sender_email = email["from"][0]["original"]
email_subject = email["title"]
email_content = "```" + email["plain_text"] + "```"
timestamp = email["timestamp"]
all_to = ', '.join([i["original"] for i in email["to"]])
all_cc = ', '.join([i["original"] for i in email["cc"]])
data = {
"text": "",
"attachments": [
{
"fallback": "An email was sent by " + sender_email,
"color": "#2196F3",
"pretext": "",
"author_name": sender_email,
"author_link": email_provider,
"author_icon": "",
"title": email_subject,
"title_link": email_provider,
"text": email_content,
"fields": [],
"footer": "Sent to : " + all_to,
"footer_icon": "",
"ts": timestamp
}
]
}
if all_cc:
data["attachments"][0]["fields"].append({
"title": "cc",
"value": all_cc
})
if email["attachments"]:
data["attachments"][0]["fields"].append({
"title": "",
"value": "This email also has attachments",
"short": False
})
INCOMING_WEBHOOK_URL = os.environ["INCOMING_WEBHOOK_URL"]
headers = {
"Content-type": "application/json"
}
print("Sending the following data to ", INCOMING_WEBHOOK_URL)
print("\n\n\n", data, "\n\n\n")
r = requests.post(INCOMING_WEBHOOK_URL, headers=headers, json=data)
print("\n\n\n Exit with status code {}\n\n".format(r.status_code))
# Slack API sends two payloads for single event. This is a bug
# involving Heroku and Slack API.
os.environ[f"CHECKED_{email['id']}"] = ''
return Response(
response="ok",
status=200
)
else:
return Response(
response="Bad request",
status=401
)
app.secret_key = os.environ.setdefault("APP_SECRET_KEY", "notsosecret")
app.config['SESSION_TYPE'] = 'filesystem'
app.debug = False
if __name__ == '__main__':
app.run(debug=True)