Skip to content

Command

If the message starts with a / it is considered a command. See more details in the commands section.

Command object

The command object is a text type contains the following extra property:

{
  content: string, // The raw content of the message
  params: params // The value of the parameters of the command
}

If the parameter is not correctly declared in the src/command.ts file or the text input from the user is not correct, the params will return undefined

Receive a command

Here's an example of how you can receive a text message in your app:

const { content, typeId } = context.message;
 
if (typeId === "text") {
  const { params } = content;
  // Use params of the command
}

Let's say the user sends the command to the app.

cmd
/send 10 usdc @bo

The helper function will extract the parameters from the command.

params
{
  "command": "send",
  "params": {
    "amount": 10,
    "token": "usdc",
    "username": [
      {
        "address": "0xbo",
        "inboxId": "0xbo",
        "accountAddresses": ["0xbo"],
        "name": "bo"
      }
    ]
  }
}

Command handlers

An easier way to declare how to handle and respond to commands is to use handlers. Handlers help you orgaize the code being useful when you are managing multiple bots

import { run, HandlerContext, CommandHandlers } from "@xmtp/message-kit";
import { commands } from "./commands.js";
import { transaction } from "./handler/transaction.js";
 
const commandHandlers: CommandHandlers = {
  "/send": transaction,
};
 
const appConfig = {
  commands: commands,
  commandHandlers: commandHandlers,
};
 
run(async (context: HandlerContext) => {
  //Your logic here
  if (text.startsWith("/")) {
    await context.intent(text);
  }
}, appConfig);