-
Notifications
You must be signed in to change notification settings - Fork 0
/
911-poller.js
48 lines (41 loc) · 1.57 KB
/
911-poller.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
exports.myHandler = function (event, context) {
var RTM = require("satori-rtm-sdk");
var request = require("request");
var endpoint = "";
var appKey = "";
var role = "";
var roleSecret = "";
var channelName = "seattle-911-incident-response";
// Check if the role is set to authenticate or not
var shouldAuthenticate = false;
var authProvider;
if (shouldAuthenticate) {
authProvider = RTM.roleSecretAuthProvider(role, roleSecret);
}
var rtm = new RTM(endpoint, appKey, { authProvider: authProvider });
rtm.on("enter-connected", function () {
console.log("Connected");
var data = request("https://data.seattle.gov/resource/pu5n-trf4.json", function (error, response, body) {
var parsedResponse = JSON.parse(body);
for (var i = 0; i < parsedResponse.length; i++) {
publishToSatoriChannel(parsedResponse[i]);
}
});
});
console.log("Starting RTM");
rtm.on("error", function (error) {
console.writeln("Error connecting to RTM: " + error.message);
rtm.stop();
});
rtm.start();
var publishToSatoriChannel = function (objectToPush) {
rtm.publish(channelName, objectToPush, function (pdu) {
if (pdu.action.endsWith("/ok")) {
// Publish is confirmed by Satori RTM.
console.log("Published data: " + JSON.stringify(objectToPush));
} else {
console.log("Publish request failed: " + pdu.body.error + " - " + pdu.body.reason);
}
});
}
}