Implement Slash Command for Slack App using Cloud Functions
Tags: go, slack, gcp, serverless
Published: 2022-09-04 08:56:59

This articles is a translation of [Cloud Functionsを使ってSlack AppのSlash Commandを実装](https://bmf-tech.com/posts/Cloud%20Functions%e3%82%92%e4%bd%bf%e3%81%a3%e3%81%a6Slack%20App%e3%81%aeSlash%20Command%e3%82%92%e5%ae%9f%e8%a3%85)

# Overview
Implement Slash Command for Slack App using Cloud Functions.

Here is the boilerplate I made.

[go-slack-app-on-cloud-functions-boilerplate](https://github.com/bmf-san/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](https://api.slack.com/apps?new_app=1), press `From scratch`.

<img width="721" alt="Screenshot 2022-09-04 17 29 50" src="https://user-images.githubusercontent.com/13291041/188304723-637b0b8a-6253-45db-86c9-17b33131444b.png">

Enter `App Name`.

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

<img width="714" alt="Screenshot 2022-09-04 17 32 07" src="https://user-images.githubusercontent.com/13291041/188304790-225cfb1c-2a31-4627-8f57-35b856b4aed8.png">

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

<img width="720" alt="Screenshot 2022-09-04 17 33 21" src="https://user-images.githubusercontent.com/13291041/188304841-db000433-2a20-4e4a-b303-9a2fac7e3e7b.png">

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`.

<img width="568" alt="Screenshot 2022-09-04 17 35 59" src="https://user-images.githubusercontent.com/13291041/188304929-a17ccbf4-3194-490e-ad65-12c77c5f324a.png">

After entering, press `Save`.

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

<img width="738" alt="Screenshot 2022-09-04 17 37 16" src="https://user-images.githubusercontent.com/13291041/188304972-f35057c1-7392-429e-90f0-b1ec02e096b0.png">

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](https://github.com/bmf-san/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](https://github.com/bmf-san/go-slack-app-on-cloud-functions-boilerplate) and deploy.

# Operation check
Try using the created Slash Command in Slack.

ex.
`/hello Bob`

<img width="489" alt="Screenshot 2022-09-04 17 47 11" src="https://user-images.githubusercontent.com/13291041/188305315-5fe063c2-971b-4b18-978a-719596c2bd87.png">

# 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](https://github.com/bmf-san/go-slack-app-on- cloud-functions-boilerplate), I made [akashi-slack-slash-command](https://github.com/bmf-san/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.