Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure Google Apps Script callback for Slack slash command

TL;DR - I would like to setup a Slack slash command that works on Google Apps Script. How do you recommend I do so?

Context:

I currently use google.com/script with Sheets to trigger a webhook call to Slack when someone fills out a Google Form. Here it is for reference. My question is an additional feature I'd like to add that won't overlap with this code.

I want to listen in Google Apps for a Slack slash command, so I've been looking through tons of examples of callbacks. I don't understand the dance necessary to get this going. Here are the variables:

  • I've setup the Slack slash command, which asks for a URL and method type (POST or GET), then gives me a token for verification of the outgoing payload.
  • I've setup OAuth 2.0 client IDs on Google APIs, which provides a client ID and secret token.
  • The Google Apps tutorial here offers https://script.google.com/macros/d/{SCRIPT ID}/usercallback, but if I'm reading it correctly in this guide it would require user authorization

I've reviewed other questions that mention callback (this and this) but haven't found anything relevant.

Guidance toward the next steps of just being able to configure a callback URL and understand how it will authentication and ultimately consume the API call from Slack in Google would be awesome.

like image 455
mbb Avatar asked Aug 02 '26 05:08

mbb


1 Answers

You are on the right track, i'm fiddling around with this since couple of weeks so maybe this helps:

Google App Script

  1. Publish > Deploy as Web App > Version: New / Execute the App as : Me / Who has access: Anyone Even Anonymous
  2. You'll get a url for that web app something like https://script.google.com/macros/s/xxx-ver-long-number-xxx/exec
  3. Copy that

Slack Integrations

  1. Create new Slash Command

  2. Method POST / URL: Paste The url from your web app

  3. Copy the Secure Token, you need it in the Google App Script

  4. Set all the other settings as you like.

  5. Don't forget to save

Back to Google App Script

  1. Copy paste this basic stuff

     function doPost(request) {
    
     //// SET SECURITY TOKEN (FROM SLACK COMMAND)
      var your_token = "YOUR_SLASH_COMMAND_TOKEN"; 
      var output;
    
     //// GET PARAMETES FROM SLACK POST REQUEST
      var params = request.parameters;
    
     //// ... and store them into variables
      var cmd_token = params.token; // or params.token[0] idk
    
     //// CHECK RECEIVED TOKEN AGAINST YOUR SAVED TOKEN
      if (your_token == cmd_token) {
         output = {"text":"SUCCESS"};
      } else {
         output = {"text":"INVALID TOKEN"};
      }
    
     //// SEND RESPONSE BACK TO SLACK
     return ContentService.createTextOutput(JSON.stringify(output)).setMimeType(ContentService.MimeType.JSON);
     }
    
  2. Save script and again publish the script as web app (you have to do this everytime you change something, always choose "new" as version

Additional Info

This is the stuff you get from every slash command. In the example above I only use token ...

token=gIkuvaNzQIHg97ATvDxqgjtO
team_id=T0001
team_domain=example
channel_id=C2147483705
channel_name=test
user_id=U2147483697
user_name=Steve
command=/weather
text=94070
response_url=https://hooks.slack.com/commands/1234/5678

Works perfect for me, hope it helps :) Good luck !

like image 78
Pascal Lamers Avatar answered Aug 07 '26 01:08

Pascal Lamers