Last active
September 30, 2015 23:40
-
-
Save logemann/28296f29bb66617309d9 to your computer and use it in GitHub Desktop.
warningday
This file contains hidden or 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
module['exports'] = function warningday(hook) { | |
console.log("Starting Hook: "+hook.params.hook); | |
var channel = "general"; | |
var request = require('request'); | |
var store = hook.datastore; | |
var keyName = hook.params.hook+"-lastRunDateMillis"; | |
// disable date compare | |
store.del(keyName, function() {}); | |
var jsonRequest = jsonRequest = createJson("#"+channel, "Entwickler aufgepasst!", "", "", | |
"Heute ist Kill-the-Warnings-Freitag. Jeder muss mindestens 20 Warnings killen.", | |
hook.env.PICTURE_BASE_URL + "/warnings.png"); | |
store.get(keyName, function(error, result) { | |
if (error) { | |
hook.res.end("Fucked up :" + error.message); | |
} else { | |
var now = new Date(); | |
var lastRun = new Date(result); | |
if (isSameDay(now, lastRun)) { | |
hook.res.end("Job already fired today"); | |
} else { | |
var millisecsSince1970 = now.getTime(); | |
store.set(keyName, millisecsSince1970, function (error, result) { | |
if (error) { | |
console.log(error); | |
hook.res.end("Error setting var in DS", error.message); | |
} | |
var resp = request( | |
{ | |
method: 'POST', | |
uri: hook.env.SLACKBOT_URL + "&channel=%23" + channel, | |
body: "@everyone aufgepasst." | |
}, function (error) { | |
if (error) { | |
hook.res.end("Ended with error: " + error); | |
} | |
var resp = request( | |
{ | |
method: 'POST', | |
json: true, | |
uri: hook.env.SLACK_WEBHOOK_URL, | |
body: jsonRequest | |
}, function (error, response, body) { | |
if (error) { | |
hook.res.end("Ended with error: " + error); | |
} | |
hook.res.end("normally ended"); | |
}); | |
}); | |
}); | |
} | |
} | |
}); | |
}; | |
function createJson(channel, author, title, titlelink, body, image) { | |
return { | |
"channel": channel, | |
"username": "Reminder-BOT", | |
"icon_emoji": ":gun:", | |
"attachments": [{ | |
"image_url": image, | |
"fallback": "", | |
"color": "#990000", | |
"author_name": author, | |
"title": title, | |
"title_link": titlelink, | |
"text": body | |
}] | |
} | |
} | |
function isSameDay(date1, date2) { | |
return date1.getFullYear() === date2.getFullYear() | |
&& date1.getDate() === date2.getDate() | |
&& date1.getMonth() === date2.getMonth(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment