Skip to content

Commit

Permalink
#20 fixes for v3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
gotlium committed Aug 17, 2016
1 parent d130c09 commit ba851e9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
24 changes: 17 additions & 7 deletions mattermost_bot/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import importlib
import traceback
import logging
import json
import re

from six import iteritems
Expand All @@ -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)
Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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'
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
7 changes: 4 additions & 3 deletions mattermost_bot/mattermost.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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
Expand Down Expand Up @@ -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"):
Expand Down

0 comments on commit ba851e9

Please sign in to comment.