diff --git a/README.md b/README.md index a0a76be..4eae564 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ 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 >= 1.x +* Mattermost >= 3.0 (for versions < 3.0, please use app version <= 1.0.15) ## Installation @@ -43,7 +43,7 @@ Then you need to configure the `BOT_URL`, `BOT_LOGIN`, `BOT_PASSWORD`, `BOT_TEAM mattermost_bot_settings.py: ```python -BOT_URL = 'http:///api/v1' # with 'http://' and with '/api/v1' path. without trailing slash +BOT_URL = 'http:///api/v3' # with 'http://' and with '/api/v3' path. without trailing slash. '/api/v1' - for version < 3.0 BOT_LOGIN = '' BOT_PASSWORD = '' BOT_TEAM = '' # possible in lowercase @@ -171,9 +171,9 @@ And add the plugins module to `PLUGINS` list of mattermost_bot settings, e.g. ma ```python PLUGINS = [ 'mattermost_bot.plugins', - 'devops.plugins', # e.g. git submodule: domain:devops-plugins.git + 'devops.plugins', # e.g. git submodule: domain:devops-plugins.git 'programmers.plugins', # e.g. python package: package_name.plugins - 'frontend.plugins', # e.g. project tree: apps.bot.plugins + 'frontend.plugins', # e.g. project tree: apps.bot.plugins ] ``` *For example you can separate git repositories with plugins on your team.* @@ -182,6 +182,3 @@ PLUGINS = [ If you are migrating from `Slack` to the `Mattermost`, and previously you are used `SlackBot`, you can use this battery without any problem. On most cases your plugins will be working properly if you are used standard API or with minimal modifications. - - -Source based on [SlackBot](https://github.com/lins05/slackbot). diff --git a/mattermost_bot/__init__.py b/mattermost_bot/__init__.py index 28e5c5c..5e6cbd6 100644 --- a/mattermost_bot/__init__.py +++ b/mattermost_bot/__init__.py @@ -1,4 +1,4 @@ -VERSION = (1, 0, 14) +VERSION = (1, 0, 15) def get_version(): diff --git a/mattermost_bot/dispatcher.py b/mattermost_bot/dispatcher.py index 924a0a6..4e18f4c 100644 --- a/mattermost_bot/dispatcher.py +++ b/mattermost_bot/dispatcher.py @@ -159,7 +159,7 @@ def get_channel_name(self, channel_id=None): return channel_name def get_team_id(self): - return self._client.user.get('team_id') + return self._client.api.team_id def get_message(self): return self._body['props']['post']['message'].strip() diff --git a/mattermost_bot/mattermost.py b/mattermost_bot/mattermost.py index ea60bc1..10497b5 100644 --- a/mattermost_bot/mattermost.py +++ b/mattermost_bot/mattermost.py @@ -12,6 +12,8 @@ class MattermostAPI(object): def __init__(self, url): self.url = url self.token = "" + self.initial = None + self.team_id = None def _get_headers(self): return {"Authorization": "Bearer " + self.token} @@ -28,48 +30,54 @@ def post(self, request, data=None): ).text) def login(self, name, email, password): - props = {'name': name, 'email': email, 'password': password} - p = requests.post(self.url + '/users/login', data=json.dumps(props)) + props = {'name': name, 'login_id': email, 'password': password} + p = requests.post( + self.url + '/users/login', data=json.dumps(props)) self.token = p.headers["Token"] + self.load_initial_data() return json.loads(p.text) + def load_initial_data(self): + self.initial = self.get('/users/initial_load') + self.team_id = self.initial['teams'][0]['id'] + def create_post(self, user_id, channel_id, message, files=None, pid=""): create_at = int(time.time() * 1000) - return self.post('/channels/%s/create' % channel_id, { - 'user_id': user_id, - 'channel_id': channel_id, - 'message': message, - 'create_at': create_at, - 'filenames': files or [], - 'pending_post_id': user_id + ':' + str(create_at), - 'state': "loading", - 'parent_id': pid, - 'root_id': pid, - }) + return self.post( + '/teams/%s/channels/%s/posts/create' % (self.team_id, channel_id), + { + 'user_id': user_id, + 'channel_id': channel_id, + 'message': message, + 'create_at': create_at, + 'filenames': files or [], + 'pending_post_id': user_id + ':' + str(create_at), + 'state': "loading", + 'parent_id': pid, + 'root_id': pid, + }) def channel(self, channel_id): - return self.get('/channels/%s/' % channel_id) - - def get_channel_posts(self, channel_id, since): - return self.get('/channels/%s/posts/%s' % (channel_id, since)) + return self.get('/teams/%s/channels/%s/' % (self.team_id, channel_id)) def get_channels(self): - return self.get('/channels/').get('channels') + return self.get('/teams/%s/channels/' % self.team_id).get('channels') def get_profiles(self): - return self.get('/users/profiles') + return self.get('/users/profiles/%s' % self.team_id) def me(self): return self.get('/users/me') def user(self, user_id): - return self.get('/users/%s' % user_id) + return self.get_profiles()[user_id] def hooks_list(self): - return self.get('/hooks/incoming/list') + return self.get('/teams/%s/hooks/incoming/list' % self.team_id) def hooks_create(self, **kwargs): - return self.post('/hooks/incoming/create', kwargs) + return self.post( + '/teams/%s/hooks/incoming/create' % self.team_id, kwargs) @staticmethod def in_webhook(url, channel, text, username=None, as_user=None, @@ -123,14 +131,9 @@ def get_users(self): return self.api.get_profiles() def connect_websocket(self): - from websocket._exceptions import WebSocketBadStatusException - host = self.api.url.replace('http', 'ws').replace('https', 'wss') - url = host + '/websocket?session_token_index=0&1' - try: - self._connect_websocket(url, cookie_name='MMTOKEN') - except WebSocketBadStatusException: - self._connect_websocket(url, cookie_name='MMAUTHTOKEN') + url = host + '/users/websocket' + self._connect_websocket(url, cookie_name='MMAUTHTOKEN') return self.websocket.getstatus() == 101 def _connect_websocket(self, url, cookie_name): @@ -161,4 +164,3 @@ def messages(self, ignore_own_msg=False, filter_action=None): def ping(self): self.websocket.ping() - diff --git a/mattermost_bot/settings.py b/mattermost_bot/settings.py index e4fcbf8..8cf3571 100644 --- a/mattermost_bot/settings.py +++ b/mattermost_bot/settings.py @@ -54,6 +54,3 @@ from local_settings import * except ImportError: pass - -if not BOT_URL.endswith('/api/v1'): - BOT_URL = '%s%sapi/v1' % (BOT_URL, '' if BOT_URL.endswith('/') else '/')