Skip to content

Commit 4bab6bd

Browse files
Merge pull request #47 from amckenna-pinterest/slack_sdk
Add async support for handle_event in plugins
2 parents 008d935 + f9d9a22 commit 4bab6bd

File tree

5 files changed

+18
-13
lines changed

5 files changed

+18
-13
lines changed

slackminion/bot.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ def _add_event_handlers(self):
290290
# generic handler for handling event types registered by plugins via notify_event_types class attribute
291291
async def _event_plugin(self, **payload):
292292
event_type, data = self._unpack_payload(**payload)
293-
self.plugin_manager.broadcast_event(event_type, data)
293+
await self.plugin_manager.broadcast_event(event_type, data)
294294

295295
# when the bot is invited to a channel, add the channel to self.channels
296296
async def _event_channel_joined(self, **payload):
@@ -336,9 +336,9 @@ def _prepare_and_send_output(self, cmd, msg, cmd_options, output):
336336
self.send_message(msg.channel, output, thread=thread_ts,
337337
reply_broadcast=cmd_options.get('reply_broadcast'), parse=parse)
338338

339-
def _event_error(self, **payload):
339+
async def _event_error(self, **payload):
340340
event_type, data = self._unpack_payload(**payload)
341-
self.plugin_manager.broadcast_event(event_type, data)
341+
await self.plugin_manager.broadcast_event(event_type, data)
342342
self.log.error(f"Received an error response from Slack: {payload}")
343343

344344
def get_channel_by_name(self, channel_name):

slackminion/plugin/manager.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import logging
3+
import inspect
34
from datetime import datetime
45

56

@@ -73,13 +74,16 @@ def load(self):
7374
# Broadcasts a slack event to handlers that have registered for the
7475
# event type via notify_event_types class attribute
7576
# Plugin MUST implement a handle_event method to handle these events.
76-
def broadcast_event(self, event_type, data):
77+
async def broadcast_event(self, event_type, data):
7778
for plugin in self.plugins:
7879
if event_type in plugin.notify_event_types:
7980
self.log.debug(
8081
f'Sending event of type {event_type} to plugin {plugin.__class__.__name__}. Data: {data}')
8182
try:
82-
plugin.handle_event(event_type, data)
83+
if inspect.iscoroutinefunction(plugin.handle_event):
84+
await plugin.handle_event(event_type, data)
85+
else:
86+
plugin.handle_event(event_type, data)
8387
# The plugin is expected to handle its own exceptions.
8488
except Exception: # noqa
8589
self.log.exception("Unhandled exception!")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = '1.1.4'
1+
version = '1.1.5'

slackminion/tests/test_bot.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,9 @@ def test_prepare_and_send_output_with_cmd_options(self):
208208
self.object.send_message.assert_called_with(self.test_event.channel, test_output, thread=test_thread_ts,
209209
reply_broadcast=None, parse=None)
210210

211-
def test_event_error(self):
212-
self.object._event_error(**test_payload)
211+
@async_test
212+
async def test_event_error(self):
213+
await self.object._event_error(**test_payload)
213214
self.object.log.error.assert_called_with(f"Received an error response from Slack: {test_payload}")
214215

215216
def test_get_channel_by_name(self):
@@ -243,19 +244,18 @@ def test_unpack_payload(self):
243244
self.assertEqual(event_type, test_payload['data']['type'])
244245
self.assertEqual(data, test_payload['data'])
245246

246-
247247
@mock.patch('slackminion.bot.MyRTMClient')
248248
@async_test
249249
async def test_handle_plugin_event(self, mock_rtm):
250250
self.object.plugin_manager = mock.Mock()
251251
plugin = PluginWithEvents(self.object)
252252
plugin.handle_event = mock.Mock()
253-
self.object.plugin_manager.broadcast_event = mock.Mock()
253+
self.object.plugin_manager.broadcast_event = AsyncMock()
254254
self.object.plugin_manager.plugins = [plugin]
255255
self.object._add_event_handlers()
256256
self.assertEqual(mock_rtm.on.call_count, 4)
257257
mock_rtm.on.assert_called_with(event=test_event_type,
258-
callback=self.object._event_plugin)
258+
callback=self.object._event_plugin)
259259
await self.object._event_plugin(**test_payload)
260260
self.object.plugin_manager.broadcast_event.assert_called_with(test_event_type, test_payload['data'])
261261

slackminion/tests/test_plugin/test_manager.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ def test_on_unload(self):
2121
self.object.unload_all()
2222
fake_plugin.on_unload.assert_called()
2323

24-
def test_broadcast_event(self):
24+
@async_test
25+
async def test_broadcast_event(self):
2526
plugin = PluginWithEvents(mock.Mock())
2627
plugin.handle_event = mock.Mock()
2728
self.object.plugins = [plugin]
28-
self.object.broadcast_event(test_event_type, test_payload['data'])
29+
await self.object.broadcast_event(test_event_type, test_payload['data'])
2930
plugin.handle_event.assert_called_with(test_event_type, test_payload['data'])

0 commit comments

Comments
 (0)