Slash Commands | Chat SDK (original) (raw)
Handle slash command invocations and respond with messages or modals.
Slash commands let users invoke your bot with /command syntax. Register handlers with onSlashCommand to respond.
Slash commands are supported on Slack, Discord, and Telegram.
bot.onSlashCommand("/status", async (event) => {
await event.channel.post("All systems operational!");
});bot.onSlashCommand(["/help", "/info"], async (event) => {
await event.channel.post(`You invoked ${event.command}`);
});Register a handler without a command to catch all slash commands:
bot.onSlashCommand(async (event) => {
console.log(`Command: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>e</mi><mi>v</mi><mi>e</mi><mi>n</mi><mi>t</mi><mi mathvariant="normal">.</mi><mi>c</mi><mi>o</mi><mi>m</mi><mi>m</mi><mi>a</mi><mi>n</mi><mi>d</mi></mrow><mo separator="true">,</mo><mi>A</mi><mi>r</mi><mi>g</mi><mi>s</mi><mo>:</mo></mrow><annotation encoding="application/x-tex">{event.command}, Args: </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal">t</span><span class="mord">.</span><span class="mord mathnormal">co</span><span class="mord mathnormal">mman</span><span class="mord mathnormal">d</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">A</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">s</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span></span></span></span>{event.text}`);
});The event object passed to slash command handlers:
| Property | Type | Description |
|---|---|---|
| command | string | The command name (e.g., /status) |
| text | string | Arguments after the command |
| user | Author | The user who invoked the command |
| channel | Channel | The channel where the command was invoked |
| adapter | Adapter | The platform adapter |
| triggerId | string (optional) | Platform trigger ID for opening modals |
| openModal | (modal) => Promise<{ viewId: string } | undefined> | Open a modal dialog |
| raw | unknown | Platform-specific payload |
Use event.channel to post messages:
bot.onSlashCommand("/greet", async (event) => {
// Public message to the channel
await event.channel.post(`Hello, ${event.user.fullName}!`);
// Ephemeral message (only visible to the user)
await event.channel.postEphemeral(
event.user,
"This message is just for you!",
{ fallbackToDM: false }
);
});Use event.openModal() to open a modal in response to a slash command:
import { Modal, TextInput, Select, SelectOption } from "chat";
bot.onSlashCommand("/feedback", async (event) => {
const result = await event.openModal(
<Modal callbackId="feedback_form" title="Send Feedback" submitLabel="Send">
<TextInput id="message" label="Your Feedback" multiline />
<Select id="category" label="Category">
<SelectOption label="Bug" value="bug" />
<SelectOption label="Feature" value="feature" />
</Select>
</Modal>
);
if (!result) {
await event.channel.post("Couldn't open the feedback form. Please try again.");
}
});When a modal is opened from a slash command, the submit handler receives relatedChannel instead of relatedThread. Use this to post back to the channel where the command was invoked.
bot.onModalSubmit("feedback_form", async (event) => {
const { message, category } = event.values;
// Post to the channel where the slash command was invoked
if (event.relatedChannel) {
await event.relatedChannel.post(`Feedback received! Category: ${category}`);
}
});Telegram supports bot commands such as /status and /status@mybot. Register handlers with onSlashCommand; commands addressed to another bot are ignored as slash commands and continue through the normal message path.
Discord slash commands are received via HTTP Interactions — no Gateway connection is needed. The adapter automatically sends a deferred response to Discord, then resolves it when your handler calls event.channel.post().
Subcommands
Discord supports subcommand groups and subcommands. The adapter flattens these into the event.command path:
| Discord command | event.command | event.text |
|---|---|---|
| /status | /status | "" |
| /project create --name="Acme" | /project create | Acme |
| /project issue list --status="open" | /project issue list | open |
For full option details (names, types), use event.raw to access the original Discord interaction payload.
Registering commands with Discord
You need to register slash commands with the Discord API before they appear in the client. The Chat SDK handles incoming commands but does not register them for you.