Skip to content

Commit

Permalink
added default_reply() as a plugin capability
Browse files Browse the repository at this point in the history
  • Loading branch information
David Benjamin committed May 15, 2016
1 parent 0781f87 commit 26abfb8
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
11 changes: 11 additions & 0 deletions slackbot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,14 @@ def wrapper(func):
return func

return wrapper


def default_reply(matchstr=r'^.*$', flags=0):
def wrapper(func):
PluginsManager.commands['default_reply'][
re.compile(matchstr, flags)] = func
logger.info('registered default_reply plugin "%s" to "%s"', func.__name__,
matchstr)
return func

return wrapper
17 changes: 10 additions & 7 deletions slackbot/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,24 @@ def start(self):
def dispatch_msg(self, msg):
category = msg[0]
msg = msg[1]
text = msg['text']
if not self._dispatch_msg_handler(category, msg):
if category == u'respond_to':
if not self._dispatch_msg_handler('default_reply', msg):
self._default_reply(msg)

def _dispatch_msg_handler(self, category, msg):
responded = False
for func, args in self._plugins.get_plugins(category, text):
for func, args in self._plugins.get_plugins(category, msg['text']):
if func:
responded = True
try:
func(Message(self._client, msg), *args)
except:
logger.exception(
'failed to handle message %s with plugin "%s"',
text, func.__name__)
msg['text'], func.__name__)
reply = u'[{}] I had a problem handling "{}"\n'.format(
func.__name__, text)
func.__name__, msg['text'])
tb = u'```\n{}\n```'.format(traceback.format_exc())
if self._errors_to:
self._client.rtm_send_message(msg['channel'], reply)
Expand All @@ -64,9 +69,7 @@ def dispatch_msg(self, msg):
self._client.rtm_send_message(msg['channel'],
'{}\n{}'.format(reply,
tb))

if not responded and category == u'respond_to':
self._default_reply(msg)
return responded

def _on_new_message(self, msg):
# ignore edits
Expand Down
3 changes: 2 additions & 1 deletion slackbot/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def __init__(self):

commands = {
'respond_to': {},
'listen_to': {}
'listen_to': {},
'default_reply': {}
}

def init_plugins(self):
Expand Down
25 changes: 24 additions & 1 deletion tests/unit/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ def raising(self, message):
def okay(self, message):
message.reply('okay')

def default_okay(self, message):
message.reply('default_okay')

def get_plugins(self, category, message):
return [[getattr(self, message), []]]
if message == 'no_plugin_defined':
return [[None, None]]
if category == 'default_reply':
return [[getattr(self, 'default_'+message), []]]
else:
return [[getattr(self, message), []]]


class FakeClient:
Expand Down Expand Up @@ -47,6 +55,7 @@ def setup_aliases(monkeypatch):

@pytest.fixture()
def dispatcher(monkeypatch):
monkeypatch.setattr('slackbot.settings.DEFAULT_REPLY', 'sorry')
dispatcher = slackbot.dispatcher.MessageDispatcher(None, None, None)
monkeypatch.setattr(dispatcher, '_get_bot_id', lambda: FAKE_BOT_ID)
monkeypatch.setattr(dispatcher, '_get_bot_name', lambda: FAKE_BOT_NAME)
Expand Down Expand Up @@ -177,3 +186,17 @@ def test_dispatch_msg_errors_to(dispatcher, monkeypatch):
error = dispatcher._client.rtm_messages[1]
assert error[0] == 'D12345'
assert 'RuntimeError' in error[1]


def test_dispatch_default_msg(dispatcher, monkeypatch):
monkeypatch.setattr('slackbot.dispatcher.Message', FakeMessage)
dispatcher.dispatch_msg(
['respond_to', {'text': 'no_plugin_defined', 'channel': FAKE_CHANNEL}])
assert dispatcher._client.rtm_messages == [(FAKE_CHANNEL, 'sorry')]


def test_dispatch_default_msg_plugin(dispatcher, monkeypatch):
monkeypatch.setattr('slackbot.dispatcher.Message', FakeMessage)
dispatcher.dispatch_msg(
['respond_to', {'text': 'default_okay', 'channel': FAKE_CHANNEL}])
assert dispatcher._client.rtm_messages == [(FAKE_CHANNEL, 'default_okay')]

0 comments on commit 26abfb8

Please sign in to comment.