This script acts as Slack Bot to post one tip once a day to a defined channel.
Last active
January 6, 2019 09:57
-
-
Save Pindar/3b1124b3ebf71426532442fec5d27243 to your computer and use it in GitHub Desktop.
This script acts as Slack Bot to post one tip once a day to a defined channel.
This file contains hidden or 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
{ | |
"scriptId":"", | |
"fileExtension": "ts" | |
} |
This file contains hidden or 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
{ | |
"timeZone": "Europe/Berlin", | |
"dependencies": { | |
}, | |
"exceptionLogging": "STACKDRIVER", | |
"urlFetchWhitelist": [ | |
"https://hooks.slack.com/XXX/XXX" | |
] | |
} |
This file contains hidden or 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
function setup() { | |
createTrigger(); | |
} | |
function main() { | |
tipOfTheDay(getProperty('SPREADSHEET_ID_1'), getProperty('CHANNEL_NAME_1')); | |
tipOfTheDay(getProperty('SPREADSHEET_ID_2'), getProperty('CHANNEL_NAME_2')); | |
} | |
function getProperty(key: string) { | |
const scriptProperties: GoogleAppsScript.Properties.Properties = PropertiesService.getScriptProperties(); | |
return scriptProperties.getProperty(key); | |
} | |
function tipOfTheDay(fileId: string, channel: string) { | |
const activeRange: GoogleAppsScript.Spreadsheet.Range = openAndActivateSpreadsheet(fileId); | |
const tips: string[] = getTips(activeRange); | |
const tipOfTheDay: string = getMessage(tips); | |
postMsgToSlack(tipOfTheDay, channel); | |
} | |
function openAndActivateSpreadsheet(fileId): GoogleAppsScript.Spreadsheet.Range { | |
const spreadsheet = SpreadsheetApp.openById(fileId); | |
const firstSheet = spreadsheet.getSheets()[0]; | |
return spreadsheet.setActiveSheet(firstSheet).getRange('A2:A100').activate(); | |
} | |
function getTips(range: GoogleAppsScript.Spreadsheet.Range) { | |
const numRows = range.getNumRows(); | |
let row = []; | |
for (let i = 1; i <= numRows; i++) { | |
let currentValue = range.getCell(i,1).getValue(); | |
if (!currentValue.length) { | |
break; | |
} | |
row.push(currentValue) | |
} | |
return row; | |
} | |
function getMessage(messages) { | |
return messages[Math.floor(Math.random() * messages.length)]; | |
} | |
function postMsgToSlack(message: string, channel: string) { | |
const formData = { | |
payload: `{"channel": "${channel}", "username": "TipOfTheDayBot", "text": "${message}", "icon_emoji": ":hoff:", "mrkdwn": true}` | |
}; | |
let options = { | |
'method' : 'post', | |
'payload' : formData | |
}; | |
UrlFetchApp.fetch(getProperty('SLACK_WEBHOOK_URL'), options); | |
} | |
function createTrigger() { | |
ScriptApp.newTrigger('main') | |
.timeBased() | |
.everyDays(1) | |
.atHour(9) | |
.create(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment