-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmanager.py
More file actions
105 lines (92 loc) · 3.47 KB
/
manager.py
File metadata and controls
105 lines (92 loc) · 3.47 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
# coding: utf-8
from __future__ import unicode_literals
import os
import sys
import gevent
import logging
from gevent.pool import Pool
from gevent.monkey import patch_all
from importlib import import_module
from slackclient import SlackClient
from settings import BOT_NAME, ICON_URL, SLACK_TOKEN_MGR
patch_all()
from subprocess import check_output
pool = Pool(20)
CMD_PREFIX = ''
logger = logging.getLogger()
class Robot(object):
def __init__(self):
self.client = SlackClient(SLACK_TOKEN_MGR)
self.apps, self.docs = self.load_apps()
def load_apps(self):
docs = ['']
apps = {}
app = import_module('apps.system')
for command in app.run.commands:
apps[command] = app
return apps, docs
def handle_messages(self, messages):
for channel, text, user in messages:
command, payloads = self.extract_command(text)
if not command:
continue
app = self.apps.get(command, None)
if not app:
continue
arguments = (self, channel, payloads, user, command)
pool.apply_async(func=app.run, args=arguments)
def extract_messages(self, events):
messages = []
for e in events:
user = e.get('user', '')
channel = e.get('channel', '')
text = e.get('text', '')
if channel and text:
messages.append((channel, text, user))
return messages
def extract_command(self, text):
tokens = text.split(' ', 1)
if 1 < len(tokens):
return tokens[0], tokens[1]
else:
return (text, '')
def run(self):
if self.client.rtm_connect():
debug_chn = '#bot'
if os.path.isfile('booting_mgr'):
file = open('booting_mgr','r')
reboot_chn = file.readline()
self.client.api_call('chat.postMessage',username=BOT_NAME+' 매니저', as_user='false',icon_url=ICON_URL,channel=reboot_chn,text='재시작 완료')
os.remove('booting_mgr')
log_file = check_output(['ls', '-c', './log']).split('\n')[0]
log_file = open('./log/'+log_file,'r')
is_debug = False
while True:
if os.path.isfile('DEBUG'):
log_file.close()
log_file = check_output(['ls', '-c', './log']).split('\n')[0]
log_file = open('./log/'+log_file,'r')
dbg_file = open('DEBUG','r')
dbg_chn = dbg_file.readline()
dbg_file.close()
is_debug = True
os.remove('DEBUG')
elif os.path.isfile('DEBUG_'):
os.remove('DEBUG_')
is_debug = False
if is_debug:
txt = ''
txt = line = log_file.readline()
while line:
line = log_file.readline()
txt += line
if txt:
self.client.api_call('chat.postMessage',username=BOT_NAME+' 매니저(Debug)', as_user='false',icon_url=ICON_URL,channel=dbg_chn,text=txt)
events = self.client.rtm_read()
if events:
messages = self.extract_messages(events)
self.handle_messages(messages)
gevent.sleep(0.3)
if '__main__' == __name__:
robot = Robot()
robot.run()