diff --git a/README.md b/README.md index 52a725d..75dd3a1 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,16 @@ A chat bot for [Mattermost](http://www.mattermost.org). * Messages can be handled concurrently * Automatically reconnect to mattermost when connection is lost * Python3 Support -* Mattermost >= 3.0 (for versions < 3.0, please use app version <= 1.0.15) + + +## Compatibility + +| Mattermost | MatterBot | +|------------------|:----------:| +| 3.3 | 1.0.18 | +| >= 3.0 && <= 3.2 | 1.0.17 | +| < 3.0 | < 1.0.16 | + ## Installation diff --git a/mattermost_bot/dispatcher.py b/mattermost_bot/dispatcher.py index 1d972f6..408a262 100644 --- a/mattermost_bot/dispatcher.py +++ b/mattermost_bot/dispatcher.py @@ -5,6 +5,7 @@ import importlib import traceback import logging +import json import re from six import iteritems @@ -25,14 +26,14 @@ def __init__(self, client, plugins): self._pool = WorkerPool(self.dispatch_msg, settings.WORKERS_NUM) self._plugins = plugins self._channel_info = {} + self.event = None def start(self): self._pool.start() @staticmethod def get_message(msg): - return msg.get( - 'props', {}).get('post', {}).get('message', '').strip() + return msg.get('data', {}).get('post', {}).get('message', '').strip() def ignore(self, _msg): msg = self.get_message(_msg) @@ -41,7 +42,7 @@ def ignore(self, _msg): return True def is_mentioned(self, msg): - mentions = msg.get('props', {}).get('mentions', []) + mentions = msg.get('data', {}).get('mentions', []) return self._client.user['id'] in mentions def is_personal(self, msg): @@ -96,11 +97,20 @@ def filter_text(self, msg): if self.is_mentioned(msg): m = MESSAGE_MATCHER.match(text) if m: - msg['props']['post']['message'] = m.group(2).strip() + msg['data']['post']['message'] = m.group(2).strip() return msg + def load_json(self): + if self.event.get('data', {}).get('post'): + self.event['data']['post'] = json.loads( + self.event['data']['post']) + if self.event.get('data', {}).get('mentions'): + self.event['data']['mentions'] = json.loads( + self.event['data']['mentions']) + def loop(self): for self.event in self._client.messages(True, 'posted'): + self.load_json() self._on_new_message(self.event) def _default_reply(self, msg): @@ -162,7 +172,7 @@ def get_team_id(self): return self._client.api.team_id def get_message(self): - return self._body['props']['post']['message'].strip() + return self._body['data']['post']['message'].strip() def is_direct_message(self): return self._body['message_type'] == 'D' @@ -171,7 +181,7 @@ def get_busy_workers(self): return self._pool.get_busy_workers() def get_mentions(self): - return self._body['props'].get('mentions') + return self._body['data'].get('mentions') def _gen_at_message(self, text): return '@{}: {}'.format(self.get_username(), text) @@ -218,7 +228,7 @@ def send(self, text, channel_id=None): def react(self, emoji_name): self._client.channel_msg( self._body['channel_id'], emoji_name, - pid=self._body['props']['post']['id']) + pid=self._body['data']['post']['id']) def comment(self, message): self.react(message) diff --git a/mattermost_bot/mattermost.py b/mattermost_bot/mattermost.py index ade6e63..728ac74 100644 --- a/mattermost_bot/mattermost.py +++ b/mattermost_bot/mattermost.py @@ -111,7 +111,7 @@ def in_webhook(url, channel, text, username=None, as_user=None, class MattermostClient(object): - def __init__(self, url, team, email, password, ssl_verify=True): + def __init__(self, url, team, email, password, ssl_verify=True, login=1): self.users = {} self.channels = {} self.mentions = {} @@ -124,7 +124,8 @@ def __init__(self, url, team, email, password, ssl_verify=True): self.email = email self.password = password - self.login(team, email, password) + if login: + self.login(team, email, password) def login(self, team, email, password): self.email = email @@ -162,7 +163,7 @@ def messages(self, ignore_own_msg=False, filter_action=None): if data: try: post = json.loads(data) - if filter_action and post.get('action') != filter_action: + if filter_action and post.get('event') != filter_action: continue if ignore_own_msg is True and post.get("user_id"): if self.user["id"] == post.get("user_id"):