This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathslackard.py
executable file
·285 lines (234 loc) · 8.65 KB
/
slackard.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python
from __future__ import print_function
from glob import glob
import functools
import importlib
import os.path
import re
import slacker
import sys
import time
import yaml
class SlackardFatalError(Exception):
pass
class SlackardNonFatalError(Exception):
pass
class Config(object):
config = {}
def __init__(self, file_):
self.file = file_
f = open(file_, 'r')
y = yaml.load(f)
f.close()
self.__dict__.update(y)
class Slackard(object):
subscribers = []
commands = []
firehoses = []
def __init__(self, config_file):
self.config = Config(config_file)
self.apikey = self.config.slackard['apikey']
self.botname = self.config.slackard['botname']
self.botnick = self.config.slackard['botnick']
self.channel = self.config.slackard['channel']
self.plugins = self.config.slackard['plugins']
try:
self.boticon = self.config.slackard['boticon']
except:
self.boticon = None
try:
self.botemoji = ':{0}:'.format(self.config.slackard['botemoji'])
except:
self.botemoji = None
def __str__(self):
return 'I am a Slackard!'
def _import_plugins(self):
self._set_import_path()
plugin_prefix = os.path.split(self.plugins)[-1]
# Import the plugins submodule (however named) and set the
# bot object in it to self
importlib.import_module(plugin_prefix)
sys.modules[plugin_prefix].bot = self
for plugin in glob('{}/[!_]*.py'.format(self._get_plugin_path())):
module = '.'.join((plugin_prefix, os.path.split(plugin)[-1][:-3]))
try:
importlib.import_module(module)
except Exception as e:
print('Failed to import {0}: {1}'.format(module, e))
def _get_plugin_path(self):
path = self.plugins
cf = self.config.file
if path[0] != '/':
path = os.path.join(os.path.dirname(os.path.realpath(cf)), path)
return path
def _set_import_path(self):
path = self._get_plugin_path()
# Use the parent directory of plugin path
path = os.path.dirname(path)
if path not in sys.path:
sys.path = [path] + sys.path
def _init_connection(self):
self.slack = slacker.Slacker(self.apikey)
try:
r = self.slack.channels.list()
except slacker.Error as e:
if e.message == 'invalid_auth':
raise SlackardFatalError('Invalid API key')
raise
except Exception as e:
raise SlackardNonFatalError(e.message)
c_map = {c['name']: c['id'] for c in r.body['channels']}
self.chan_id = c_map[self.channel]
def _fetch_messages_since(self, oldest=None):
h = self.slack.channels.history(self.chan_id, oldest=oldest)
assert(h.successful)
messages = h.body['messages']
messages.reverse()
return [m for m in messages if m['ts'] != oldest]
def speak(self, message=None, paste=False, attachments=None):
if paste:
message = '```{0}```'.format(message)
self.slack.chat.post_message(self.chan_id, message,
username=self.botname,
icon_emoji=self.botemoji,
icon_url=self.boticon,
attachments=attachments)
def upload(self, file, filename=None, title=None):
if title is None:
title = ''
title = '{} (Upload by {})'.format(title, self.botname)
self.slack.files.upload(file, channels=self.chan_id,
filename=filename,
title=title)
def set_topic(self, topic):
self.slack.channels.set_topic(channel=self.chan_id, topic=topic)
def channel_info(self):
info = self.slack.channels.info(channel=self.chan_id)
return info.body['channel']
def run(self):
self._init_connection()
self._import_plugins()
cmd_matcher = re.compile('^{0}:\s*(\S+)\s*(.*)'.format(
self.botnick), re.IGNORECASE)
h = self.slack.channels.history(self.chan_id, count=1)
assert(h.successful)
t0 = time.time()
if len(h.body['messages']):
ts = h.body['messages'][0]['ts']
else:
ts = t0
while True:
t1 = time.time()
delta_t = t1 - t0
if delta_t < 5.0:
time.sleep(5.0 - delta_t)
t0 = time.time()
try:
messages = self._fetch_messages_since(ts)
except Exception as e:
# Possibly an error we can recover from so raise
# a non-fatal exception and attempt to recover
raise SlackardNonFatalError(e.message)
for message in messages:
ts = message['ts']
if 'text' in message:
# Skip actions on self-produced messages.
try:
if (message['subtype'] == 'bot_message' and
message['username'] == self.botname):
continue
except KeyError:
pass
print(message)
# Plugins receive full message object in all invocations
for f in self.firehoses:
f(message)
for (f, matcher) in self.subscribers:
if matcher.search(message['text']):
f(message)
m = cmd_matcher.match(message['text'])
if m:
cmd, args = m.groups()
for (f, command) in self.commands:
if command == cmd:
f(args, message)
def subscribe(self, pattern):
if hasattr(pattern, '__call__'):
raise TypeError('Must supply pattern string')
def real_subscribe(wrapped):
@functools.wraps(wrapped)
def _f(*args, **kwargs):
return wrapped(*args, **kwargs)
try:
matcher = re.compile(pattern, re.IGNORECASE)
self.subscribers.append((_f, matcher))
except:
print('Failed to compile matcher for {0}'.format(wrapped))
return _f
return real_subscribe
def command(self, command):
if hasattr(command, '__call__'):
raise TypeError('Must supply command string')
def real_command(wrapped):
@functools.wraps(wrapped)
def _f(*args, **kwargs):
return wrapped(*args, **kwargs)
self.commands.append((_f, command))
return _f
return real_command
def firehose(self, wrapped):
@functools.wraps(wrapped)
def _f(*args, **kwargs):
return wrapped(*args, **kwargs)
self.firehoses.append(_f)
return _f
def usage():
yaml_template = """
slackard:
apikey: my_api_key_from-api.slack.com
channel: random
botname: Slackard
botnick: slack # short form name for commands.
# Use either boticon or botemoji
boticon: http://i.imgur.com/IwtcgFm.png
botemoji: boom
# plugins directory relative to config file, or absolute
# create empty __init__.py in that directory
plugins: ./myplugins
"""
print('Usage: slackard <config.yaml>')
print('\nExample YAML\n{}'.format(yaml_template))
def main():
config_file = None
try:
config_file = sys.argv[1]
except IndexError:
pass
if config_file is None:
usage()
sys.exit(1)
if not os.path.isfile(config_file):
print('Config file "{}" not found.'.format(config_file))
sys.exit(1)
try:
bot = Slackard(config_file)
except Exception as e:
print('Encountered error: {}'.format(e.message))
sys.exit(1)
while True:
try:
bot.run()
except SlackardFatalError as e:
print('Fatal error: {}'.format(e.message))
sys.exit(1)
except SlackardNonFatalError as e:
print('Non-fatal error: {}'.format(e.message))
delay = 5
print('Delaying for {} seconds...'.format(delay))
time.sleep(delay)
bot._init_connection()
except Exception as e:
print('Unhandled exception: {}'.format(e.message))
sys.exit(1)
if __name__ == '__main__':
main()