-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtagwatcher.rb
194 lines (156 loc) · 4.47 KB
/
tagwatcher.rb
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env ruby
require 'mail'
require 'json'
require 'nationbuilder'
require 'slack-notifier'
def get_old_tags(file)
Dir.mkdir('cache') unless File.exists?('cache')
Dir.mkdir('cache/email') unless File.exists?('cache/email')
Dir.mkdir('cache/slack') unless File.exists?('cache/slack')
if File.exist?(file)
return JSON.parse(File.open(file, 'r').read)
else
p file + " does not exist. Creating cache. No report will be sent."
file = File.new(file, 'w')
file.puts(Array.new.to_json)
return Array.new
end
end
def update_cache(all_tags, file)
file = File.open(file, 'w')
file.write(all_tags.to_json)
end
def get_new_tags(all_tags, old_tags)
return all_tags - old_tags
end
def get_tag_list(nation, api_key)
client = NationBuilder::Client.new(nation, api_key)
people_tags = client.call(:people_tags, :index, limit: 500)
page = NationBuilder::Paginator.new(client, people_tags)
tags = Array.new
while !page.nil?
page.body["results"].each do |tag|
tags.push(tag["name"])
end
page = page.next
end
return tags
end
def get_tagged_people(nation, api_key, tag)
client = NationBuilder::Client.new(nation, api_key)
people_tagged = client.call(:people_tags, :people, tag: URI.encode(tag), limit: 11)
people = Array.new
people_tagged["results"].each do |person|
people.push({
first_name: person["first_name"],
last_name: person["last_name"],
id: person["id"]
})
end
return people
end
def send_tag_report(nation, new_tags, from, to)
text = ''
html = '<ul>'
new_tags.each do |tag|
text << '* ' + tag + '\n'
html << '<li>' + tag + '</li>'
end
html << '</ul>'
Mail.defaults do
delivery_method :sendmail
end
Mail.deliver do
to to
from from
subject 'New tags in the ' + nation + ' nation'
text_part do
body text
end
html_part do
content_type 'text/html; charset=UTF-8'
body html
end
end
p "Tag report sent."
end
def send_tag_alert(nation, api_key, new_tags, webhook_url, channel, username)
notifier = Slack::Notifier.new webhook_url do
defaults channel: channel,
username: username
end
tags = Array.new
new_tags.each do |tag|
tagged_people = get_tagged_people(nation, api_key, tag)
people = Array.new
tagged_people.each do |person|
people.push("<https://" + nation + ".nationbuilder.com/admin/signups/" + person[:id].to_s + "|" + person[:first_name] + " " + person[:last_name] + ">")
end
tags.push({
fallback: '• ' + tag,
title: 'Tag',
text: tag,
color: "%06x" % (rand * 0xffffff),
fields: [
{
title: 'Tagged people',
value: people.join(", ")
},
{
title: 'More than 10 people tagged?',
value: (tagged_people.length > 10 ? 'Yes' : 'No')
}
]
})
end
notifier.post text: "New tag(s)", attachments: tags
p "Tag alert sent."
end
if __FILE__ == $0
NATION = ENV["NATION"]
API_KEY = ENV["API_KEY"]
if NATION.nil? || API_KEY.nil?
p "Invalid nation credentials. Usage: NATION=nation_slug API_KEY=api_key"
exit 1
end
MODE = ENV["MODE"]
if MODE.nil?
p "No mode specified. Usage: MODE=email|slack"
exit 1
elsif MODE != 'email' && MODE != 'slack'
p "Invalid mode specified. Valid options: email, slack"
exit 1
end
if MODE == 'email'
TO = ENV["TO"]
FROM = ENV["FROM"] ? ENV["FROM"] : ENV["TO"]
if TO.nil?
p "Invalid email configuration. Usage: TO=to_email_address (FROM=from_email_address)"
exit 1
end
elsif MODE == 'slack'
WEBHOOK_URL = ENV["WEBHOOK_URL"]
CHANNEL = ENV["CHANNEL"]
USERNAME = ENV["USERNAME"] || "NationBuilder Tag Alerts"
if WEBHOOK_URL.nil? || CHANNEL.nil?
p "Invalid Slack configuration. Usage: WEBHOOK_URL=slack_webhook_url CHANNEL=channel_name (USERNAME=bot_username)"
exit 1
end
end
FILE = ENV["FILE"] ? ENV["FILE"] : 'cache/' + MODE + '/' + NATION + '.json'
p "Generating report of new tags on the " + NATION + " nation."
all_tags = get_tag_list(NATION, API_KEY)
old_tags = get_old_tags(FILE)
new_tags = get_new_tags(all_tags, old_tags)
update_cache(all_tags, FILE)
if new_tags.any? && new_tags != all_tags
p new_tags.length.to_s + " new tags."
if MODE == 'email'
send_tag_report(NATION, new_tags, FROM, TO)
elsif MODE == 'slack'
send_tag_alert(NATION, API_KEY, new_tags, WEBHOOK_URL, CHANNEL, USERNAME)
end
else
p "No new tags"
end
end