Developing Slash Commands

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

When the user invokes your app by slash command, an additional field is attached to the message sent to your app. 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 app's configuration). In addition, the slash command has an annotation, just as when the user interacts with your app 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 app will receive corresponds to the command ID that you specify for that command in your app's configuration:

This allows your app 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 app by name.