-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslack_msg.py
executable file
·57 lines (47 loc) · 1.33 KB
/
slack_msg.py
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env python3
import argparse
import os
import platform
import requests
import sys
import yaml
options = ['url', 'channel', 'username', 'icon']
config_file = '/etc/slack_msg.yaml'
config = {
'username': platform.node(),
'icon': 'robot_face'
}
if os.path.exists(config_file):
try:
with open(config_file) as fh:
config = yaml.load(fh, Loader=yaml.SafeLoader)
except Exception as e:
sys.stderr.write('Failed to load %s: %s\n' % (config_file, e))
sys.exit(1)
parser = argparse.ArgumentParser()
parser.add_argument('--message')
for option in options:
parser.add_argument('--%s' % option)
args = vars(parser.parse_args())
for option in options:
if args[option] is not None:
config[option] = args[option]
if option not in config:
sys.stderr.write('Missing setting: %s\n' % option)
sys.exit(1)
if args["message"] is None:
message = ""
for line in sys.stdin:
message += line
else:
message = args["message"]
payload = {
'channel': config['channel'],
'username': config['username'],
'icon_emoji': ':%s:' % config['icon'],
'text': message
}
if 'proxy' in config and config['proxy'] is not None:
requests.post(config['url'], json=payload, proxies={'https': config['proxy']})
else:
requests.post(config['url'], json=payload)