-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathwarnings-slack.js
223 lines (176 loc) · 5.97 KB
/
warnings-slack.js
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import * as fs from "fs";
import jsonminify from "jsonminify";
// set up slack
import { WebClient } from "@slack/web-api";
const SLACK_TOKEN = process.env.SLACK_TOKEN;
const client = new WebClient(SLACK_TOKEN);
const SLACK_CHANNEL = process.env.SLACK_CHANNEL;
const three_ticks = "```";
// utility functions
const loadFile = async (file) => {
let rawdata = fs.readFileSync(file);
const data = JSON.parse(rawdata);
return data;
};
const saveFile = async (dir, filename, data) => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
const path = dir + filename;
const data_string = JSON.stringify(data);
const debug = fs.writeFileSync(path, data_string, (err) => {
if (err) throw err;
});
console.log(`${filename} saved.`);
return true;
};
// If the warning includes geospatial data, we can turn it into a map
// on geojson.io. This makes a URL that will display that map.
const makeMapURL = (warning) => {
// skip if no geometry
if (!warning.geometry) return null;
const geojson = {
type: "FeatureCollection",
features: [{ geometry: warning.geometry }],
};
// make the tiniset json possible, and urlencode it
const map_data = encodeURIComponent(jsonminify(JSON.stringify(geojson)));
const url = `https://geojson.io/#data=data:application/json,${map_data}`;
return url;
};
// Slack doesn't like long messages; this function will truncate the warning text if it's too long
const truncator = (text) => {
if (text.length < 200) return text;
return text.substring(0, 200) + "... [truncated]";
};
// This function builds the Slack message
const buildSlackMessage = (warning) => {
warning.slack = {
lines: [],
blocks: [],
};
warning.slack.lines.push(`:warning: *${warning.properties.event}*\n`);
warning.slack.lines.push(`*${warning.properties.headline}*\n`);
warning.slack.lines.push(
`Locations/counties included: *${truncator(warning.properties.areaDesc)}*`
);
if (warning.map_url) {
warning.slack.lines.push(`View warning <${warning.map_url}|on a map>`);
}
warning.slack.lines.push(`Warning time: *${warning.properties.effective}*`);
warning.slack.lines.push(`See thread :thread: for details ...`);
let blockText = "";
warning.slack.lines.forEach((message_part) => {
blockText += message_part + "\n";
});
const new_block = {
type: "section",
text: {
type: "mrkdwn",
text: blockText,
},
};
warning.slack.blocks.push(new_block);
return warning;
};
// This sends the slack messages
const sendSlack = async (warning) => {
if (!warning.slack.blocks) {
console.log("No blocks to send to slack.");
return null;
}
const message = {};
message.blocks = JSON.stringify(warning.slack.blocks);
message.channel = SLACK_CHANNEL;
message.text = warning.properties.event;
message.unfurl_links = false;
console.log("Sending message to Slack.");
console.log(message);
if (!SLACK_TOKEN) {
console.log(`-----`);
console.log(
`You haven't set a SLACK_TOKEN which probably means you're running on a local machine.`
);
console.log(`To use it locally, get your Slack bot token and then do:`);
console.log(`export SLACK_TOKEN= ... followed by the token`);
console.log(`-----`);
return;
}
//// SEND TO SLACK
try {
// Call the chat.postMessage method using the WebClient
const slack_response = await client.chat.postMessage(message);
// thread the details
if (slack_response.ts) {
var description;
if (warning.properties.description.length < 3000) {
description = warning.properties.description;
} else {
description =
warning.properties.description.slice(0, 2900) + " ... [Truncated]";
}
// send the description as a threaded message
const new_block = [
{
type: "section",
text: {
type: "mrkdwn",
text: `${three_ticks}${description}${three_ticks}`,
},
},
];
const threaded_message = {};
threaded_message.thread_ts = slack_response.ts;
threaded_message.blocks = JSON.stringify(new_block);
threaded_message.channel = SLACK_CHANNEL;
threaded_message.text = "Weather warning details";
const slack_response_2 = await client.chat.postMessage(threaded_message);
}
return true;
} catch (error) {
console.error(error);
process.exit(1);
}
};
// primary function
const main = async () => {
// read "seen" file of warning IDs we've seen before
const seen = await loadFile("./data/seen.json");
const current_warnings = await loadFile("./tmp/download.json");
console.log("- - - - - - -\n");
console.log(current_warnings.title);
console.log(`Last updated: `, current_warnings.updated);
// loop through current warnings
console.log(`${current_warnings.features.length} warnings in file...`);
for (let i = 0; i < current_warnings.features.length; i++) {
const warning = current_warnings.features[i];
// skip if this warning is not new
let matches = seen.indexOf(warning.properties.id);
if (matches > -1) {
// We have a match, so this one is not new
console.log(`Warning ${i + 1} is not new. Skipping.`);
continue;
}
console.log(`\n-+-+-+- NEW WARNING! -+-+-+-\n`);
console.log(`${warning.properties.event}`);
console.log(`${warning.properties.headline}`);
console.log(`Locations/counties included: ${warning.properties.areaDesc}`);
console.log(`\nWarning time: ${warning.properties.effective}`);
warning.map_url = makeMapURL(warning);
if (warning.url) {
console.log(`Map url: ${url}`);
}
const message = buildSlackMessage(warning);
// if there are blocks to send, send them to slack
if (message.slack.blocks.length > 0) {
const slack_response = await sendSlack(message);
seen.push(warning.properties.id);
}
}
await saveFile("./data/", "seen.json", seen);
console.log("Done!");
};
main().catch((err) => {
console.error(err);
process.exit(1);
});