Required Skills
Basic understanding of Slack workflow, Incoming Webhooks configuration, and Google App Script.
Overview
This is a simple method of automatic posting to Slack. The reminder information registered in the spreadsheet is posted to the specified user in the Slack channel at the specified date and time.
Script Processing
The script is executed when the reminder date and time entered into the spreadsheet is exceeded at the time of script execution. The timing of posting depends on the script execution trigger, so it is recommended to have the trigger set arbitrarily, such as immediacy or sending a batch every morning.
Spread Sheet
Spreadsheet Contents. This sample only executes one reminder.
Post if Flag is Null.
How to use Reminder
Please fill in the following fields. Please leave Flag blank when registering.
Tip: If you want to mentions to a user group, register according to the following rules
Formatting text for app surfaces Mentioning groups
https://api.slack.com/reference/surfaces/formatting#mentioning-groups
Flag | NotificationDateTime | Message | UserID | ChannelID |
yyyy/MM/dd HH:mm:ss | Post Message | @username or @userid or !subteam^groupid | C12345678 |
Sample Script
This is a sample script.
A little log output is also left written to check for errors.
function sendReminders() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Reminder");
var data = sheet.getDataRange().getValues();
var now = new Date();
for (var i = 1; i < data.length; i++) { // Skip header row
var flag = data[i][0]; // Flag in column A
var notificationDateTimeStr = data[i][1]; // Notification execution date and time in column B
var message = data[i][2]; // Notification message in column C
var userId = data[i][3]; // Slack Mention Target UserID in column D
var channelId = data[i][4]; // Slack channel ID in column E
// Skip if notification execution date/time is empty
if (!notificationDateTimeStr) {
continue;
}
var notificationDateTime = new Date(notificationDateTimeStr);
if (flag !== "sent" && now >= notificationDateTime) {
var slackURL = "https://hooks.slack.com/services/url"; // Enter the Webhook URL here
var payload = {
"channel": channelId,
"text": message + " <" + userId + ">"
};
var options = {
"method": "post",
"contentType": "application/json",
"payload": JSON.stringify(payload)
};
// Log output for debugging You can turn this off.
Logger.log("URL: " + slackURL);
Logger.log("Payload: " + JSON.stringify(payload));
Logger.log("Options: " + JSON.stringify(options));
var response = UrlFetchApp.fetch(slackURL, options);
Logger.log("Response: " + response.getContentText());
// Update flag to "sent" to prevent re-execution
sheet.getRange(i + 1, 1).setValue("sent");
}
}
}