Created
October 7, 2018 12:24
-
-
Save 8q/f6c2fdb4b68fe03e65aa42be8bfda3f4 to your computer and use it in GitHub Desktop.
GASによるサーバーの死活監視Slackボット
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
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