Skip to content

Skills

Skills are the actions of the agent. They are defined in the src/skills/your-skill.ts file.

Defining a skill

import { Skill } from "@xmtp/message-kit";
 
export const checkDomain: Skill[] = [
  {
    skill: // name of the skill
    handler: handler(context)
    examples: // examples of the skill
    description: // description of the skill
    params: {
      <name>: {
        default: "" // default value
        type: "string" | "number" | "username" | "quoted" | "address" | "prompt" | "url"
        values: ["USDC","ETH"]// accepted values
        optional: true // if true, the parameter is optional
        plural: false // if true, the parameter is an array
      }
  },
];

Handling logic

When a message is send through XMTP to your agent, the handler function is triggered. It brings the message with all the context .

src/index.ts
function handler(context: Context) {
  const {
    message: {
      content: { text, params, attachment, reply, reference, skill, previousMsg },
      sender: { address, inboxId, username, ensDomain },
      sent,
      typeId
    },
  } = context;
 
  if (typeId === "text") {
    // Do something with the text
  } else if (typeId === "skill") {
    // Do something with the skill
    const { address, domain } = params;
    // Use params
  } else if (typeId === "reply") {
    // Do something with the `reply`
    console.log(reply,previousMsg);
  } else if (typeId === "attachment") {
    // Do something with the attachment data url
    console.log(attachment);
  }
}

Sending messages

App messages are messages that are sent when you send a reply to a message and are highlighted differently by the apps.

// Send a message
await context.send("Your message.");
// Reply to the last message
await context.reply("Your message.");
// Send a message to specific users
await context.sendTo("Your message.", ["address1", "address2"]);
// Await a response
const answer = await context.awaitResponse("Please answer with yes or no", [
  "yes",
  "no",
]);
console.log(`You answered: ${answer}`);
// Send an image (max 1MB)
await context.sendImage("path/to/image.png");
// Send a remote image (max 1MB)
await context.sendImage("https://picsum.photos/200/300");
//Send a message to another agent
await context.sendAgentMessage("Would you like to approve this transaction?", {
  agentId: "payment-bot",
  skillUsed: "approve-tx",
  amount: "10",
  token: "USDC",
  chain: "base",
  destinationAddress: "0x123...789",
});
// Send a dm to the sender
await context.dm("Your message.");