DEV Community 👩‍💻👨‍💻

Kenta Takeuchi
Kenta Takeuchi

Posted on • Originally published at bmf-tech.com

Implement Slash Command for Slack App using Cloud Functions

This articles is a translation of Cloud Functionsを使ってSlack AppのSlash Commandを実装

Overview

Implement Slash Command for Slack App using Cloud Functions.

Here is the boilerplate I made.

go-slack-app-on-cloud-functions-boilerplate

There are various ways to create a Slack App that can use Slash Command, but I decided to use CloudFront Functions because it's cheap, easy, and serverless.

premise

  • Availability of Google Cloud Platform
  • gcloud command setup completed
    • the gcloud command must be available to deploy the application
  • Cloud Build API enabled
    • I need to build a function to deploy it to Cloud Functions.

create a function in Cloud Functions

Create a function in the Cloud Functions console.
Select HTTP for the trigger type, Allow unauthenticated calls for authentication, and check HTTPS required.

Make a note of the trigger URL described in Cloud Functions Function Details > Triggers later after deploying the function.

Preparing the Slack App

Create a Slack App

In Create an app, press From scratch.

Screenshot 2022-09-04 17 29 50

Enter App Name.

Select a workspace with Pick a workspace to develop your app in:
Press Create App.

Screenshot 2022-09-04 17 32 07

Set Slash Command

On the setting screen (ex.https://api.slack.com/app/*****), select Slash Commands.

Screenshot 2022-09-04 17 33 21

Press Create New Command and optionally enter Command, Short Description, Usage Hint, Escape channels, users, and links sent to your app.

For Request URL, enter the trigger URL you made a note of earlier.
The trigger URL has the format https://REGION-NAME-PROJECT-ID.cloudfunctions.net/FUNCTION_NAME.

Screenshot 2022-09-04 17 35 59

After entering, press Save.

Install the Slack App

On the setting screen (ex. https://api.slack.com/apps/*****), press Install App.

Screenshot 2022-09-04 17 37 16

Press Install to workspace to install Appwo in any workspace.

Get the Signing Secret

On the setting screen (ex. https://api.slack.com/apps/*****), press Basic Information.

There is Signing Secret in the item called App Credentials, so make a note of the value.

implement the function

Implement functions that deploy to Cloud Functions.

There are some addictive parts (such as the go mod vendor part), but I will omit the implementation details.

See below for the source code.

go-slack-app-on-cloud-functions-boilerplate

deploy the function

Prepare environment variables according to the README of go-slack-app-on-cloud-functions-boilerplate and deploy.

Operation check

Try using the created Slash Command in Slack.

ex.
/hello Bob

Screenshot 2022-09-04 17 47 11

Impression

I'd love to be able to code the part that makes a Slack App.

postscript

I wanted to create a Slack Command for attendance management at work go-slack-app-on-cloud-functions-boilerplate, I made akashi-slack-slash-command.

In this implementation, the storage is Spreadsheet, but when using Google Workspace, there is a management problem that Spreadsheet sharing settings cannot be flexibly adjusted around permissions, so at work, the storage is replaced from Spreadsheet to Cloud Storage I adjusted the implementation and used it.

If there is an organization that uses Akashi for attendance management and Slack for chat tools, I think Slack Command can be used easily.

It doesn't cost much to operate, and the scalability may be a bit questionable.

I think that it can probably be used without problems unless it is an organization with more than a few thousand people.

Top comments (0)

Top Heroku Alternatives (For Free!)

Recently Heroku shut down free Heroku Dynos, free Heroku Postgres, and free Heroku Data for Redis on November 28th, 2022. So Meshv Patel put together some free alternatives in this classic DEV post.