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);
  }
}

Text messages

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

//text
let textMessage: userMessage = {
  message: "Your message.",
  originalMessage: context.message,
};
await context.send(textMessage);
 
const answer = await context.awaitResponse("Please answer with yes or no", [
  "yes",
  "no",
]);
console.log(`You answered: ${answer}`);

Other messages

// reaction
let reaction: userMessage = {
  message: "😅",
  receivers: [context.message.sender.address],
  originalMessage: context.message,
  typeId: "reaction",
};
await context.send(reaction);
 
// Reply
let reply: userMessage = {
  message: "Your message.",
  receivers: [context.message.sender.address],
  originalMessage: context.message,
  typeId: "reply",
};
await context.send(reply);
 
//Attachment
let attachment: userMessage = {
  message: "https://picsum.photos/200/300",
  receivers: [context.message.sender.address],
  originalMessage: context.message,
  typeId: "attachment",
};
await context.send(attachment);
 
//agent message
let agentMessage: userMessage = {
  message: "Would you like to approve this transaction?",
  metadata: {
    agentId: "payment-bot",
    skillUsed: "approve-tx",
    amount: "10",
    token: "USDC",
    chain: "base",
    destinationAddress: "0x123...789",
  },
  receivers: [context.message.sender.address],
  originalMessage: context.message,
  typeId: "agent",
};
await context.send(agentMessage);