Last active
August 2, 2019 13:12
-
-
Save AWtnb/423557a05d28d1282e4adfcdd51fe7cc to your computer and use it in GitHub Desktop.
training manager on slack
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
/* | |
今日のトレーニングを slack で確認してカレンダーに登録 | |
incomming webhook | |
interactive component | |
*/ | |
/////////////////////////////////////////////////////// | |
// global variables | |
/////////////////////////////////////////////////////// | |
var PROPs = PropertiesService.getScriptProperties(); | |
var CHANNEL_ID = PROPs.getProperty("CHANNEL_ID"); | |
var SHEET_ID = PROPs.getProperty("SHEET_ID"); | |
var sht = SpreadsheetApp.openById(SHEET_ID).getSheets(); | |
var WEBHOOK_URL = PROPs.getProperty("WEBHOOK_URL"); | |
var CALENDAR_ID = PROPs.getProperty("CALENDAR_ID"); | |
var clndr = CalendarApp.getCalendarById(CALENDAR_ID); | |
/////////////////////////////////////////////////////// | |
// slack | |
/////////////////////////////////////////////////////// | |
function send2slack(text, block, url) { | |
var payload = { | |
"text": text, | |
"blocks": block | |
} | |
var options = { | |
"method" : "POST", | |
"payload": JSON.stringify(payload) | |
} | |
UrlFetchApp.fetch(url, options); | |
} | |
function makeButton (buttonText, val) { | |
return { | |
"type": "button", | |
"text": { | |
"type": "plain_text", | |
"text": buttonText, | |
"emoji": true | |
}, | |
"value": val | |
} | |
} | |
function checkOnSlack () { | |
var text = "キントレ デキタカ?"; | |
var blocks = [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": text | |
} | |
}, | |
{ | |
"type": "actions", | |
"elements": [ | |
makeButton("できた", "done"), | |
makeButton("できなかった", "not") | |
] | |
} | |
]; | |
send2slack(text, blocks, WEBHOOK_URL); | |
} | |
/////////////////////////////////////////////////////// | |
// interactive reaction | |
/////////////////////////////////////////////////////// | |
function doPost (e) { | |
var payload = JSON.parse(e.parameter.payload); | |
var response_url = payload.response_url; | |
var response_type = payload.actions[0].type; | |
var selected = payload.actions[0].value; | |
var counter = 0 | |
if (selected == "done") { | |
var lastCounter = sht[0].getRange(sht[0].getLastRow(), 2).getValue(); | |
counter = Number(lastCounter) + 1; | |
} | |
sht[0].appendRow([new Date(), counter]); | |
var msg = "ザンネン キロク ヤリナオシ……"; | |
if (counter > 0) { | |
msg = "キロク コウシン!(" + counter + "ニチメ)"; | |
} | |
var block = [ | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": msg | |
} | |
} | |
]; | |
send2slack(msg, block, response_url); | |
} | |
/////////////////////////////////////////////////////// | |
// カレンダーに登録 | |
/////////////////////////////////////////////////////// | |
function recordOnCalendar () { | |
var lastCounter = sht[0].getRange(sht[0].getLastRow(), 2).getValue(); | |
if (lastCounter == 0) { | |
return; | |
} | |
var record = "運動(" + lastCounter + "日目)"; | |
var d = new Date(); | |
d.setDate(d.getDate() - 1); | |
clndr.createAllDayEvent(record, d); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment