Skip to content

Commit c7676f3

Browse files
committed
(+) format: adding options for slack message formatting
1 parent 5d0a450 commit c7676f3

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

etc/slacker/config.dist.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
# This is example of config file for slacker.
33
# There are two sections:
44
# * default: this is default options for sending message to slack.
5-
# * rules: list of rules for matching email message. Currently 'from',
5+
# * rules: list of rules for matching email message. Currently 'from',
66
# 'to' and 'subject' fields are supported. Their values can be
77
# regexp to match corresponding email field.
8-
#
8+
#
99
# Each rule in list tested in order. First matched rule is used to update
1010
# values from 'default' section with its 'options' subsection.
1111
######
@@ -16,6 +16,7 @@ default:
1616
icon_url: ''
1717
slack_token: xoxb-00000000000-aaaaaaaaaaaaaaaaaaaaaaaa
1818
debug: false
19+
format: "subject: %(subject)s; body: %(body)s" ## default slack message format
1920

2021

2122
rules:
@@ -38,3 +39,4 @@ rules:
3839
channel: '#cron'
3940
icon_url: ''
4041
debug: true ## will output full email with all headers
42+
format: "email body is: %(body)s" ## custom message format

handler.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def handle_message(self, message):
2525
options = self.process_rules(message)
2626

2727
print('matched', options)
28-
self.send_to_slack(message.get_payload(), **options)
28+
self.send_to_slack(self.extract_text(message), **options)
2929

3030
if options['debug']:
3131
self.send_to_slack('DEBUG: ' + str(message), **options)
@@ -58,6 +58,12 @@ def process_rules(self, message):
5858

5959
return default
6060

61+
def extract_text(self, message):
62+
fmt = self.config['default'].get('format', '%(body)s')
63+
body = message.get_payload()
64+
subject = message['Subject']
65+
return fmt % dict(body=body, subject=subject)
66+
6167
def send_to_slack(self, text, **options):
6268
print('sending to slack', text, options)
6369

0 commit comments

Comments
 (0)