Skip to content

Commit

Permalink
Merge pull request #2 from iky/reply-from-entrypoint-result
Browse files Browse the repository at this point in the history
Add simple reply mechanism
  • Loading branch information
iky committed Jul 27, 2017
2 parents 4624778 + 0ef3a90 commit 8164b83
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 7 deletions.
18 changes: 18 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Release Notes
=============

Version 0.0.3
-------------

Released 2017-07-27

* Adds replying by returning from handle_message entrypoint


Version 0.0.2
-------------

Released 2017-02-11

* Initial release
* Adds RTM extension
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright {yyyy} {name of copyright owner}
Copyright 2017 Ondrej Kohout

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
15 changes: 15 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,18 @@ to the entrypoint:
@rtm.handle_message('^egg (?P<ham>\w+)')
def on_egg(self, event, message, ham=None):
pass
Respond back to the channel by returning a string in the message handling
entrypoint:

.. code:: python
from nameko_slack import rtm
class Service:
name = 'some-service'
@rtm.handle_message
def sure(self, event, message):
return 'sure, {}'.format(message)
14 changes: 13 additions & 1 deletion nameko_slack/rtm.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from functools import partial
import re

import eventlet
Expand Down Expand Up @@ -52,6 +53,9 @@ def handle(self, event):
for handle_event in self._handlers:
handle_event(event)

def reply(self, event, message):
self._client.rtm_send_message(event['channel'], message)


class RTMEventHandlerEntrypoint(Entrypoint):

Expand Down Expand Up @@ -101,8 +105,16 @@ def handle_event(self, event):
args = (event, event.get('text'))
kwargs = {}
context_data = {}
handle_result = partial(self.handle_result, event)
self.container.spawn_worker(
self, args, kwargs, context_data=context_data)
self, args, kwargs,
context_data=context_data,
handle_result=handle_result)

def handle_result(self, event, worker_ctx, result, exc_info):
if result:
self.client.reply(event, result)
return result, exc_info


handle_message = RTMMessageHandlerEntrypoint.decorator
10 changes: 5 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='nameko-slack',
version='0.0.2',
version='0.0.3',
description='Nameko extension for interaction with Slack APIs',
author='Ondrej Kohout',
author_email='ondrej.kohout@gmail.com',
Expand All @@ -17,10 +17,10 @@
],
extras_require={
'dev': [
"coverage==4.3.4",
"flake8==3.2.1",
"pylint==1.6.4",
"pytest==3.0.5",
"coverage",
"flake8",
"pylint",
"pytest",
]
},
dependency_links=[],
Expand Down
22 changes: 22 additions & 0 deletions tests/test_rtm.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def _runner(service_class, events):
container.start()
sleep(0.1) # enough to handle all the test events

# return reply calls
return SlackClient.return_value.rtm_send_message.call_args_list

return _runner


Expand Down Expand Up @@ -270,6 +273,25 @@ def handle_spam_messages(self, event, message):
])


def test_replies_on_handle_message(events, service_runner):

class Service:

name = 'sample'

@rtm.handle_message
def handle_message(self, event, message):
return 'sure, {}'.format(message)

reply_calls = service_runner(Service, events)

assert reply_calls == [
call('D11', 'sure, spam ham'),
call('D11', 'sure, ham spam'),
call('D11', 'sure, spam egg'),
]


@patch('nameko_slack.rtm.SlackClient')
def test_handlers_do_not_block(
SlackClient, container_factory, config, tracker
Expand Down

0 comments on commit 8164b83

Please sign in to comment.