This repository has been archived by the owner on Aug 11, 2022. It is now read-only.
generated from anoadragon453/nio-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_responder.py
79 lines (65 loc) · 2.51 KB
/
message_responder.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
import re
import logging
from errors import BotException
from chat_functions import send_text_to_room
from github import Github
logger = logging.getLogger(__name__)
class Message(object):
def __init__(self, client, store, config, message_content, room, event):
"""Initialize a new Message
Args:
client:
store:
config:
message_content:
room:
event:
"""
self.client = client
self.store = store
self.config = config
self.message_content = message_content
self.room = room
self.event = event
# MSC#### regex
self.msc_number_regex = re.compile(r"(^| |\()MSC(\d+)", flags=re.IGNORECASE)
# Initialize Github object
self.github = Github(self.config.repo_slug)
async def process(self):
"""Process and possibly respond to the message"""
# Link all mentioned MSCs
await self._link_mscs()
async def _link_mscs(self):
"""Post a link to any mentioned MSCs"""
links = []
for match in self.msc_number_regex.finditer(self.message_content):
# Extract issue number from MSC ID
logger.debug("Got match: %s", match.group(2))
msc_num = int(match.group(2))
try:
metadata = self.github.get_info_for_issue_pr(msc_num)
except FileNotFoundError:
# Unknown MSC, no match
return
except PermissionError:
# Forbidden from accessing the API
return
except BotException as e:
await send_text_to_room(self.client, self.room.room_id, f"Error: {e.msg}")
return
keys = ['title', 'user', 'html_url']
for key in keys:
if key not in metadata:
logger.error("MSC metadata does not contain required key '%s': %s", key, metadata)
await send_text_to_room(self.client, self.room, "Error (missing metadata)")
return
link = (
f"[{metadata['title']}]({metadata['html_url']})"
f" by [{metadata['user']['login']}]({metadata['user']['html_url']})"
)
if link not in links:
links.append(link)
if links:
response_content = "\n\n".join(links)
await send_text_to_room(self.client, self.room.room_id, response_content,
notice=True)