How to create custom slash command for slack workspace
Slack is great tool for communication between work team. Slack provides many features for users like, realtime messages, sharing files…
Slack is great tool for communication between work team. Slack provides many features for users like, realtime messages, sharing files, voice and video calling, screen sharing, allow users to create groups and channels for particular topics and work, connect slack with different work and project management tools like jira, gmail, calendar and many more other features and functionalities.
Slack also provides functionality for integrating and installing our custom apps for extend the slack functionality with our requirements. And they have very large number of apps in slack app directory, which maintain by slack community.
What is slash command in slack ?
The slash command is act like some shortcut for performing specific action in slack.
we can use slash command by typing ‘/’ in start of message or simply click on ‘+’ icon on very start of input box, this will open attachment and shortcut menu with slash command listed, click on any one command, type your message and send that message, and that message will act as a specific action associated with that slash command.
Slack provides many in-built slash command for messages and search users or messages in slack. you can find all listed in-built slash command here.
Sometimes, slack’s in-built slash command can’t fit or not useful to our use-case or requirement, in this case, slack provide the functionality to create our own slash command for workspace, that can fulfils our requirement. also we can distribute that command with other workspaces by creating custom App in slack.
How to create custom slash command in slack ?
To create our custom slash command, first we need to create custom app, that hold this custom slash command. so let’s first create our first slack app and slash command.
To create new slack app, first we need to visit https://api.slack.com/apps/
Click on create an App button, that will ask us to choose any one from two method to create new slack app.
Here we have two option,
The first option From scratch, when we want to create new app from very start and no need of any template or other app configuration.
The second option From an app manifest, this is used when we want to create app with some predefined template or other app configuration.
In upcoming articles, we will also go through in very details with create app from app manifest. for now, we will focus on the first method, From scratch. click on the first option, that will ask us to give app name and select our workspace, in which we want to add this app.
Click on create App, that will create an app and we will redirect on created app’s configuration page.
At here, our app is created, this page will hold all necessary information and credential that can use to connect and customize the app from backend using slack’s javascript SDK.
Now we need to add some features and functionality to our app, we have many option for features and functionalities like, Incoming webhooks, Interactive Components, Slash commands, Event subscriptions, Bots, Permissions. we can select whatever feature or functionality we want to add in our app.
We will go through each feature and functionalities in upcoming articles. For now we will focus on slash commands.
Click on Slash commands. and that will redirect us to other screen, that manage all slash command for this app.
Click on create new command. that will redirect us to create slash command screen where we can give slash command, its endpoint, description and other information.
The first thing we need to add is Command, that how we can call our command with slash ‘/’, you can select any name for the command, if you select same name of command that already created for your app, then this command’s action will overwrite with old command. so please, try to make this command as unique keyword.
The second thing is request URL, this is very important part of the command is whenever this command will trigger, this command will send a POST request to given request with given data with the command. so for this we need to create a API for that and we need to host that API on any server, so slack can send the request on that API and fetch the response from there.
So, let’s create one simple post request API for this command.
So here we created very basic post API for our slash command. we added a console for request body, so we can track that what we can get in request from slack and also as a response of that api, here I am sending some plain response with the message that I am getting from slash command. So whatever we will send with slash command, here I will respond with that same message to slack.
You can also give short description for this command and hint for how to use this command, so whenever we try to use the command, this hint will show, that how we can work with the command.
and at the bottom, we can see the preview of our created command.
click on save, so the new slash command is created 🎉 🎉 🎊
Click on basic information on left side menu, so we can move to next step
So we created new app and we also created our first slash command. so now its time to install this app in our workspace and test the command.
click on install to Workspace, It will ask some permission, allow the permission, so the app is install in our workspace.
As now, the app is installed in our workspace, its time to test the command.
so go on slack workspace and open any messages conversation, type ‘/’ and the command name, that we give the name to command. as you type first 2 or 3 char, you will get suggestion with our command, with short description and hist.
To use the command click on that and type anything (like below image) and press enter
As you hit enter, the next moment, the slack will send the request on command request webhook with given message and fetch the output and that output will added in message conversation list.
Here, you can see the response that we are sending from the api is listed on message conversation list.
While we get the request on out API side, remember we added a console to track the request from slack, now its time to check what we can get from slack request.
As you can see in log, that we are getting many things from the slack like, token, team_id, team_domain, channel_id, channel_name, user_id, username, command, text (message), api_app_id, is_enterprise_install, response_url, trigger_id.
you can use this information as per your requirement with slack.
So we successfully created out first slack custom app and first slash command in that app. 🎉 🎉 🎊
So, now you, Build your custom apps and custom slash command and build awesome integration with slack
In the upcoming articles on slack, we will explore many things related slack, bot, incoming webhooks, events and activities, custom design blocks and also we will explore slack javascript SDK to work with backend.
Stay tuned, follow, share and get notified
share your feedbacks, suggestions on
sagardspeed2@gmail.com and twitter @sagarDavara