-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.py
More file actions
65 lines (50 loc) · 1.86 KB
/
bot.py
File metadata and controls
65 lines (50 loc) · 1.86 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
import time
from slackclient import SlackClient
from settings import token
sc = SlackClient(token)
team_join_event = 'team_join'
admin_dm_channels = []
with open('template.txt') as f:
template = f.read()
def _get_admin_ids():
users = sc.api_call('users.list')
return [u['id'] for u in users['members'] if u.get('is_admin') is True]
def get_admin_dm_channels():
global admin_dm_channels
for admin_id in _get_admin_ids():
response = sc.api_call('im.open', user=admin_id)
admin_dm_channels.append(response['channel']['id'])
return admin_dm_channels
def notify_admin(username):
for dm_channel_id in admin_dm_channels:
m = 'User: {} successfully on-boarded'.format(username)
sc.rtm_send_message(channel=dm_channel_id, message=m)
def send_welcome_message(user):
user_id = user['id']
slack_name = user['name']
real_name = user['real_name']
response = sc.api_call('im.open', user=user_id)
try:
dm_channel_id = response['channel']['id']
except (KeyError, ValueError):
print("Shite happened: {}".format(slack_name))
return
message = template.format(real_name=real_name, slack_name=slack_name)
sc.rtm_send_message(channel=dm_channel_id, message=message)
notify_admin(username=user['name'])
print("Sent onboarding message to {}".format(slack_name))
def main():
if sc.rtm_connect():
print('---Connected to Slack successfully---')
while True:
for event in sc.rtm_read():
if event.get('type') == team_join_event and (
event['user']['is_bot'] is False):
send_welcome_message(user=event['user'])
# imma sleep for a while
time.sleep(1)
else:
print('Connection Failed, invalid token?')
if __name__ == '__main__':
get_admin_dm_channels()
main()