Last active
February 21, 2024 01:38
-
-
Save cm-kazup0n/b3c27e72057202ab094f6568212aa2e8 to your computer and use it in GitHub Desktop.
oncall_reimder.gas
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のBot Tokenをセット | |
// see https://note.com/crefil/n/n2b68b3c4aa6b | |
const slackBotToken = PropertiesService.getScriptProperties().getProperty("SLACK_BOT_TOKEN"); | |
// SlackチャンネルIDまたはチャンネル名 | |
const channel = ''; | |
const slackUserIds = { | |
'名前': 'ID' | |
} | |
// ここにスプレッドシートのIDを入力 | |
const spreadsheetId = '1_MPyjSEVG5w-dE9MrqhO0ROoNH2ULuHSbp2LR1KkXbk'; | |
function findTodaysAssignee() { | |
const today = new Date(); | |
const dayOfWeek = today.getDay(); // 0(日曜)から6(土曜)までの数値を取得 | |
// 土曜日(dayOfWeek = 6)または日曜日(dayOfWeek = 0)の場合は終了 | |
if (dayOfWeek === 0 || dayOfWeek === 6) { | |
Logger.log('今日は担当者がいません。'); | |
return null; | |
} | |
const sheetName = `${today.getFullYear()}年${today.getMonth() + 1}月`; | |
const sheet = SpreadsheetApp.openById(spreadsheetId).getSheetByName(sheetName); | |
var lastRow = sheet.getLastRow(); | |
for (var row = 2; row <= lastRow; row++) { | |
var dateCell = sheet.getRange(row, 1).getValue(); | |
var assigneeCell = sheet.getRange(row, 2).getValue(); | |
var assigneeDate = new Date(dateCell); | |
// 今日の日付と一致する場合、担当者をログに出力 | |
if ( | |
assigneeDate.getDate() === today.getDate() && | |
assigneeDate.getMonth() === today.getMonth() && | |
assigneeDate.getFullYear() === today.getFullYear() | |
) { | |
Logger.log('今日の担当者: ' + assigneeCell); | |
return assigneeCell; | |
} | |
} | |
Logger.log('今日は担当者がいません。'); | |
return null; | |
} | |
// Slackにメッセージを投稿する関数 | |
function postMessageToSlack(text) { | |
var apiUrl = 'https://slack.com/api/chat.postMessage'; | |
var payload = { | |
'channel': channel, | |
'text': text | |
}; | |
var options = { | |
'method': 'post', | |
'headers': { | |
'Authorization': 'Bearer ' + slackBotToken, | |
'Content-Type': 'application/json', | |
}, | |
'payload': JSON.stringify(payload) | |
}; | |
var response = UrlFetchApp.fetch(apiUrl, options); | |
var responseData = JSON.parse(response.getContentText()); | |
if (responseData.ok) { | |
Logger.log('Message sent successfully!'); | |
} else { | |
Logger.log('Error sending message: ' + responseData.error); | |
} | |
} | |
// メイン関数(テスト用) | |
function main() { | |
const assgignee = findTodaysAssignee(); | |
if(assgignee != null){ | |
const slackUserId = slackUserIds[assgignee]; | |
const text = `今日のoncall担当は<@${slackUserId}>です`; | |
Logger.log(text); | |
postMessageToSlack(text); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment