Skip to content

Commit

Permalink
Adding reply_thread to message api (#91)
Browse files Browse the repository at this point in the history
Adding reply_thread to message api

As per #90 enhancement request adding a new api to message class to
allow replying to messages in a threads.
  • Loading branch information
attzonko authored Jan 21, 2019
1 parent 0f197bb commit 808b095
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 27 deletions.
11 changes: 7 additions & 4 deletions mmpy_bot/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions mmpy_bot/mattermost.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions mmpy_bot/plugins/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -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!')
23 changes: 5 additions & 18 deletions tests/behavior_tests/bots/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions tests/behavior_tests/test_conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion tests/unit_tests/test_messagedispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'}}
Expand Down

0 comments on commit 808b095

Please sign in to comment.