/
notifications.py
executable file
·163 lines (141 loc) · 4.66 KB
/
notifications.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
import yaml
import simplejson
from helpers import do_curl, get_config
from sh import sendemail, ssh, curl
import notifications
from html import HTML
config = get_config('notifications')
def notify(event, message, **kw):
if not event in config['events']:
return
if not 'app' in kw:
kw['app'] = config['default_app']
for user, methods in config['events'][event].items():
if not hasattr(methods, '__iter__'):
methods = (methods,)
for method in methods:
getattr(notifications, "send_" + method)(user, event, message, **kw)
def send_pushover(user, event, message, **kw):
if not user in config['pushover']['user_keys']:
return
if not 'app' in kw:
kw['app'] = config['default_app']
token = config['pushover']['tokens'][kw['app']]
args = {
'token': token,
'user': config['pushover']['user_keys'][user],
'message': message,
}
args = dict(args, **kw);
result = do_curl(config['pushover']['path'], **args)
print "Sent %s Pushover notification to %s" % (event, user)
return simplejson.loads(str(result));
def send_email(user, event, message, **kw):
if not user in config['email']['user_emails']:
return
args = {
'f': config['email']['from_address'],
't': config['email']['user_emails'][user],
'u': kw['subject'] if 'subject' in kw else 'Notification',
}
if not 'app' in kw:
kw['app'] = config['default_app']
body = HTML('html')
tr = body.table().tr()
tr.td(valign='top').img(src=config['icons'][kw['app']], style='float:left; margin: 15px')
try:
if 'email_body' in kw:
tr.td().text(kw['email_body'], escape=False)
else:
getattr(notifications, event + '_email')(tr.td(), message, **kw)
except:
with tr.td().p(style='margin-top: 15px') as p:
p.b("Message:")
p.br()
p.text(message)
ip = curl('ifconfig.me').strip()
if ip != config['ip']:
ybdst = ssh.bake(config['ip'])
print "Sent %s email to %s" % (event, user)
return ybdst.sendemail(_in=str(body), **args)
else:
print "Sent %s email to %s" % (event, user)
return sendemail(_in=str(body), **args)
def custom_field_email(html, message, **kw):
html.p(message)
if len(kw['passed']) > 0:
p = html.p("The following fields were present:")
l = p.ul()
for passed in kw['passed']:
l.li(passed)
if len(kw['failed']) > 0:
p = html.p("The following fields were missing:")
l = p.ul()
for failed in kw['failed']:
l.li(failed)
if len(kw['actions']) > 0:
p = html.p("The following actons were taken to resolve the problem:")
l = p.ol()
for action in kw['actions']:
l.li(action)
def site_sync_email(html, message, **kw):
html.p(message)
if len(kw['data']) > 0:
l = html.ul();
for site in kw['data'].values():
sl = l.li(site['site_name']).ul()
if 'fields' in site:
fl = sl.li("The following fields were changed:").ul()
for field in site['fields']:
fl.li(field)
if 'new_initiatives' in site:
il = sl.li("Not in the following initiatives in WebSTA-Q").ul()
for init in site['new_initiatives']:
il.li(init)
if 'old_initiatives' in site:
il = sl.li("In the following initiatives in WebSTA-Q, but not CiviCRM:").ul()
for init in site['old_initiatives']:
il.li(init)
if 'added_grants' in site:
il = sl.li("The following grants were added:").ul()
for grant in site['added_grants']:
il.li(grant)
if 'removed_grants' in site:
il = sl.li("The following grants were removed:").ul( )
for grant in site['removed_grants']:
il.li(grant)
def main(args):
'''
Usage:
Send notification for an event:
notification event eventtype> message [key1=value1 [key2=value2]]
Send a Pushover notification to a set of user(s)
notification pushover user[,user2] message [key1=value1 [key2=value2]]
Send an Email to a set of users
notification email user[,user2] message [key1=value1 [key2=value2]]
'''
try:
from helpers import split_params
if args[0] == 'event':
event = args[1]
message = args[2]
args = split_params(args[3:])
notify(event, message, **args)
elif args[0] == 'pushover':
users = args[1].split(',')
message = args[2]
args = split_params(args[3:])
for user in users:
send_pushover(user, 'adhoc', message, **args)
elif args[0] == 'email':
users = args[1].split(',')
message = args[2]
args = split_params(args[3:])
for user in users:
send_email(user, 'adhoc', message, **args)
except:
print main.__doc__
if __name__ == '__main__':
import sys
main(sys.argv[1:])