forked from JohnRoux/slack-autoresponder
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
124 lines (106 loc) · 3.36 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
#!/usr/bin/env python
import json
import logging
import os
import threading
import time
from collections import defaultdict
from datetime import datetime
from random import choice
from time import sleep
import slack
from requests import post
from slack import RTMClient, WebClient
HITS = [
'guyz',
'hey guys',
'hi guys',
'my guys',
'thanks guys',
'the guys',
'these guys',
'those guys',
'you guys',
]
BASE_RESPONSE = 'Some people in the community find "guys" alienating, next time would you consider _{}_? :slightly_smiling_face: (<http://bit.ly/2uJCn3y|Learn more>)'
RESPONSES = [
'all',
'everyone',
'folks',
'y\'all',
]
@slack.RTMClient.run_on(event='message')
def process_message_event(**payload):
event = payload['data']
web_client = payload['web_client']
try:
if 'text' not in event:
return
channel = event['channel']
text = event['text']
if 'user' not in event:
return
user = event['user']
if any(hit in text.lower() for hit in HITS):
web_client.chat_postEphemeral(
channel=channel,
text=BASE_RESPONSE.format(choice(RESPONSES)),
user=user,
as_user='true',
)
global metrics
if metrics is not None:
global channels
channel = channels.get(channel)
global users
user = users.get(user)
if user and channel:
metrics['{}-{}'.format(channel, user)] += 1
except Exception as exc:
logging.error('Exception while processing message', exc, event)
def run_bot():
token = os.environ.get('SLACKBOT_TOKEN')
report_url = os.environ.get('SLACKBOT_REPORT_URL')
rtm_client = RTMClient(token=token)
web_client = WebClient(token=token)
global metrics
metrics = None
global channels
channels = {u['id']: u['name'] for u in web_client.channels_list()['channels']}
global users
users = {u['id']: u['name'] for u in web_client.users_list()['members']}
if report_url:
# Reports are enabled, so start reporting thread
global last_run
last_run = None
metrics = defaultdict(int)
class ReportingThread(threading.Thread):
def run(self):
while True:
curr_min = datetime.utcnow().minute
global last_run
global metrics
if last_run == curr_min:
sleep(5)
continue
last_run = curr_min
if not metrics:
continue
try:
metrics = json.dumps(metrics)
resp = post(
url=report_url,
json=dict(text=metrics))
resp.raise_for_status()
except Exception as exc:
print(exc)
metrics = defaultdict(int)
reporting = ReportingThread(name='Reporting Thread')
reporting.start()
while True:
try:
rtm_client.start()
except Exception as exc:
logging.error('Exception during rtm_client.start', exc)
if __name__ == '__main__':
run_bot()