Custom Commands - adamhoward56/iMessageBot GitHub Wiki

If you want to create or modify the bot's default commands, you may do so in src/commands.js. This module contains an object, commands, whose entries are automatically loaded and parsed by the bot.

Sample custom command

Below is a sample command definition that adds two numbers together.

'add': {
    type: 'func',
    desc: 'Adds two numbers together.',
    run: (args) => {
        let a = Number(args[1]),
            b = Number(args[2])
            
        return a + b
    },

    usage: '/add <first> <second>'
}

This custom command will be automatically loaded the next time the server is started. To test it out, try sending /add 4 5 to the iMessage account running the bot. You should receive the message 9 back!

Required command fields

  • type (String) - Must be one of the three options:

    • text - This command sends back a fixed message, i.e. just a string
    • func - This command's run field contains a function that returns a string (or an object that has a toString() method). The message parser passes a single argument, args, to the run method of a func command. The first element of args is the command name, and subsequent arguments are tokenized by spaces.
    • action - Similar to the func type, but does not require a return value. Actions are fully custom, and expect that the command itself handles sending a message back to the user. For this reason, actions are called by the parser with parameters (context, args). context is an object containing all fields about the message from the parser, and args is the same as those passed to the func type. To actually send a message (or multiple messages), you may call the function sendResponse(context, message) which parses the context object to determine the appropriate recipient, and a message to send to that recipient. You may call this as many (or as little) times as you would like in an action command. Note: the return value of an action command is ignored.
  • desc (String) - A description of what the command does.

  • run (Function) - The function called during command parsing. (Alternatively a string, when type == 'text').

  • usage (String) - String used to help users interact with the command.

⚠️ **GitHub.com Fallback** ⚠️