diff --git a/mmpy_bot/dispatcher.py b/mmpy_bot/dispatcher.py index 3bbe04c2..824503dd 100644 --- a/mmpy_bot/dispatcher.py +++ b/mmpy_bot/dispatcher.py @@ -264,12 +264,15 @@ def send_webapi(self, text, attachments=None, channel_id=None, **kwargs): attachments=attachments, ssl_verify=self._client.api.ssl_verify, **kwargs) - def reply(self, text, files=None, props={}): - self.send(self._gen_reply(text), files=files, props=props) + def reply(self, text, files=None, props=None): + self.send(self._gen_reply(text), files=files, props=props or {}) - def send(self, text, channel_id=None, files=None, props={}): + def reply_thread(self, text, files=None, props=None): + self.send(self._gen_reply(text), files=files, props=props or {}, pid=self._body['data']['post']['id']) + + def send(self, text, channel_id=None, files=None, props=None, pid=''): return self._client.channel_msg( - channel_id or self.channel, text, files=files, props=props) + channel_id or self.channel, text, files=files, pid=pid, props=props or {}) def update(self, text, message_id, channel_id=None): return self._client.update_msg( diff --git a/mmpy_bot/mattermost.py b/mmpy_bot/mattermost.py index 1a316288..c8e2dff8 100644 --- a/mmpy_bot/mattermost.py +++ b/mmpy_bot/mattermost.py @@ -37,7 +37,7 @@ def create_reaction(self, user_id, post_id, emoji_name): 'emoji_name': emoji_name, }) - def create_post(self, user_id, channel_id, message, files=None, pid="", props={}): + def create_post(self, user_id, channel_id, message, files=None, pid="", props=None): # create_at = int(time.time() * 1000) return self.post( '/posts', @@ -46,7 +46,7 @@ def create_post(self, user_id, channel_id, message, files=None, pid="", props={} 'message': message, 'file_ids': files or [], 'root_id': pid, - 'props': props + 'props': props or {} }) @staticmethod @@ -220,10 +220,10 @@ def react_msg(self, post_id, emoji_name): return self.api.create_reaction(self.user["id"], post_id, emoji_name) - def channel_msg(self, channel, message, files=None, pid="", props={}): + def channel_msg(self, channel, message, files=None, pid="", props=None): c_id = self.channels.get(channel, {}).get("id") or channel return self.api.create_post(self.user["id"], c_id, "{}".format(message), - files, pid, props=props) + files, pid, props=props or {}) def update_msg(self, message_id, channel, message, pid=""): c_id = self.channels.get(channel, {}).get("id") or channel diff --git a/mmpy_bot/plugins/hello.py b/mmpy_bot/plugins/hello.py index a58a1c0e..e4975554 100644 --- a/mmpy_bot/plugins/hello.py +++ b/mmpy_bot/plugins/hello.py @@ -53,3 +53,7 @@ def hello_comment(message): @listen_to('hello_react', re.IGNORECASE) def hello_react(message): message.react('+1') + +@listen_to('hello_reply_threaded', re.IGNORECASE) +def hello_reply_threaded(message): + message.reply_thread('hello threaded!') \ No newline at end of file diff --git a/tests/behavior_tests/bots/driver.py b/tests/behavior_tests/bots/driver.py index c12c1107..7957bad2 100644 --- a/tests/behavior_tests/bots/driver.py +++ b/tests/behavior_tests/bots/driver.py @@ -126,14 +126,6 @@ def send_direct_message(self, msg, tobot=False, colon=True): msg = self._format_message(msg, tobot=tobot, colon=colon) self._send_message_to_bot(self.dm_chan, msg) - def validate_bot_direct_message(self, match): - posts = self.bot._client.api.get('/channels/%s/posts' % self.dm_chan) - last_response = posts['posts'][posts['order'][0]] - if re.search(match, last_response['message']): - return - else: - raise AssertionError('expected to get message like "{}", but got nothing'.format(match)) - def wait_for_bot_direct_message(self, match, maxwait=10): self._wait_for_bot_message(self.dm_chan, match, maxwait=maxwait, tosender=False) @@ -154,6 +146,9 @@ def _has_got_message_rtm(self, channel, match, tosender=True, thread=False): if event['event'] == 'posted': post_data = json.loads(event['data']['post']) if re.match(match, post_data['message'], re.DOTALL): + if thread: + if post_data['parent_id'] == "": + return False return True return False @@ -185,16 +180,8 @@ def _send_channel_message(self, chan, msg, **kwargs): def send_channel_message(self, msg, **kwargs): self._send_channel_message(self.cm_chan, msg, **kwargs) - def validate_bot_channel_message(self, match): - posts = self.bot._client.api.get('/channels/%s/posts' % self.cm_chan) - last_response = posts['posts'][posts['order'][0]] - if re.search(match, last_response['message']): - return - else: - raise AssertionError('expected to get message like "{}", but got nothing'.format(match)) - - def wait_for_bot_channel_message(self, match, tosender=True): - self._wait_for_bot_message(self.cm_chan, match, tosender=tosender) + def wait_for_bot_channel_message(self, match, tosender=True, thread=False): + self._wait_for_bot_message(self.cm_chan, match, tosender=tosender, thread=thread) def send_private_channel_message(self, msg, **kwargs): self._send_channel_message(self.gm_chan, msg, **kwargs) diff --git a/tests/behavior_tests/test_conversation.py b/tests/behavior_tests/test_conversation.py index e89a087c..b4c98e6d 100644 --- a/tests/behavior_tests/test_conversation.py +++ b/tests/behavior_tests/test_conversation.py @@ -53,3 +53,7 @@ def test_bot_reply_to_private_channel_message(driver): driver.wait_for_bot_private_channel_message('hello sender!') driver.send_private_channel_message('hello', colon=False) driver.wait_for_bot_private_channel_message('hello sender!') + +def test_bot_reply_to_message_thread(driver): + driver.send_channel_message('hello_reply_threaded', tobot=False) + driver.wait_for_bot_channel_message('hello threaded!', thread=True) \ No newline at end of file diff --git a/tests/unit_tests/test_messagedispatcher.py b/tests/unit_tests/test_messagedispatcher.py index 47f43d93..9b02c844 100644 --- a/tests/unit_tests/test_messagedispatcher.py +++ b/tests/unit_tests/test_messagedispatcher.py @@ -17,7 +17,7 @@ def test_get_message(message): raise AssertionError() -def test__ignore_sender(): +def test_ignore_sender(): dispatcher = MessageDispatcher(None, None) event = {'event': 'posted', 'data': {'sender_name': 'betty'}} event2 = {'event': 'posted', 'data': {'sender_name': 'Carter'}}