Created
July 10, 2017 16:16
-
-
Save froggr/d454fff7a802e410aed29967fdba0090 to your computer and use it in GitHub Desktop.
slack slash command to google apps script to spreadsheet
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
/******* | |
* This google apps scripts takes input from slack slash command and posts it to a google sheet. | |
* Lots of this came from various resources from the googles. Thx to all of those out there that provided instruction. | |
* If you see something of yours below, please comment and I'll add a link to your reference. | |
*********/ | |
function doPost(req) { | |
var params = req.parameters; | |
if (params.token == "[TOKEN HERE") { | |
var textRaw = String(params.text); | |
var scriptProperties = PropertiesService.getScriptProperties(); | |
scriptProperties.setProperties({ | |
'user_name': String(params.user_name), | |
'textRaw': textRaw, | |
}); | |
ScriptApp.newTrigger("updateSheet") | |
.timeBased() | |
.after(5) | |
.create(); | |
return ContentService.createTextOutput(JSON.stringify({"text" : "Thanks "+ params.user_name + ". Your update was: \n `" + textRaw + "`\n A confirmation of your update will be posted back once its been processed into Frog's Google Sheet. \n\n And just remember that *you are special* :smile: "})).setMimeType(ContentService.MimeType.JSON); | |
} else { | |
return; | |
} | |
} | |
function test(){ | |
var req = { "parameters" : { | |
"token" : "[TOKEN HERE]", | |
"user_name" : "a name", | |
"text" : "I am some text, I am some more text. I am the last text." | |
} | |
}; | |
return doPost(req); | |
} | |
function getNextRow(sheets) { | |
//this gets the next row in sheet with no timestamp | |
var timestamps = sheets.getRangeByName("timestamp").getValues(); | |
for (var i=0;i<timestamps.length;i++) { | |
if(timestamps[i][0] == "") { | |
return Number(i); | |
break; | |
} | |
} | |
} | |
function updateSheet(){ | |
var sheets = SpreadsheetApp.openById('[SPREADSHEET ID]'); | |
var sP = PropertiesService.getScriptProperties(); | |
var data = sP.getProperties(); | |
//splits on comma and period. then each array value is row in sheet | |
var text = data.textRaw.replace(/, /g, '. ').split(". "); | |
//hard coded list of users to substitute expexted username with propername | |
var user_list = {"username":"Proper Name"}; | |
var hoursArray = []; | |
for(var x=0; x<text.length; x++){ | |
var nr = getNextRow(sheets) +1; | |
//check if name is in username list otherwise use the username as fallback | |
var username = data.user_name; | |
if (user_list[data.user_name] != undefined){ | |
username = user_list[data.user_name]; | |
} | |
sheets.getRangeByName('timestamp').getCell(nr,1).setValue(new Date()); | |
sheets.getRangeByName('user').getCell(nr,1).setValue(username); | |
sheets.getRangeByName('update').getCell(nr,1).setValue(text[x]); | |
var hours = (text[x].length / data.textRaw.length) * 8.4; | |
var roundedHours = (Math.round(hours * 2) / 2).toFixed(1); | |
hoursArray.push(roundedHours); | |
sheets.getRangeByName('hours').getCell(nr,1).setValue(roundedHours); | |
} | |
var channel = "#_carnegie"; | |
postResponse(channel,data.user_name, text, hoursArray); | |
} | |
function postResponse(channel, userName, text, hoursArray) { | |
var updates = "update from " + userName + "* \n"; | |
var count = 0; | |
for(var x=0;x<text.length;x++){ | |
count++; | |
updates += "*" + count + ")* " + text[x] + " - [" + hoursArray[x] + "h] \n"; | |
} | |
var payload = { | |
"channel": channel, | |
"username": "[USERNAME for BOT]", | |
"icon_emoji": ":smile:", | |
"link_names": 1, | |
"attachments":[ | |
{ | |
"fallback": "update from " + userName, | |
"color": "#D00000", | |
"mrkdwn_in": ["fields"], | |
"fields": [ | |
{ | |
"value" : updates, | |
"short" : false | |
}] | |
} | |
] | |
}; | |
var url = '[INCOMMING WEBHOOK URL]'; | |
var options = { | |
'method': 'post', | |
'payload': JSON.stringify(payload) | |
}; | |
var response = UrlFetchApp.fetch(url,options); | |
var triggers = ScriptApp.getProjectTriggers(); | |
for (var i = 0; i < triggers.length; i++) { | |
ScriptApp.deleteTrigger(triggers[i]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment