Developing Slash Commands

Slash Commands enable you to register and advertise specific commands for your bot that help users understand some of the bot’s features. When the user is chatting with a bot and types a slash, the bot’s slash commands appear in a popup, with a description of what each command does.

When the user invokes your bot by slash command, an additional field is attached to the message sent to your bot. This field indicates that the user selected a slash command, as well as which slash command they selected (based on the command ID specified in your bot's configuration). In addition, the slash command has an annotation, just as when the user interacts with your bot by name.

{
  ...
  "message": {
    ...
    "text": "/vote yes"
    "argumentText": " yes"
    "annotations": [
      {
        "length": 5
        "startIndex": 0
        "slashCommand": {
          "type": "INVOKE",
          "user": {
            "avatarUrl": "https://.../avatar.png",
            "displayName": "VoteBot",
            "name": "users/1234567890987654321",
            "type": "BOT"
          }
        },
        "type": "SLASH_COMMAND"
      }
    ]
    "slashCommand": {
      "commandId": 2
    }
  }
}

The command ID that your bot will receive corresponds to the command ID that you specify for that command in your bot's configuration:

This allows your bot to handle slash command cases explicitly with limited argument parsing:

if (message.slashCommand) {
  switch (message.slashCommand.commandId) {
    case 2:  // /vote
      return vote(message.argumentText);
  }
}

// Other existing handling for users invoking your bot by name.