Skip to content

Commit

Permalink
Option to mention someone
Browse files Browse the repository at this point in the history
  • Loading branch information
dahlia committed Dec 13, 2017
1 parent 8650690 commit 2de489d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,14 @@ You can also filter some messages only
logger.info('info message') # Not posted to slack
logger.info('info message to slack', extra={'notify_slack': True}) # Posted to slack
Mentioning someone
''''''''''''''''''

Use ``mention`` option to send message with mentioning someone:

.. code-block:: python
sh = SlackHandler('YOUR_WEB_HOOK_URL', mention='U012ABC34')
You can find a member ID (e.g., ``U012ABC34``) from user's profile view.
13 changes: 10 additions & 3 deletions slack_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,32 @@


class SlackHandler(HTTPHandler):
def __init__(self, url, username=None, icon_url=None, icon_emoji=None, channel=None):
def __init__(self, url, username=None, icon_url=None, icon_emoji=None, channel=None, mention=None):
o = urlparse(url)
is_secure = o.scheme == 'https'
HTTPHandler.__init__(self, o.netloc, o.path, method="POST", secure=is_secure)
self.username = username
self.icon_url = icon_url
self.icon_emoji = icon_emoji
self.channel = channel
self.mention = mention and mention.lstrip('@')

def mapLogRecord(self, record):
text = self.format(record)

if isinstance(self.formatter, SlackFormatter):
payload = {
'attachments': [
self.format(record),
text,
],
}
if self.mention:
payload['text'] = '<@{0}>'.format(self.mention)
else:
if self.mention:
text = '<@{0}> {1}'.format(self.mention, text)
payload = {
'text': self.format(record),
'text': text,
}

if self.username:
Expand Down

0 comments on commit 2de489d

Please sign in to comment.