Create Your Own Custom Slash Command in Slack

Slack is a tool that allows teams to communicate and collaborate. One of its key features is the use of slash commands. These commands trigger specific actions or integrations within the platform.

While Slack provides you with several built-in slash commands, you can create custom commands using Slack’s API. You can then configure the custom command to perform a certain task when a user triggers it.

4

Why Create a Custom Slash Command in Slack?

Slack’s built-in slash commands may not cater to the specific needs of your team or organization. By creating custom slash commands, you can create a more personalized experience for your team. This in turn increases the team’s engagement.

Setting Up a Slack App

To follow through, you should be comfortable with thebasic usage of Slack. Sign in to your Slack account in the browser and create a new workspace. You will use this workspace to test your apps before you use them in your team’s workspace.

Open another tab and navigate to theSlack API website.

A person using slack on a laptop

Click on theCreate an appbutton. On the next page that appears, choose the option to create an appFrom scratch.

Name the app and select the workspace in which the app will operate.

Slack API homepage

Click theCreate Appbutton. This will redirect you to a page that contains the basic information about your app.

In the features section, navigate to theOAuth & Permissionsfeature, then scroll down toBot Token Scopes. Add achat:writescope for your app. This scope will allow your app to send messages to the workspace.

Option of creating an app on the Slack API website

Scroll up to theOAuth Tokens for Your Workspacesection, and click on theInstall to Workspacebutton.

On the next page that appears, click on theAllowbutton. This will install the app in your workspace.

Slack app naming page on the Slack API website

Creating a Custom Slash Command Using the Slack API

After setting up the Slack app, you need to create a custom command for the task you want to accomplish. For example, a slash command that returns a joke to the user. Navigate to theSlash Commandsfeature. Click on theCreate New Commandbutton.

Fill out theCommand,Request URL, andShort Descriptionof the command you want to create. The Request URL is the endpoint that Slack sends a POST request to when a user triggers the command. Use a localhost URL as a placeholder; you will switch this URL later.

Click onSave. When you save, Slack adds a commands scope for your app automatically. This scope allows your app to add shortcuts and slash commands that people in your workspace can use. Changing the scopes will require you to reinstall the app. Click on thereinstall your appbutton.

On the page that appears next, click allow reinstalling the app on your workspace.

Configuring the Slash Command’s Behavior

Launch any Python IDE.Create a new virtual environment. Create a new Python file and a new .env file. On the terminal, run the following command to install the required libraries.

Thepython-dotenvlibrary will allow you to load the environment variables in the .env file. Therequestslibrary lets you make HTTP requests andFlaskhandles incoming HTTP requests and return responses.Slack-bolthandles incoming Slack requests.

Open the .env file and create two environment variables. Name themBOT_TOKENandSIGNING_SECRETrespectively. Navigate to the Slack API website. Under your app’sBasic Informationcopy the signing secret and use it to initialize the BOT_TOKEN variable. Navigate to the OAuth & Permissions feature and copy the Bot User OAuth Token. Use it to initialize the SIGNING_SECRET variable.

The bot token is a unique identifier that authenticates your app with Slack’s API when making API calls. The signing secret verifies that incoming requests to your app are coming from Slack.

The full source code is available in aGitHub repository.

Import the required libraries and modules.

Create a new instance of a Flask application.

Load the environment variables from the .env file using theload_dotenv()function.

Create a new instance of a Slack app using theAppclass from the slack_bolt library.

Create a route handler function that handles incoming requests to the/slack/commandendpoint. When the endpoint receives aPOSTrequest, it parses the request body data. It then checks whether the command is/joke. If so, it calls theget_joke()function and returns a JSON response to Slack with the result of the command.

Create a function that sends a GET request to theicanhazdadjokeAPI to retrieve a random joke in JSON format. It then returns the joke as a string.

Create a new instance of aSlackRequestHandlerobject that will handle incoming requests from Slack.

Check whether the script is being run directly and, if it is, start the Flask application on port 5000.

Run the program to start the server running on localhost.

For the program to communicate with Slack, you need a URL that it can access over the internet. Download and runngrok. Thengrok software allows you to create a secure public URLthat tunnels traffic to a web server running on your computer.

Run the following command to get the public URL:

The image below shows the public URL on ngrok, highlighted in white.

Copy the URL and navigate to the Slack API website. Click on theSlash Commandsfeature. Edit the /joke command and replace the request URL placeholder with your public URL. Add /slack/events at the end of the URL to point to your endpoint.

Go to your workspace, then type and send the /joke command. You should see a joke in response:

Familiarize Yourself With Other Slack Features

Slack has a lot of features to offer beyond Slash commands. Familiarizing yourself with these features will enable you to understand how they work.

You can then look for ways to customize them to your workspace’s requirements. This will increase the productivity of your team.

Want to know what are mentions in Slack? When and how can you use them effectively? Here’s everything you need to know.

If an AI can roast you, it can also prep you for emergencies.

This small feature makes a massive difference.

The key is not to spook your friends with over-the-top shenanigans.

These are the best free movies I found on Tubi, but there are heaps more for you to search through.

you may block out the constant surveillance and restore your privacy with a few quick changes.

Technology Explained

PC & Mobile