Skip to content

Commit f49e691

Browse files
authored
Merge pull request #4 from spoqa/mention
Option to mention someone
2 parents 8650690 + 2de489d commit f49e691

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

README.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,14 @@ You can also filter some messages only
6262
6363
logger.info('info message') # Not posted to slack
6464
logger.info('info message to slack', extra={'notify_slack': True}) # Posted to slack
65+
66+
Mentioning someone
67+
''''''''''''''''''
68+
69+
Use ``mention`` option to send message with mentioning someone:
70+
71+
.. code-block:: python
72+
73+
sh = SlackHandler('YOUR_WEB_HOOK_URL', mention='U012ABC34')
74+
75+
You can find a member ID (e.g., ``U012ABC34``) from user's profile view.

slack_logger.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,32 @@
55

66

77
class SlackHandler(HTTPHandler):
8-
def __init__(self, url, username=None, icon_url=None, icon_emoji=None, channel=None):
8+
def __init__(self, url, username=None, icon_url=None, icon_emoji=None, channel=None, mention=None):
99
o = urlparse(url)
1010
is_secure = o.scheme == 'https'
1111
HTTPHandler.__init__(self, o.netloc, o.path, method="POST", secure=is_secure)
1212
self.username = username
1313
self.icon_url = icon_url
1414
self.icon_emoji = icon_emoji
1515
self.channel = channel
16+
self.mention = mention and mention.lstrip('@')
1617

1718
def mapLogRecord(self, record):
19+
text = self.format(record)
20+
1821
if isinstance(self.formatter, SlackFormatter):
1922
payload = {
2023
'attachments': [
21-
self.format(record),
24+
text,
2225
],
2326
}
27+
if self.mention:
28+
payload['text'] = '<@{0}>'.format(self.mention)
2429
else:
30+
if self.mention:
31+
text = '<@{0}> {1}'.format(self.mention, text)
2532
payload = {
26-
'text': self.format(record),
33+
'text': text,
2734
}
2835

2936
if self.username:

0 commit comments

Comments
 (0)