-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify.py
48 lines (43 loc) · 2.07 KB
/
notify.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
from parameters.remote_parameters import webhook_url
def file_name():
import os
full_path = os.path.realpath(__file__)
parts = full_path.split('/')
if len(parts) < 3:
return full_path
return '/'.join(parts[-2:])
def send_to_slack(message,username=None,channel=None,icon=None):
"""This script sends the given message to a particular channel on
Slack, as configured by the webhook_url. Note that this shouldn't
be heavily used (e.g., for reporting every error a script
encounters) as API limits are a consideration. This script IS
suitable for running when a script-terminating exception is caught,
so that you can report the irregular termination of an ETL script."""
import os, re, json, requests
import socket
IP_address = socket.gethostbyname(socket.gethostname())
hostname = re.sub(".local","",socket.gethostname())
name_of_current_script = os.path.basename(__file__)
caboose = "(Sent from {} running on a computer called {} at {}.)".format(name_of_current_script, hostname, IP_address)
# Set the webhook_url to the one provided by Slack when you create the webhook at https://my.slack.com/services/new/incoming-webhook/
slack_data = {'text': message + " " + caboose}
slack_data['username'] = "({})".format(file_name()) #'TACHYON'
if username is not None:
slack_data['username'] = "{} ({})".format(username, file_name())
#To send this as a direct message instead, use the following line.
if channel is not None:
slack_data['channel'] = channel
if icon is not None:
slack_data['icon_emoji'] = icon #':coffin:' #':tophat:' # ':satellite_antenna:'
response = requests.post(
webhook_url, data=json.dumps(slack_data),
headers={'Content-Type': 'application/json'}
)
if response.status_code != 200:
raise ValueError(
'Request to Slack returned an error %s, the response is:\n%s'
% (response.status_code, response.text)
)
if __name__ == '__main__':
msg = "No sir, away! A papaya war is on!"
send_to_slack(msg)