Created
August 6, 2019 13:39
-
-
Save mariovalney/9a1ffe2a59db583fc101c450097f1061 to your computer and use it in GitHub Desktop.
Monitor a API Endpoint to check JSON and sent data to Slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const API_URL = "https://api.example.com/your-endpoint"; // Your API request | |
const SLACK_URL = ""; // Check: https://api.slack.com/incoming-webhooks | |
const NOTIFICATION = {text: "ITEM I WANT CHANGED!"}; // Message sent to Slack | |
const INTERVAL = 60000; // Interval in miliseconds | |
const interval = setInterval(() => { | |
fetch(API_URL) | |
.then(response => { | |
return response.json(); | |
}) | |
.then(data => { | |
/** | |
* Check your JSON data here | |
* Example: | |
*/ | |
for (var i = 0; i < data.length; i++) { | |
if (data[i].title !== 'ITEM I WANT TO CHECK') continue; | |
if (data[i].property === 'not this') continue; | |
/** | |
* Finally send data to Slack | |
*/ | |
const request = { | |
method: "POST", | |
body: JSON.stringify(NOTIFICATION) | |
}; | |
fetch(SLACK_URL, request) | |
.then(res => { | |
alert(NOTIFICATION); // Redundancy is good. | |
clearInterval(interval); // Finish after sent data. | |
}); | |
} | |
}) | |
}, INTERVAL); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment