Skip to content

Commit

Permalink
react + comment
Browse files Browse the repository at this point in the history
  • Loading branch information
gotlium committed Feb 28, 2016
1 parent d2da552 commit 48c2701
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
5 changes: 4 additions & 1 deletion docs/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Settings
WORKERS_NUM = 10
'''
# You can specify you custom module.
# Custom default reply module
Example:
filename:
Expand All @@ -44,6 +44,9 @@ Settings
'''
DEFAULT_REPLY_MODULE = None
# or simple string for default answer
DEFAULT_REPLY = None
'''
If you use Mattermost Web API to send messages (with send_webapi()
or reply_webapi()), you can customize the bot logo by providing Icon or Emoji.
Expand Down
20 changes: 20 additions & 0 deletions mattermost_bot/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ def loop(self):
self._on_new_message(self.event)

def _default_reply(self, msg):
if settings.DEFAULT_REPLY:
return self._client.channel_msg(
msg['channel_id'], settings.DEFAULT_REPLY)

default_reply = [
u'Bad command "%s", You can ask me one of the '
u'following questions:\n' % self.get_message(msg),
Expand All @@ -122,6 +126,9 @@ class Message(object):
channels = {}

def __init__(self, client, body, pool):
from mattermost_bot.bot import PluginsManager

self._plugins = PluginsManager()
self._client = client
self._body = body
self._pool = pool
Expand Down Expand Up @@ -207,6 +214,19 @@ def reply(self, text):
def send(self, text, channel_id=None):
self._client.channel_msg(channel_id or self._body['channel_id'], text)

def react(self, emoji_name):
self._client.channel_msg(
self._body['channel_id'], emoji_name,
pid=self._body['props']['post']['id'])

def comment(self, message):
self.react(message)

def docs_reply(self, docs_format=' • `{0}` {1}'):
reply = [docs_format.format(v.__name__, v.__doc__ or "")
for p, v in iteritems(self._plugins.commands['respond_to'])]
return '\n'.join(reply)

@property
def channel(self):
return self._body['channel_id']
Expand Down
10 changes: 6 additions & 4 deletions mattermost_bot/mattermost.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def login(self, name, email, password):
self.token = p.headers["Token"]
return json.loads(p.text)

def create_post(self, user_id, channel_id, message, files=None):
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,
Expand All @@ -42,7 +42,9 @@ def create_post(self, user_id, channel_id, message, files=None):
'create_at': create_at,
'filenames': files or [],
'pending_post_id': user_id + ':' + str(create_at),
'state': "loading"
'state': "loading",
'parent_id': pid,
'root_id': pid,
})

def channel(self, channel_id):
Expand Down Expand Up @@ -113,9 +115,9 @@ def login(self, team, email, password):
self.info = self.api.me()
return self.user

def channel_msg(self, channel, message):
def channel_msg(self, channel, message, pid=""):
c_id = self.channels.get(channel, {}).get("id") or channel
return self.api.create_post(self.user["id"], c_id, message)
return self.api.create_post(self.user["id"], c_id, message, pid=pid)

def get_users(self):
return self.api.get_profiles()
Expand Down
10 changes: 10 additions & 0 deletions mattermost_bot/plugins/hello.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,13 @@ def web_api_reply(message):
username='Mattermost-Bot',
icon_url='https://goo.gl/OF4DBq',
)


@listen_to('hello_comment', re.IGNORECASE)
def web_api_reply(message):
message.comment('some comments ...')


@listen_to('hello_react', re.IGNORECASE)
def web_api_reply(message):
message.react(':+1:')
1 change: 1 addition & 0 deletions mattermost_bot/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
WORKERS_NUM = 10

DEFAULT_REPLY_MODULE = None
DEFAULT_REPLY = None

'''
If you use Mattermost Web API to send messages (with send_webapi()
Expand Down

0 comments on commit 48c2701

Please sign in to comment.