Skip to content

Instantly share code, notes, and snippets.

@mariovalney
Created August 6, 2019 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariovalney/9a1ffe2a59db583fc101c450097f1061 to your computer and use it in GitHub Desktop.
Save mariovalney/9a1ffe2a59db583fc101c450097f1061 to your computer and use it in GitHub Desktop.
Monitor a API Endpoint to check JSON and sent data to Slack
"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