Skip to content

Instantly share code, notes, and snippets.

@8q
Created October 7, 2018 12:24
Show Gist options
  • Save 8q/f6c2fdb4b68fe03e65aa42be8bfda3f4 to your computer and use it in GitHub Desktop.
Save 8q/f6c2fdb4b68fe03e65aa42be8bfda3f4 to your computer and use it in GitHub Desktop.
GASによるサーバーの死活監視Slackボット
function isServerAlive() {
var serverUrl = PropertiesService.getScriptProperties().getProperty('SERVER_URL');
var options = {
muteHttpExceptions: true,
};
var res = UrlFetchApp.fetch(serverUrl, options);
return res.getResponseCode() == 200;
}
function postMessage(msg) {
var methodUrl = 'https://slack.com/api/chat.postMessage';
var token = PropertiesService.getScriptProperties().getProperty('SLACK_API_TOKEN');
var channel = PropertiesService.getScriptProperties().getProperty('SLACK_CHANNEL');
var headers = {
Authorization: 'Bearer ' + token,
}
var data = {
channel: channel,
text: msg,
as_user: true,
};
var options = {
method: 'post',
headers: headers,
contentType: 'application/json',
payload: JSON.stringify(data),
};
var res = UrlFetchApp.fetch(methodUrl, options);
Logger.log(res.getContentText());
}
function main() {
if(!isServerAlive()) {
postMessage('<@U7Y6FLENM> sayakachan.net がダウンしてるよ!');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment