Skip Navigation

Custom Commands

One of the functionalities available to Red Apps are custom commands. Custom commands are commands starting with the section sign (§) available under the \ key in Sabre Red 360. Red Apps can execute custom logic for defined custom commands.

custom command

Custom Commands Definition

Custom commands are defined in manifest.json file. To handle §TEST custom command we need to define the following:

{
    "name": "com-sabre-redapp-example3-web-customcommand-web-module",
    // Other Properties...

    // Custom commands:
    "customCommands": [
        {
            "TEST": "com-sabre-redapp-example3-web-customcommand-web-module-SampleCustomCommandHandler"
        }
    ]
}

Definition contains the list of all custom commands. Each custom command is a separate object. The key of this object defines a custom command prefix (note the lack of § sign in the definition). The value of that key defines the name of the service that will handle the given custom command.

Warning
Custom command prefixes cannot contain the § sign. In such case, the § sign will be treated as the beginning of a parameter.

Custom Command Parameter

Custom commands support parameters. A Parameter can be added to the custom command by adding the section sign (§) at the custom command end. After that, we can add any string value. Parameters do not require any additional configuration as all custom commands support parameters by default.

If we define TEST custom command prefix in manifest.json file, then this custom command handler would be triggered by sending both §TEST and §TEST§ABC commands.

Multiple Custom Commands

Red App can define more than one custom command.

Note
Each custom command definition object should define only one custom command.

Otherwise, only one of those custom commands will be registered. If a Red App requires more than one custom command then those should be defined as separate objects in manifest.json file:

{
    "name": "com-sabre-redapp-example3-web-customcommand-web-module",
    // Other Properties...

    // Custom commands:
    "customCommands": [
        {
            "TEST": "com-sabre-redapp-example3-web-customcommand-web-module-SampleCustomCommandHandler"
        },
        {
            "TEST2": "com-sabre-redapp-example3-web-customcommand-web-module-AnotherCustomCommandHandler"
        }
    ]
}

Custom Command Handler

Service that handles custom command has to extend abstract CustomCommandHandler service and implement onCommandSend method. The onCommandSend method as an argument gets the CustomCommandRq object and returns Promise with the CustomCommandRs object. This method is called whenever the agent sends a matching custom command.

All the required imports can be found in the sabre-ngv-customCommand module:

import {CustomCommandHandler} from 'sabre-ngv-customCommand/interfaces/CustomCommandHandler';
import {CustomCommandRq} from 'sabre-ngv-customCommand/domain/CustomCommandRq';
import {CustomCommandRs} from 'sabre-ngv-customCommand/domain/CustomCommandRs';

After adding all imports we can define handler service:

export class SampleCustomCommandHandler extends CustomCommandHandler {
    static SERVICE_NAME = 'com-sabre-redapp-example3-web-customcommand-web-module-SampleCustomCommandHandler';

    async onCommandSend(rq: CustomCommandRq): Promise<CustomCommandRs> {
        // Custom logic to run
    }
}

Please note the SERVICE_NAME string as it’s being used in the custom command definition in the manifest.json file. In this example onCommandSend method is marked as async (it is not required). It can be marked this way because it returns Promise. Due to this, we are able to use await in the method body.

Note
Remember to register service.

Custom Command Request

This is the object passed to the onCommandSend method of the custom command handler with data about custom command that triggered the given handler. It contains a command parameter with a custom command base (prefixed with §) and an optional parameter with a parameter value (if was sent).

Given we have defined the TEST prefix. Below you can find commands that would trigger the custom command handler with the request object that would be passed to the onCommandSend method:

Command CustomCommandRq

§TEST

{command: '§TEST'}

§TEST§

{command: '§TEST'}

§TEST§ABC

{command: '§TEST', parameter: 'ABC'}

§TEST§ABC§

{command: '§TEST', parameter: 'ABC§'}

§TEST§ABC§DEF

{command: '§TEST', parameter: 'ABC§DEF'}

Custom Command Response

Response object returned from the onCommandSend method. At the moment it is empty.

Example

An example of Custom Commands usage with comments can be found in com.sabre.redapp.example3.web.customcommand sample.