I'm building a Slack App for staff doing daily checkins. Staff checkin using the /checkin
slash command in Slack. Slack requires a response to the slash command within 3 seconds. If it doesn't then it gives an operation_timeout error to the person who initiated the command.
In my setup the slash command fails the first time for every user, but always works the second time. Rather than rely on this issue going away, is there a way to make sure that the script responds within 3 seconds? Google Apps Script seems a bit slow with it. Even returning a simple response seems slow.
Can I return a quick response to Slack first, and then process the data afterwards? It seems my logUser function is taking longer to execute than the 3s. Or can it be run in a non-blocking way? But even with that, it does seem the response is unreasonably slow.
function doPost(request){
var params = request.parameters;
var user = params.user_id;
var text = params.text;
if(text == ''){
logUser(user);
return ContentService.createTextOutput(':white_check_mark: Checked In! Thank you! <@' + user + '> ');
}
}
Edit: I think I can use ScriptApp.newTrigger("logUser").timeBased().after(200).create();
to delay the running of logUser, to allow the message to be returned to Slack early. But then I lose the ability to give a parameter to the logUser function?
Edit: logUser
function as requested
function logUser(user){
var checkinSheet = getTodaySheet();
var name = getNameFromId(user);
var date = new Date();
var time = date.getTime();
checkinSheet.appendRow([name, user[0], "TRUE"]);
}
logUser is selecting the correct Google Sheet (we have one for every day), checking the SlackID of the person who initiated the Slack command against an employee spreadsheet, finding the name of the person who initiated it, and then posting that name into the correct Google Sheet. This can be time consuming, so I can understand why this blocks the response.
Can logUser be run after returning the response to Slack?
Use the PropertiesService
to store the user's name to the script's properties and then run the function logUser()
after you have returned to Slack.
You can store information directly as a script property and then use this in your logUser()
function to log the users in. It will require a little reworking of your current code but the bsaic run down will be as follows:
userId
as a script propertylogUser()
in an unspecified amount of time in the futureThen on trigger of logUser()
:
Firstly in your doPost()
:
function doPost(request){
var params = request.parameters;
var user = params.user_id;
var text = params.text;
if(text == ''){
PropertiesService.getScriptProperties().setProperty(user, "logged-in");
ScriptApp.newTrigger("logUser").timeBased().after(200).create();
return ContentService.createTextOutput(':white_check_mark: Checked In! Thank you! <@' + user + '> ');
}
}
Then for your logUser()
function:
function logUser(){
var checkinSheet = getTodaySheet();
var usersToLogIn = PropertiesService.getScriptProperties().getKeys();
var date = new Date();
var time = date.getTime();
usersToLogIn.forEach(function(user) {
var name = getNameFromId(user);
checkinSheet.appendRow([name, user[0], "TRUE"]);
PropertiesService.getScriptProperties().deleteProperty(user);
});
}