-
Notifications
You must be signed in to change notification settings - Fork 0
/
trellobot.py
247 lines (185 loc) · 7.8 KB
/
trellobot.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python3
from config import *
import sys
import json
from datetime import datetime
import ipaddress
import logging
import requests
from flask import Flask, request, abort
logging.basicConfig()
logger = logging.getLogger(name="trellobot")
logger.setLevel(LOGGING_LEVEL)
logger.info("Main module has started.")
def say(message):
# Sending messages to telegram group by the bot
# Enable LOGGING_LEVEL='DEBUG' to view the message
message = message.replace(" ", "")
logger.info("Trying to send the message.")
logger.debug(message)
result = requests.post(
url=f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage",
data={
"chat_id": CHAT_ID,
"text": message,
"parse_mode": "HTML",
"disable_web_page_preview": "true",
},
timeout=30,
)
if result.ok:
logger.info("Message was successfully sent to telegram group.")
else:
logger.error("Could not send message to telegram.")
def create_card(action_data, member_name):
logger.info(f"Starting a processing function: {sys._getframe(0).f_code.co_name}.")
card_name = action_data["card"]["name"]
card_url = f"https://trello.com/c/{action_data['card']['shortLink']}"
list_name = action_data["list"]["name"]
say(
f"""🔥 {member_name} создал новую карточку!
Статус: <b>{list_name}</b>
<a href="{card_url}">{card_name}</a>"""
)
def rename_card(action_data, member_name):
logger.info(f"Starting a processing function: {sys._getframe(0).f_code.co_name}.")
card_name_old = action_data["old"]["name"]
card_name_new = action_data["card"]["name"]
card_url = f"https://trello.com/c/{action_data['card']['shortLink']}"
list_name = action_data["list"]["name"]
say(
f"""✏️ {member_name} переименовал карточку!
Статус: <b>{list_name}</b>
<i>{card_name_old}</i>
-= 🔽 🔽 🔽 =-
<a href="{card_url}">{card_name_new}</a>"""
)
def transfer_card(action_data, member_name):
logger.info(f"Starting a processing function: {sys._getframe(0).f_code.co_name}.")
card_name = action_data["card"]["name"]
card_url = f"https://trello.com/c/{action_data['card']['shortLink']}"
list_name_old = action_data["listBefore"]["name"]
list_name_new = action_data["listAfter"]["name"]
say(
f"""🔃 {member_name} поменял статус карточки!
Название: <a href="{card_url}">{card_name}</a>
<i>{list_name_old}</i> ➡️ ➡️ ➡️ <b>{list_name_new}</b>"""
)
def close_card(action_data, member_name):
logger.info(f"Starting a processing function: {sys._getframe(0).f_code.co_name}.")
card_name = action_data["card"]["name"]
list_name = action_data["list"]["name"]
say(
f"""🗑 {member_name} удалил карточку!
Название: <b>{card_name}</b>
Статус: <b>{list_name}</b>"""
)
def new_comment(action_data, member_name):
logger.info(f"Starting a processing function: {sys._getframe(0).f_code.co_name}.")
card_name = action_data["card"]["name"]
card_url = f"https://trello.com/c/{action_data['card']['shortLink']}"
list_name = action_data["list"]["name"]
comment_text = action_data["text"]
say(
f"""Название: <a href="{card_url}">{card_name}</a>
Статус: <b>{list_name}</b>
💬 Комментарий {member_name}: <i>{comment_text}</i>"""
)
def new_attachment(action_data, member_name):
logger.info(f"Starting a processing function: {sys._getframe(0).f_code.co_name}.")
card_name = action_data["card"]["name"]
card_url = f"https://trello.com/c/{action_data['card']['shortLink']}"
list_name = action_data["list"]["name"]
attachment_url = action_data["attachment"]["url"]
say(
f"""Название: <a href="{card_url}">{card_name}</a>
Статус: <b>{list_name}</b>
📎 <a href="{attachment_url}">Новое вложение</a> от {member_name}"""
)
def due_time(action_data, member_name):
logger.info(f"Starting a processing function: {sys._getframe(0).f_code.co_name}.")
card_name = action_data["card"]["name"]
card_url = f"https://trello.com/c/{action_data['card']['shortLink']}"
list_name = action_data["list"]["name"]
due = datetime.fromisoformat(action_data["card"]["due"])
say(
f"""Название: <a href="{card_url}">{card_name}</a>
Статус: <b>{list_name}</b>
⏰ {member_name} выставил дедлайн: <i>{due.day}/{due.month}/{due.year} в {due.hour}:{due.minute}</i>"""
)
def new_desc(action_data, member_name):
logger.info(f"Starting a processing function: {sys._getframe(0).f_code.co_name}.")
card_name = action_data["card"]["name"]
card_url = f"https://trello.com/c/{action_data['card']['shortLink']}"
list_name = action_data["list"]["name"]
desc = action_data["card"]["desc"]
say(
f"""Название: <a href="{card_url}">{card_name}</a>
Статус: <b>{list_name}</b>
📝 {member_name} добавил описание: <i>{desc}</i>"""
)
# processing json of an action on Trello board and calling appropriate function
def handler(action):
action_type = action["type"]
action_data = action["data"]
member_name = action["memberCreator"]["fullName"]
logger.info("Action handler has started.")
logger.debug(f"Action: {action_type}")
logger.debug(f"Member name: {member_name}")
logger.debug(json.dumps(action_data, indent=2))
if "old" not in action_data:
action["data"]["old"] = {}
if "card" not in action_data:
logger.info("Received not a card action. Passing.")
pass
elif action_type == "commentCard":
new_comment(action_data, member_name)
elif action_type == "addAttachmentToCard":
new_attachment(action_data, member_name)
elif "listBefore" in action_data and "listAfter" in action_data:
transfer_card(action_data, member_name)
elif action_type == "updateCard" and "name" in action_data["old"]:
rename_card(action_data, member_name)
elif action_type == "createCard":
create_card(action_data, member_name)
elif "closed" in action_data["old"] and action_data["card"]["closed"]:
close_card(action_data, member_name)
elif "due" in action_data["card"]:
try:
due_time(action_data, member_name)
except TypeError:
logger.exception("Invalid due time. Passed without sending message.")
pass
elif "desc" in action_data["card"]:
new_desc(action_data, member_name)
else:
logger.warning(
f"Unknown action on Trello. Action type: {action_type}. To view json data set LOGGING_LEVEL='DEBUG'."
)
# initialising Flask server to listen to Trello webhook's requests
app = Flask(__name__)
@app.route("/", methods=["POST", "HEAD"])
def webhook():
if ipaddress.ip_address(request.remote_addr) not in ipaddress.ip_network(
# allow only Trello ips
# https://developer.atlassian.com/cloud/trello/guides/rest-api/webhooks/#webhook-sources
"104.192.142.240/28"
):
logger.warning(
f"New request from not white-listed ip: {request.remote_addr}. Aborted."
)
abort(403)
if request.method == "HEAD":
# needed for inital response when webhook is created
logger.info("Received HEAD request. Returning code 200.")
return {}, 200
if request.method == "POST":
logger.info(f"Received new action from Webhook. Passing to handler.")
handler(request.json["action"])
return {}, 200
logger.info("Starting Flask server!")
try:
app.run(host="0.0.0.0", port=PORT)
except Exception:
logger.exception("FATAL. Caught exception with server.")
sys.exit(1)