-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreporter_slack.py
More file actions
39 lines (28 loc) · 1.24 KB
/
reporter_slack.py
File metadata and controls
39 lines (28 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import sys
import json
import requests
from settings import RackspaceStoredSettings
class ReportSlacker(RackspaceStoredSettings):
def __init__(self, *args, **kwargs):
super(ReportSlacker, self).__init__(*args, **kwargs)
try:
self.webhook_url = self.setting('SLACK_WEBHOOK')
except KeyError:
try:
self.webhook_url = self.settings['slack_webhook']
except KeyError:
raise EnvironmentError('Backuper: Setting `SLACK_WEBHOOK` is not defined in the environment')
def send_webhook_request(self, text, channel='#logs', username='backup-bot', icon_emoji=':mega:', **kwargs):
message = {'text': text, 'channel': channel, 'username': username, 'icon_emoji': icon_emoji, }
message.update(kwargs)
return requests.post(self.webhook_url, data=json.dumps(message), headers={'content-type': 'application/json'})
def report_stdin(self):
return self.send_webhook_request(text='\n'.join([line.strip() for line in sys.stdin.readlines()]))
def main():
"""
Script's entry point
"""
slack_response = ReportSlacker().report_stdin()
print slack_response.status_code, slack_response.content
if __name__ == '__main__':
main()