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.
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!
-
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'srunfield contains a function that returns a string (or an object that has atoString()method). The message parser passes a single argument,args, to the run method of afunccommand. The first element ofargsis the command name, and subsequent arguments are tokenized by spaces. -
action- Similar to thefunctype, 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).contextis an object containing all fields about the message from the parser, andargsis the same as those passed to thefunctype. To actually send a message (or multiple messages), you may call the functionsendResponse(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 anactioncommand. 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, whentype == 'text'). -
usage(String) - String used to help users interact with the command.