-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathslack_bot.py
More file actions
134 lines (111 loc) · 4.07 KB
/
slack_bot.py
File metadata and controls
134 lines (111 loc) · 4.07 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
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
import logging
import os
import sys
import time
import traceback
from subprocess import Popen, PIPE
# pip install slackclient
from slackclient import SlackClient
SLACK_BOT_NAME = 'prometheus'
SLACK_CHANNEL = 'test'
SLACKBOT_TOKEN = os.environ.get('SLACKBOT_TOKEN')
ADMIN_MODE = False
SHELL_DIR = "/home/ec2-user"
#retrieve SLACK_BOT_ID and SLACK_BOT_MENTION as this is unqiue per team
slack_client = SlackClient(SLACKBOT_TOKEN)
api_call = slack_client.api_call("users.list")
if api_call.get('ok'):
# retrieve all users so we can find our bot
users = api_call.get('members')
for user in users:
if 'name' in user and user.get('name') == SLACK_BOT_NAME.lower():
SLACK_BOT_ID = user.get('id')
SLACK_BOT_MENTION = '<@%s>' % SLACK_BOT_ID
else:
print("api-call failed for token " + SLACKBOT_TOKEN)
#begin slackbot helper functions
def send_message(text):
slack_client.rtm_send_message(channel=SLACK_CHANNEL, message=text)
def process_terminal_cmd(cmd):
if cmd.startswith('deactivate admin'):
global ADMIN_MODE
ADMIN_MODE = False
send_message("*!!!ADMIN MODE DEACTIVATED!!!*")
else:
cmd = cmd.replace('<','').replace('>','')
cmd = cmd.split(' ')
send_message("_Executing.._\n_Output:_")
with Popen(cmd, stdout=PIPE, stderr=PIPE, cwd=SHELL_DIR) as p:
for line in p.stdout:
send_message("{}".format(line.decode()))
for line in p.stderr:
send_message("{}".format(line.decode()))
def process_deploy():
exit()
def process_help(*args):
pass
def process_event(event):
#import global admin state and bot name
global ADMIN_MODE
# filter out slack events that are not for us
text = event.get('text')
if text is None:
return
# stop einstein from replying to itself
user=event.get('user')
if user == SLACK_BOT_NAME.lower():
return
# make sure our bot is only called for a specified channel
channel = event.get('channel')
if channel is None:
return
if channel != slack_client.server.channels.find(SLACK_CHANNEL).id:
slack_client.rtm_send_message(channel = channel, message ='<@{user}> I only run tasks asked from `{channel}` channel'.format(user,
channel=SLACK_CHANNEL))
return
# remove bot name and extract command
if text.startswith(SLACK_BOT_MENTION):
cmd = text.split('%s' % SLACK_BOT_MENTION)[1]
cmd = cmd.strip()
else:
cmd = text.strip()
# process command
try:
if ADMIN_MODE:
process_terminal_cmd(cmd)
elif cmd.startswith('activate admin'):
ADMIN_MODE = True
send_message('*!!!ADMIN MODE ACTIVATED!!!*')
elif cmd.startswith('goodbye'):
process_deploy()
else:
send_message("*I don't know how to do that*: `%s`" % cmd)
except:
send_message("*Exception thrown while executing*: `%s`" % sys.exc_info()[1])
def process_events(events):
for event in events:
try:
process_event(event)
except Exception as e:
logging.exception(e)
msg = '%s: %s\n%s' % (e.__class__.__name__, e, traceback.format_exc())
send_message(msg)
def main():
if slack_client.rtm_connect():
send_message('_starting..._')
# --
# Here is a good place to init git repositories if needed, in order to provide git-based features:
# - list of commits to deploy
# - history of deployments
# - status of deployed services vs what's available in git
send_message("*All right, I'm ready, ask me anything!*")
while True:
events = slack_client.rtm_read()
if events:
logging.info(events)
process_events(events)
time.sleep(0.1)
else:
logging.error('Connection Failed, invalid token?')
if __name__ == '__main__':
main()