Last active
December 6, 2016 05:20
-
-
Save nesheep5/e4a3fd72f24b7542dd3ec7d21c6f6041 to your computer and use it in GitHub Desktop.
すぐできる!Java100本ノックをSlackに自動投稿するBot ref: http://qiita.com/nesheep5/items/c6a50554199a910af578
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
| var URL = "https://github.com/JustSystems/java-100practices/tree/master/contents/"; | |
| var TOKEN = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; // 取得したトークンを貼り付け | |
| function main() { | |
| // 平日のみ投稿 | |
| var today = new Date(); | |
| if(isJapaneseHoliday(today) || today.getDay() == 0 ||today.getDay() == 6){return;} | |
| var qNumRange = SpreadsheetApp.getActiveSheet().getRange(1, 2); | |
| var qNum = qNumRange.getValue(); | |
| postSlackMessage(qNum); | |
| // 問題インクリメント | |
| qNum++; | |
| if(qNum > 100){ qNum = 1;} | |
| qNumRange.setValue(qNum); | |
| } | |
| function postSlackMessage(qNum) { | |
| var slackApp = SlackApp.create(TOKEN); //SlackApp インスタンスの取得 | |
| var options = { | |
| channelId: "#java100knock", | |
| userName: "Java100本ノックBot", | |
| message: "今日の問題はこちら!\n" + | |
| URL + ('000' + qNum).slice( -3 ) // 問題番号0埋め | |
| }; | |
| slackApp.postMessage(options.channelId, options.message, {username: options.userName}); | |
| } | |
| function isJapaneseHoliday(date) { | |
| var year = date.getFullYear(); | |
| var month = date.getMonth(); | |
| var day = date.getDate(); | |
| var startDate = new Date(); | |
| startDate.setFullYear(year, month-1, day); | |
| startDate.setHours(0, 0, 0, 0); | |
| var endDate = new Date(); | |
| endDate.setFullYear(year, month-1, day); | |
| endDate.setHours(23, 59, 59, 999); | |
| var cal = CalendarApp.getCalendarById("ja.japanese#holiday@group.v.calendar.google.com"); | |
| var holidays = cal.getEvents(startDate, endDate); | |
| return holidays.length != 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment