How to Create Custom Slash Commands in Slack

Slack is a team productivity tool that comes with many configurations and services meant for customization. One of the best of its customizable features are Slash Commands. Slash Commands are essentially keywords that perform a certain action when you enter them as messages with a preceding forward slash symbol (/) in Slack.

10 Productivity Tools to Do More with Slack

10 Productivity Tools to Do More with Slack

Since its launch in 2013, Slack has grown to become a top team communication tool. Its availability across…Read more

Built-in Slash commands

The current set of built-in Slash commands performs an array of predefined functions in Slack. For instance, by entering the /away Slash Command into the message input box, you can toggle the Status of your account, as you can see it on the gif below.

Slack command, away

Custom Slash commands

You can also create custom Slash commands in Slack. Custom Slash commands allow developers to make secure HTTP requests to web applications or services, and receive relevant responses from them that are displayed as new Slack messages.

As an example, say there is a web service that returns the current score of a football match in plain text. You can create a Slash command to make a secure HTTP request to that web service, and display the score received as the response text in the Slack message window.

Create a Slash command

To create a Slash command, enter the https://[yourTeam] URL to your browser’s address bar, just replace the [yourTeam] placeholder with the name of your Slack team, and click the Slash Commands option.

Create new command

On the next screen, enter the keyword you want to use for your Slack Command (don’t use any space), then click the Add Slash Command Integration button. You can later edit this keyword if you want.

New Slash command form

On the next screen, you can configure the settings for your Slash command. You need to enter the URL of the web application you want to send your request to. You can also see a token value for the Slash command you’ve just created, which you can use in your web server to validate the request sent by Slack.

Slash command integration settings

Display the response

By default, the responses for the requests made by Slash commands are shown as plain text in the message window. If you’d rather show the messages in the Slack message format instead, the responses need to be in valid JSON format.

Also, by default, the response of a Slash Command is visible only to the user who entered that command, and not others in the channel. This can only be changed if the response is a valid JSON carrying the property called "response_type" with the value "in_channel", then all users in the channel can see the response. The "text" property holds the main text that will be shown in the message.

// Example JSON response
	"response_type": "in_channel",
	"text": "..."

As an example, I’ll display an ASCII art chessboard in response to my custom /lets-play Slash command I’ve created in the previous section, so as to demonstrate that you can display more complicated characters in the response as well. You can read more on Slack’s message formatting in the Slack API docs.

Below, you can see a screenshot of the JSON response my example Slash command will display, or you can find the same JSON file on Github.

Sample JSON

If you also want line breaks in your message, use the \n new line character in the value of the "text" property.

Run the command

Go to a channel or to the DM message window in Slack, and enter the new Slash command into the message input field. If you had ticked the Show this command in the autocomplete list checkbox in your Slash command settings, you’ll see your command in the suggestion list while typing.

If you hadn’t and you want to update this settings now, you can return to your Slash command settings page, and edit your configurations by clicking on the little pencil icon right to the command.

Below you can see a gif demo that demonstrates how my custom Slash command with the ASCII art chessboard works in Slack.

slash command demo