Skip to content

Instantly share code, notes, and snippets.

@gusari
Last active December 28, 2016 07:27
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 gusari/a1b3c168b63696152a0d97d3ff262150 to your computer and use it in GitHub Desktop.
Save gusari/a1b3c168b63696152a0d97d3ff262150 to your computer and use it in GitHub Desktop.
function lineErrorWatch(){
var token = PropertiesService.getScriptProperties().getProperty('SLACK_ACCESS_TOKEN');
var slackApp = SlackApp.create(token);
var channelId = "#xxx" // put your slack channel id here
var slackId = "xxx"; // put your slack id here
var boticon = "http://a274.phobos.apple.com/us/r30/Purple2/v4/72/47/a9/7247a999-3323-2063-e0d4-0cf48a8a2f82/mzl.nfffibwl.png"
var start = 0;
var max = 100;
var filterString = 'label:line is:unread';
var threads = GmailApp.search(filterString,start,max);
var isToBreakOuterLoop = false;
for(var i = 0; i < threads.length; i++) {
var thread = threads[i];
var msgs = thread.getMessages();
//Logger.log(msgs.length);
for(var j = 0; j < msgs.length; j++){
var msg = msgs[j];
if (!msg.isUnread()) continue;
var body = msg.getBody();
if (!body) continue;
//Logger.log(body);
var matches = body.match(/Error count\xa0\xa0<span style="color:#333333">([0-9]+)<\/span>/);
var channelNameMatches = body.match(/Channel name\xa0\xa0<span style="color:#333333">(.*)<\/span>/);
var channelName = "unknown";
if (channelNameMatches && channelNameMatches.length >= 2) channelName = channelNameMatches[1];
if (!matches || matches.length < 2) continue;
var errorNum = parseInt(matches[1]);
//Logger.log(errorNum);
if (errorNum >= 5) {
if (channelName.indexOf("テスト") != -1) {
slackApp.postMessage("#dora_dev", "「" + channelName + "」から直近5分で最低" + errorNum + "つのエラーが確認されています", {username: "linebot" , icon_url: boticon});
} else {
slackApp.postMessage("#dora_dev", "「" + channelName + "」から直近5分で最低" + errorNum + "つのエラーが確認されています<@slackId>", {username: "linebot" , icon_url: boticon});
}
isToBreakOuterLoop = true;
break;
}
}
if(isToBreakOuterLoop) { break; }
Utilities.sleep(1000);
}
threads.forEach(function(thread){ thread.markRead(); } );
}
@gusari
Copy link
Author

gusari commented Dec 28, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment