Created
August 9, 2022 20:05
-
-
Save thealanberman/b8b2060625a1610a805bb9257aff8279 to your computer and use it in GitHub Desktop.
Slice of Life slackbot. Prompts twice a day for everyone in the channel to post a picture.
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
// INSTRUCTIONS | |
// Set the Incoming webhook URL in the Script Properties key-value store | |
// give it the key name "slack_url" | |
// Set a time-based Trigger of the 'DelayedTrigger' function. Have it trigger at 0800 in the desired timezone | |
// The script will post 2 messages: | |
// One between between 8am and 3pm | |
// The other between 3pm and 10pm | |
function DelayedTrigger() { | |
// for readability | |
var minutes = 60 * 1000 | |
// There are 840 minutes between 8am and 10pm | |
var morningDelay = randomInteger(1,420) | |
var eveningDelay = randomInteger(421,840) | |
const now = new Date() | |
var morningAlertTime = new Date(now.getTime() + (morningDelay * minutes)) | |
var eveningAlertTime = new Date(now.getTime() + (eveningDelay * minutes)) | |
Logger.log("morningDelay=" + morningDelay) | |
Logger.log("morning alert " + Utilities.formatDate(morningAlertTime,"CST", "yyyy-MM-dd HH:mm:ss")) | |
Logger.log("eveningDelay=" + eveningDelay) | |
Logger.log("evening alert " + Utilities.formatDate(eveningAlertTime,"CST", "yyyy-MM-dd HH:mm:ss")) | |
// create morning and evening triggers | |
ScriptApp.newTrigger("RandomAlert") | |
.timeBased() | |
.after(morningDelay * minutes) | |
.create(); | |
ScriptApp.newTrigger("RandomAlert") | |
.timeBased() | |
.after(eveningDelay * minutes) | |
.create(); | |
} | |
function RandomAlert() { | |
var randomMessage = { | |
'username': 'slice-of-life-bot', | |
'icon_emoji': ':camera:', | |
'channel': '#slice-of-life', | |
'text': 'Time to post a picture! Just whatever is around you. Mundane snapshots of life welcome. @here' | |
} | |
// get URL from Script Properties key-value store | |
var slack_url = PropertiesService.getScriptProperties().getProperty('slack_url') | |
// set the HTTP options, including the JSON payload | |
var options = { | |
'method' : 'post', | |
'payload' : randomMessage, | |
'contentType': 'application/json', | |
'payload' : JSON.stringify(randomMessage) | |
} | |
// send the payload | |
UrlFetchApp.fetch(slack_url, options); | |
} | |
// function to generate a random integer between 'min' and 'max' | |
function randomInteger(min, max) { | |
return Math.floor(Math.random() * (max - min + 1)) + min; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment