-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers.py
More file actions
66 lines (55 loc) · 2.02 KB
/
Copy pathhandlers.py
File metadata and controls
66 lines (55 loc) · 2.02 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
import yaml
import re
import requests
import time
from structures import Message
with open('config/config.yml', 'r') as configfile:
config = yaml.safe_load(configfile)
with open(config['github_token'], 'r') as tokenfile:
token = tokenfile.read().strip()
AUTH_HEADER = {'Authorization': f'token {token}'}
GH_ENDPOINT = 'https://api.github.com/'
RATELIMIT = 60*60*24 # 24 hours
ratelimits = {}
def on_message(client, message: Message):
if f'<@{client.client_id}>' not in message.text: return
if message.user == client.client_id: return
content = message.text.strip()
match = re.match(
f"<@{client.client_id}> add ([^\s]+) to(?: the)? github.*",
content
)
if match:
if message.user in ratelimits and \
ratelimits[message.user] + RATELIMIT > time.time():
client.api_call(
'chat.postMessage',
channel=message.channel,
text="To prevent abuse, you may not run this command frequently."
)
account = match.group(1)
response = requests.put(
GH_ENDPOINT + f'orgs/UofT-group-sideprojects/memberships/{account}',
headers=AUTH_HEADER
)
payload = response.json()
if response.status_code != 200:
print(payload)
answer = "Sorry, I can't do that D:"
elif payload['state'] == 'pending':
answer = f"An invite has been sent to {account} Check your email :)"
ratelimits[message.user] = time.time()
else:
answer = f"{account} has already been invited"
client.api_call(
'chat.postMessage',
channel=message.channel,
text=answer
)
elif content.startswith(f"<@{client.client_id}> help"):
client.api_call(
'chat.postMessage',
channel=message.channel,
text=f'Send "<@{client.client_id}> add <your github handle> to github" ' \
'to invite yourself to the group project organization'
)