-
Notifications
You must be signed in to change notification settings - Fork 0
/
tagalert.py
128 lines (116 loc) · 3.14 KB
/
tagalert.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
#!/usr/bin/python3
import json
import sys
import emails
import requests
API_ROOT = 'https://api.churchsuite.co.uk/v1'
CONTACT_URL_TEMPLATE = (
'https://{account}.churchsuite.co.uk/modules/'
'addressbook/contact_view.php?id={contact_id}'
)
TAG_REPORT_TEMPLATE = (
'https://{account}.churchsuite.co.uk/modules/'
'addressbook/tag_view.php?id={tag_id}'
)
def search_tag(config):
tag_url = "{}/addressbook/tag/{}?contacts=true".format(
API_ROOT,
config['tagid'],
)
response = requests.get(
tag_url,
headers={
'X-Account': config['account'],
'X-Auth': config['apikey'],
'X-Application': 'tagalert',
'Content-Type': 'application/json',
}
)
if response.status_code == 200:
return response.json()
else:
print('Got {} response from {}'.format(
response.status_code,
tag_url,
))
print(response.text)
sys.exit(1)
def notify(config, tag_name, contacts):
contact_string = (
"<a href='{contact_url}'>"
"{contact[first_name]} {contact[last_name]}"
"</a>"
)
contact_html = '<br />'.join(
contact_string.format(
contact_url=CONTACT_URL_TEMPLATE.format(
account=config['account'],
contact_id=contact['id'],
),
contact=contact)
for contact in contacts
)
html = """<p>This is an automated email generated by 'tagalert'.</p>
<p>
Tag: <b>{tag_name}</b>
<br />
Matches: <b>{total}</b>
</p>
<p>
<a href="{tag_view}">View full tag report on ChurchSuite</a>
</p>
<p>
Match details:
<br />
{contacts}
</p>
""".format(
tag_name=tag_name,
total=len(contacts),
tag_view=TAG_REPORT_TEMPLATE.format(
account=config['account'],
tag_id=config['tagid'],
),
contacts=contact_html,
)
smtp = config['smtp']
message = emails.html(
html=html,
subject='ChurchSuite Tag Alert: {}'.format(tag_name),
mail_from=smtp.get('user'),
)
for address in config['subscribers']:
response = message.send(
to=address,
smtp={
"host": smtp.get('host'),
"ssl": smtp.get('ssl', False),
"port": smtp.get('port', 25),
"user": smtp.get('user', None),
"password": smtp.get('password', None),
}
)
print('Notifying {} ({})'.format(address, response.status_code))
if __name__ == "__main__":
try:
configfile = sys.argv[1]
except IndexError:
print('Please provide a JSON config file')
sys.exit(1)
with open(configfile, 'r') as f:
config = json.load(f)
data = search_tag(config)
contacts = data['contacts']
tag_name = data['name']
total = int(data['tag_no_contacts'] or 0)
if total <= 0:
print('No contacts found for tag: {}'.format(
tag_name,
))
else:
print('Found {} contacts for tag: {}'.format(
total,
tag_name,
))
notify(config, tag_name, contacts)
print('Done')