Agent starter
This library provides a wrapper around XMTP SDK for Node to make it easier to use in your agent.
Install
yarn
yarn add xmtp
Overview
These are the steps to initialize the XMTP listener and send messages.
ENCRYPTION_KEY
: The private key of the wallet that will be used to send or receive messages.
import { XMTP } from "@xmtp/agent-starter";
const xmtp = new XMTP(onMessage, {
encryptionKey: ENCRYPTION_KEY,
});
await xmtp.init();
const onMessage = async (message, user) => {
console.log(`Decoded message: ${message.content.text} by ${user.address}`);
// Your AI model response
const response = await api("Hi, how are you?");
//Send text message
await xmtp.send({
message: response,
originalMessage: message,
});
};
Address availability
Returns true
if an address has XMTP enabled
const isOnXMTP = await xmtp.canMessage(address);
Groups
To learn more about groups, read the XMTP documentation.
To create a group from your agent, you can use the following code:
const group = await xmtp?.conversations.newGroup([address1, address2]);
As an admin you can add members to the group.
// get the group
await group.sync();
//By address
await group.addMembers([userAddresses]);
//By inboxId
await group.addMembersByInboxId([addedInboxes]);
Receive messages
const onMessage = async (message, user) => {
console.log(`Decoded message: ${message.content.text} by ${user.address}`);
let typeId = message.typeId;
if (typeId === "text") {
// Do something with the text
} else if (typeId === "reaction") {
// Do something with the reaction
} else if (typeId === "reply") {
// Do something with the `reply`
} else if (typeId === "attachment") {
// Do something with the attachment data url
} else if (typeId === "agent_message") {
// Do something with the agent message
} else if (typeId === "group_updated") {
// Do something with the group updated metadata
}
};
Send 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.",
receivers: ["0x123..."], // optional
originalMessage: message, // optional
};
await xmtp.send(textMessage);