Last active
May 23, 2020 02:04
-
-
Save letswritetw/3f00ed30df54f766101053312437549a to your computer and use it in GitHub Desktop.
line-bot-gas-monitor
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
var token = 'LINE 的 Channel access token'; | |
// 回傳群組 ID,沒用時要註解起來 | |
function doPost(e) { | |
var message = JSON.parse(e.postData.contents); | |
var targetID = message.events[0].source.groupId; | |
var replayToken = message.events[0].replyToken; | |
var data = { | |
replyToken: replayToken, | |
messages: [ | |
{ | |
"type": "text", | |
"text": "群組ID為:" + targetID | |
}, | |
] | |
}; | |
var option = { | |
method: 'post', | |
headers: { Authorization: 'Bearer ' + token }, | |
contentType: 'application/json', | |
payload: JSON.stringify(data) | |
}; | |
UrlFetchApp.fetch('https://api.line.me/v2/bot/message/reply', option); | |
} | |
// 要監測的網址清單 | |
var monitors = [ | |
'https://www.google.com.tw/', | |
'https://www.microsoft.com/', | |
'https://www.facebook.com/' | |
]; | |
// LINE 機器人推播訊息 | |
function sendMessage(message) { | |
var data = { | |
to: '這邊填群組ID 或 個人ID', | |
messages: [ | |
{ | |
"type": "text", | |
"text": message | |
}, | |
] | |
}; | |
var option = { | |
method: 'post', | |
headers: { Authorization: 'Bearer ' + token }, | |
contentType: 'application/json', | |
payload: JSON.stringify(data) | |
}; | |
UrlFetchApp.fetch('https://api.line.me/v2/bot/message/push', option); | |
} | |
// 執行監測 | |
var limit = 20000; // 這邊是回應超過多久就推播,單位:毫秒 | |
function doMonitor() { | |
for(var i = 0, len = monitors.length; i < len; i++) { | |
var url = monitors[i]; | |
var start = new Date().getTime(); | |
var res = UrlFetchApp.fetch(url, { muteHttpExceptions: true }).getResponseCode(); | |
var end = new Date().getTime(); | |
var distance = end - start; | |
// 先檢查回應碼是否為 200 | |
if(res === 200) { | |
// 是 200 就計算時間 | |
var end = new Date().getTime(); | |
var distance = end - start; | |
if(distance > limit) { | |
sendMessage(url + '\n回應時間 ' + (Math.ceil(distance / 1000)) + ' 秒!回應碼:' + res) | |
} | |
} | |
// 不是 200 就發訊息 | |
else { | |
sendMessage(url + '\n回應碼異常:' + res) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment