Search code examples
google-apps-scriptslack

Slack command to Google Script returning Timeout Error


I setup a custom slash command to store data into a Google Spreadsheet. Everything works perfectly (the script gets triggered and does its magic) except the respond takes too long (more than the given max of 3000ms) and Slack throws me a timeout error.

simplified google script:

function doPost(request) {

   //// get data from slack payload
   var params = request.parameters;

   //// call a function with given parameters
   custom_function(params); 

   //// 
   var output = {"text":"SUCCESS"};

 //// respond to slacks POST request
 return ContentService.createTextOutput(JSON.stringify(output)).setMimeType(ContentService.MimeType.JSON);

}

Result: Due to the long execution time of custom_function(); the end return ContentService. ... comes too late (past 3000ms timelimit) = timeout error in slack

Additional Info: I setup delayed responses with UrlFetchApp.fetch(url,options); with the code of custom_function(); - I am receiving those responses in Slack together with the timeout error.

Question: Is there any way I DON'T have to wait until custom_function(); is finished and send some HTTP 200 OK back immediately ? The doPost(); in my case doesn't need anything from custom_function in return to finish so why wait ... ?

THANKS !


Solution

  • You can create a time based trigger to run the code in the future. Specifically the after method of ClockTriggerBuilder can run the code x milliseconds in the future.

    https://developers.google.com/apps-script/reference/script/clock-trigger-builder#after(Integer)

    function doPost(){
    
     //execute the the script function "myFunction" 100 ms in the future
     ScriptApp.newTrigger("myFunction")
        .timeBased()
        .after(100)
        .create();
    
     return ContentService.createTextOutput("OK");
    }