Skip to content

Instantly share code, notes, and snippets.

@thealanberman
Created July 22, 2022 04:26
Show Gist options
  • Save thealanberman/3ecbb2c48f62198859a053fc9de9e60b to your computer and use it in GitHub Desktop.
Save thealanberman/3ecbb2c48f62198859a053fc9de9e60b to your computer and use it in GitHub Desktop.
Google App Script to send a "post a pic" message twice a day
// Randomized posting of 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': 'slicebot',
'text': '@channel Time to post a picture! Just whatever is around you. Mundane snapshots of life welcome.'
}
// get URL from Script Properties key-value store
var mm_url = PropertiesService.getScriptProperties().getProperty('communitas_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(mm_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