Handling Slack Slash Commands
Handling Slash Commands
Slack's slash command is a special event which mostly used as certain entry points for your slack app. A typical slash command message looks like: /todo ask @crushermd to bake a birthday cake for @worf in #d-social
.
To enable this feature, you need to:
- Configure slash command in your Slack app settings
- Handle slash command events in your slack bot
Configuring Slash Command in Your Slack App Settings
- Slack's slash command can be created in Slack Developer Console → \${YourApp} → Slash Commands → Create New Command
- Fill in fields in the form correspondingly and save it.
Command
: Your command text, must start with/
Request URL
: The webhook url of your slack bot.Short Description
: Description to explain what this command does.Usage Hint
: Hint text for arguments (Optional)
Handling Slash Command Events in Your Slack Bot
To determine whether the event is a slash command event, you may check the boolean value: context.event.isCommand
:
async function App(context) {
if (context.event.isCommand) {
// handling the slash command event
}
}
You can get the command from context.event.command
and its arguments from context.event.text
and use them in the reply:
async function App(context) {
if (context.event.isCommand) {
await context.sendText(
`I received slash command '${context.event.command}' with arguments: '${context.event.text}'`
);
}
}