Skip to content

Commit

Permalink
mm >= 3.0 support
Browse files Browse the repository at this point in the history
  • Loading branch information
gotlium committed May 20, 2016
1 parent ac10cb2 commit 059b104
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 42 deletions.
11 changes: 4 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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://<mm.example.com>/api/v1' # with 'http://' and with '/api/v1' path. without trailing slash
BOT_URL = 'http://<mm.example.com>/api/v3' # with 'http://' and with '/api/v3' path. without trailing slash. '/api/v1' - for version < 3.0
BOT_LOGIN = '<bot-email-address>'
BOT_PASSWORD = '<bot-password>'
BOT_TEAM = '<your-team>' # possible in lowercase
Expand Down Expand Up @@ -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.*
Expand All @@ -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).
2 changes: 1 addition & 1 deletion mattermost_bot/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = (1, 0, 14)
VERSION = (1, 0, 15)


def get_version():
Expand Down
2 changes: 1 addition & 1 deletion mattermost_bot/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
62 changes: 32 additions & 30 deletions mattermost_bot/mattermost.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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,
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -161,4 +164,3 @@ def messages(self, ignore_own_msg=False, filter_action=None):

def ping(self):
self.websocket.ping()

3 changes: 0 additions & 3 deletions mattermost_bot/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 '/')

0 comments on commit 059b104

Please sign in to comment.