-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnotify.py
59 lines (51 loc) · 2.45 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
49
50
51
52
53
54
55
56
57
58
59
from parameters.remote_parameters import webhook_url
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': "@david " + message + " " + caboose}
slack_data['username'] = 'TACHYON'
if username is not None:
slack_data['username'] = username
#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)
)
def mail_via_aws_ses(mail_parameters):
pass
def mail(mail_parameters):
mail_via_aws_ses(mail_parameters)
# [ ] Do something clever where if one channel fails, send a notification via the other channel.
def send_notifications(slack_parameters=None, mail_parameters=None):
if slack_parameters is not None:
sp = slack_parameters
message = sp['message']
username = sp.get('username', None)
channel = sp.get('channel', None)
icon = sp.get('icon', None)
send_to_slack(message, username, channel, icon)
if mail_parameters is not None:
mail(mail_parameters)
if __name__ == '__main__':
msg = "No sir, away! A papaya war is on!"
send_to_slack(msg)