The Talent500 Blog
Implement Slack Slash Command in Golang using Socket Mode 1

Implement Slack Slash Command in Golang using Socket Mode

Slash commands are messages that begin with “/” and are entered into the dialogue box. They are simple to use and allow you to easily extend the functionality of Slack. They let users connect with your ecosystem by posting to Slack Channels, and your Slack application works in the background as an orchestrator to direct user requests to the right systems. We want to summarise them as functions that can be remotely triggered via Slack. This article contains a quick overview of the slash command as well as a detailed explanation of how to begin utilising socket mode in Golang. The major Slack API features shown are Slack Slash Commands, Ephemeral Messages, and Interactive Messages.

What can Slash Commands accomplish?

You might be familiar with Slack’s built-in commands. If not, enter any conversation, then type “/” once, then “/” again to display all of the commands that are presently available in your workspace.

With Slack, you may create equivalent commands for your application. You first subscribe using the Slack application’s backend. After listening to the orders that your users have provided, you may begin conversing with them. It’s an excellent strategy for creating a shortcut that consumers can quickly remember and utilise. /gipsy is without a doubt one of the most often used Slack Slash Commands in the office.

Implement Slack Slash Command in Golang using Socket Mode 2

Slash instructions are of two parts. For example, when you enter /rocket count=5, Slack divides the message into two parts:

  • /rocket is the command.
  • The text count is 5

The command segment assists you in routing the user’s request to the appropriate Service or Controller, and the text portion must be processed so that your application can understand it. You can anticipate key-value parameters if you create a traditional command-line interface. But be cautious; your user may type anything after the command, so anytime its parameters are erroneous, display a help message describing how the command line works. The good news is that Slack allows for interactivity; if a command is incorrect, you can also send an interactive message or create a modal to obtain the necessary information.

Here’s a rundown of the command you’ll be writing with this tutorial:

Implement Slack Slash Command in Golang using Socket Mode 3

Build Slack Applications in Golang using Socket Mode — Setting Up

Step 1

  1. Make a new application.
  2. Socket Mode should be enabled in the corresponding area.
  3. Register the OAuth scope that you require (chat:write — allows you to post messages in channels).
  4. Create a Slash Command.

This animated animation shows how to add a new Slask Command. this example is named as /rocket.

Step 2

Make a project structure. Each section that follows will concentrate on one of these files.

+ controllers
|`- slashCommandController.go
+ drivers
|`- Slack.go
+ views
|`+ slackCommandAssets
| | `- rocket.json
|`- slashCommandView.go
+ main.go

Step 3

  1. Make the driver/slack.go file.
  2. Make the main.go file.

Step 4: Creating the controller -> slashCommandController.go

The controller’s activity is depicted in the flow chart below:

  • When receiving a Slack Command/rocket, deliver an ephemeral interactive message.
  • Update the message to show the rocket launch countdown if the user authorises.

Implement Slack Slash Command in Golang using Socket Mode 4

Configuring the controller

To begin, we will create an initialization function Object() { [native code] }. This technique makes use of *socket mode. SocketmodeHandler is used to specify which events to receive and which functions will handle them. In this situation, we are paying attention to two events:

  • c.launchRocketAnnouncement will handle the Slash Command “/rocket.”
  • The interaction with our message’s approve button will be handled by c.launchRocket. This button is a component of an “Interactive Block” and is distinguished by an ActionID represented by the constant views. RocketAnnoncement ActionID. Use a constant here to ensure that the ActionID I’m listening for matches the one defined in the message we send when the user issues the /rocket command.

The following are the critical points to code when responding to rocket with an Ephemeral message:

  1. You must first cast the generic socketmode.
  2. You received an event in Slack.
  3. SlashCommand
  4. You must acknowledge receipt of the event. (Slack wants you to respond within 3 seconds.)
  5. You must create the message that will be sent. This will be covered later, with the function views. LaunchRocketAnnoncement.
  6. You must use the Slack API to deliver your message. There are two options for this activity. The first approach is to utilise the standard API chat. post message. The second option is to utilise the Slash Command’s Answer URL. (More on the Response URL in the following section)

Responding to a user click on the approve button

The method’s structure is identical to the previous one. If you can code one, you can certainly code this one.

See slashCommandController for further information.

Go for a complete code.

Further information on the Response URL

Response URL is a webhook that allows you to communicate with a channel up to 5 times in 30 minutes. Slack provides response URLs whenever a Slash command or an interactive message is activated.

You have two possibilities when utilising the Response URL:

  • Slack should respond to the channel so that everyone can see the Bot’s response. ResponseTypeInChannel
  • Reply with an ephemeral message, and only the person who initiated the command will see it.ResponseTypeEphemeral

In this scenario, we provide the following MsgOption in our PostMessage call:

It’s important to note that the Response URL allows you to post a message in a channel even if your Bot hasn’t been joined to it. Indeed, chat.postMessage only allows you to post a message to a channel that your Bot has been added to; otherwise, the error not in the channel is returned. In other words, a Response URL allows you to engage with a user notwithstanding the regular permission requirements (a limited number of times, of course, for security).

Other options to consider when creating commands:

  • Make a message appointment.
  • At the App Home, send the Message.
  • Launch a Modal with the trigger id from Slack. SlashCommand. This is an intriguing Slack feature.

Step 5: Create the View > slashCommandView.go

Last but not least, we must figure out how to create our Slack message. Slack has introduced Block-kit to assist us in creating messages without trial and error. To handle slack messages, save the Block-kit payload as a JSON file, then use a Go template to create my View and inject variables.

Manage Static Assets in Golang

In Block-kit, you may fine-tune the message here. The payload is saved in a JSON file named slackCommandAssets/annoncement.json.

The main points to remember when designing this View are: 

  1. When the template is rendered, the user name is included in the message. Views/utils.go is a simple utility function. The template arguments are the number of seconds in the countdown. The number is a unique identity for the approve button we discussed when constructing the controller.ActionID.
  2. Unmarshal the created message into a Slack Message.

The next step is to try the app

After completing the lesson successfully, you may launch your app:

./main.go should be executed.


Implementing a Slack Slash Command using Socket Mode in Golang is a straightforward process that can significantly improve the functionality of your Slack bot. By following the steps outlined in this guide, you can easily set up a Socket Mode connection, handle incoming slash command requests, and send responses back to Slack. With this integration, you can empower your team to streamline communication and automate tasks, making your workflow more efficient and productive. By leveraging the power of Golang and Socket Mode, you can create a powerful and flexible Slack bot that can help your team stay organized and focused on what matters most.



Afreen Khalfe

Afreen Khalfe

A professional writer and graphic design expert. She loves writing about technology trends, web development, coding, and much more. A strong lady who loves to sit around nature and hear nature’s sound.

Add comment