From 3b1ef57694ab519455a0b2a8ab8aa4e9e5261292 Mon Sep 17 00:00:00 2001 From: Lunatic Luna <59903692+iishiishii@users.noreply.github.com> Date: Tue, 21 Apr 2020 14:13:56 +0700 Subject: [PATCH] bots: Support adding reactions to message for a bot. It adds a react() function that allows a bot to react to a message in lib.py. It adds an example of the use of react() function and its test. The changes are in the following files: - lib.py - helloworld.py - tests/test_lib.py - test_lib.py --- .../zulip_bots/bots/helloworld/helloworld.py | 4 ++++ zulip_bots/zulip_bots/lib.py | 5 +++++ zulip_bots/zulip_bots/simple_lib.py | 3 +++ zulip_bots/zulip_bots/test_lib.py | 3 +++ zulip_bots/zulip_bots/tests/test_lib.py | 15 +++++++++++++++ 5 files changed, 30 insertions(+) diff --git a/zulip_bots/zulip_bots/bots/helloworld/helloworld.py b/zulip_bots/zulip_bots/bots/helloworld/helloworld.py index 24b520a..17f378e 100644 --- a/zulip_bots/zulip_bots/bots/helloworld/helloworld.py +++ b/zulip_bots/zulip_bots/bots/helloworld/helloworld.py @@ -16,4 +16,8 @@ class HelloWorldHandler: content = 'beep boop' # type: str bot_handler.send_reply(message, content) + emoji_name = 'wave' # type: str + bot_handler.react(message, emoji_name) + return + handler_class = HelloWorldHandler diff --git a/zulip_bots/zulip_bots/lib.py b/zulip_bots/zulip_bots/lib.py index 8e6e133..32b69cb 100644 --- a/zulip_bots/zulip_bots/lib.py +++ b/zulip_bots/zulip_bots/lib.py @@ -153,6 +153,11 @@ class ExternalBotHandler: def identity(self) -> BotIdentity: return BotIdentity(self.full_name, self.email) + def react(self, message: Dict[str, Any], emoji_name: str) -> Dict[str, Any]: + return self._client.add_reaction(dict(message_id=message['id'], + emoji_name=emoji_name, + reaction_type='unicode_emoji')) + def send_message(self, message: (Dict[str, Any])) -> Dict[str, Any]: if not self._rate_limit.is_legal(): self._rate_limit.show_error_and_exit() diff --git a/zulip_bots/zulip_bots/simple_lib.py b/zulip_bots/zulip_bots/simple_lib.py index 052af11..ae16847 100644 --- a/zulip_bots/zulip_bots/simple_lib.py +++ b/zulip_bots/zulip_bots/simple_lib.py @@ -30,6 +30,9 @@ class SimpleMessageServer: self.messages[self.message_id] = message return message + def add_reaction(self, reaction_data): + return dict(result='success', msg='', uri='https://server/messages/{}/reactions'.format(uuid4())) + def update(self, message): self.messages[message['message_id']] = message diff --git a/zulip_bots/zulip_bots/test_lib.py b/zulip_bots/zulip_bots/test_lib.py index 0bc0874..1708993 100755 --- a/zulip_bots/zulip_bots/test_lib.py +++ b/zulip_bots/zulip_bots/test_lib.py @@ -50,6 +50,9 @@ class StubBotHandler: self.transcript.append(('send_reply', response_message)) return self.message_server.send(response_message) + def react(self, message: Dict[str, Any], emoji_name: str) -> Dict[str, Any]: + return self.message_server.add_reaction(emoji_name) + def update_message(self, message: Dict[str, Any]) -> None: self.message_server.update(message) diff --git a/zulip_bots/zulip_bots/tests/test_lib.py b/zulip_bots/zulip_bots/tests/test_lib.py index 5d3d766..c42c52a 100644 --- a/zulip_bots/zulip_bots/tests/test_lib.py +++ b/zulip_bots/zulip_bots/tests/test_lib.py @@ -109,6 +109,21 @@ class LibTest(TestCase): client.get_storage.assert_not_called() self.assertEqual(val, [5]) + def test_react(self): + client = FakeClient() + handler = ExternalBotHandler( + client = client, + root_dir=None, + bot_details=None, + bot_config_file=None + ) + emoji_name = 'wave' + message = {'id': 10} + expected = {'message_id': message['id'], 'emoji_name': 'wave', 'reaction_type': 'unicode_emoji'} + client.add_reaction = MagicMock() + handler.react(message, emoji_name) + client.add_reaction.assert_called_once_with(dict(expected)) + def test_send_reply(self): client = FakeClient() profile = client.get_profile()