Agent filters
Import built-in filters as filter or f from @xmtp/agent-sdk.
| Filter | Checks |
|---|---|
fromSelf(message, client) | Sender is the current client |
hasContent(message) | Decoded content is present |
isDM(conversation) | Conversation is a direct message |
isGroup(conversation) | Conversation is a group |
isGroupAdmin(conversation, message) | Sender is a group admin |
isGroupSuperAdmin(conversation, message) | Sender is a group super admin |
usesCodec(message, Codec) | Message uses a custom codec; narrows its TypeScript type |
import { const filter: { fromSelf: <ContentTypes>(message: DecodedMessage<ContentTypes>, client: Client<ContentTypes>) => boolean; hasContent: <ContentTypes>(message: DecodedMessage<ContentTypes>) => message is DecodedMessageWithContent<ContentTypes>; isDM: (conversation: Conversation) => conversation is Dm; isGroup: (conversation: Conversation) => conversation is Group; isGroupAdmin: (conversation: Conversation, message: DecodedMessage) => boolean; isGroupSuperAdmin: (conversation: Conversation, message: DecodedMessage) => boolean; usesCodec: <T extends ContentCodec>(message: DecodedMessage, codecClass: new () => T) => message is DecodedMessageWithContent<ReturnType<T["decode"]>>;}
Type guards used by Agent middleware to classify messages and conversations.
filter } from "@xmtp/agent-sdk";
const agent: Agent<BuiltInContentTypes>
agent.NodeJS.EventEmitter<EventHandlerMap<BuiltInContentTypes>>.on<"message">(eventName: keyof EventHandlerMap<BuiltInContentTypes>, listener: (ctx: MessageContext<unknown, BuiltInContentTypes>) => void): Agent<BuiltInContentTypes>
Adds the listener function to the end of the listeners array for the event
named eventName. No checks are made to see if the listener has already
been added. Multiple calls passing the same combination of eventName and
listener will result in the listener being added, and called, multiple times.
server.on('connection', (stream) => { console.log('someone connected!');});
Returns a reference to the EventEmitter, so that calls can be chained.
By default, event listeners are invoked in the order they are added. The emitter.prependListener() method can be used as an alternative to add the
event listener to the beginning of the listeners array.
import { EventEmitter } from 'node:events';const myEE = new EventEmitter();myEE.on('foo', () => console.log('a'));myEE.prependListener('foo', () => console.log('b'));myEE.emit('foo');// Prints:// b// a
on("message", async (ctx: MessageContext<unknown, BuiltInContentTypes>
ctx) => { if (const filter: { fromSelf: <ContentTypes>(message: DecodedMessage<ContentTypes>, client: Client<ContentTypes>) => boolean; hasContent: <ContentTypes>(message: DecodedMessage<ContentTypes>) => message is DecodedMessageWithContent<ContentTypes>; isDM: (conversation: Conversation) => conversation is Dm; isGroup: (conversation: Conversation) => conversation is Group; isGroupAdmin: (conversation: Conversation, message: DecodedMessage) => boolean; isGroupSuperAdmin: (conversation: Conversation, message: DecodedMessage) => boolean; usesCodec: <T extends ContentCodec>(message: DecodedMessage, codecClass: new () => T) => message is DecodedMessageWithContent<ReturnType<T["decode"]>>;}
Type guards used by Agent middleware to classify messages and conversations.
filter.fromSelf: <unknown>(message: DecodedMessage<unknown>, client: Client<unknown>) => boolean
Return true when a message was sent by the supplied client.
fromSelf(ctx: MessageContext<unknown, BuiltInContentTypes>
ctx.MessageContext<unknown, BuiltInContentTypes>.message: DecodedMessageWithContent<unknown>
Return the decoded message.
message, ctx: MessageContext<unknown, BuiltInContentTypes>
ctx.ClientContext<BuiltInContentTypes>.client: Client<BuiltInContentTypes>
Return the wrapped XMTP client.
client)) return; if (const filter: { fromSelf: <ContentTypes>(message: DecodedMessage<ContentTypes>, client: Client<ContentTypes>) => boolean; hasContent: <ContentTypes>(message: DecodedMessage<ContentTypes>) => message is DecodedMessageWithContent<ContentTypes>; isDM: (conversation: Conversation) => conversation is Dm; isGroup: (conversation: Conversation) => conversation is Group; isGroupAdmin: (conversation: Conversation, message: DecodedMessage) => boolean; isGroupSuperAdmin: (conversation: Conversation, message: DecodedMessage) => boolean; usesCodec: <T extends ContentCodec>(message: DecodedMessage, codecClass: new () => T) => message is DecodedMessageWithContent<ReturnType<T["decode"]>>;}
Type guards used by Agent middleware to classify messages and conversations.
filter.hasContent: <unknown>(message: DecodedMessage<unknown>) => message is DecodedMessageWithContent<unknown>
Return true when a message contains decoded content.
hasContent(ctx: MessageContext<unknown, BuiltInContentTypes>
ctx.MessageContext<unknown, BuiltInContentTypes>.message: DecodedMessageWithContent<unknown>
Return the decoded message.
message) && ctx: MessageContext<unknown, BuiltInContentTypes>
ctx.MessageContext<unknown, BuiltInContentTypes>.isText(): this is MessageContext<string>
Narrow the message to plain text content.
isText()) { await ctx: MessageContext<unknown, BuiltInContentTypes> & MessageContext<string, unknown>
ctx.MessageContext<MessageContentType = unknown, ContentTypes = unknown>.sendTextReply(text: string): Promise<void>
Reply to this message with plain text content.
sendTextReply("Received text"); }});
MessageContext also supplies type guards for Markdown, text, replies, reactions, read receipts, remote attachments, transaction references, and wallet send calls.

