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.

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 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 How to Create Custom Slash Commands in Slack appeared first on Hongkiat.

Posted by WordPress Guru