Skip to content

Agent context

Message events receive MessageContext. Conversation events receive ConversationContext. start and stop receive ClientContext. unhandledError receives an Error. Error middleware receives a partial context with a client and optional message and conversation.

Property or methodPurpose
clientNode SDK client
conversationCurrent conversation
messageSDK message with tagged content, for message events
contentDecoded value, for message events
getClientAddress()Current client’s account identifier
isDm(), isGroup()Narrow the conversation type
isAllowed(), isDenied(), isUnknown()Read conversation consent state with await
sendRemoteAttachment(file, callback?)Use backend attachment hosting, or an app upload callback
usesCodec(Codec)Narrow custom content
isMarkdown(), isText()Narrow text content
isReply(), isReaction()Narrow tagged reply or reaction content
isReadReceipt()Narrow read-receipt content
isRemoteAttachment()Narrow remote-attachment content
isTransactionReference()Narrow transaction-reference content
isWalletSendCalls()Narrow wallet-call content
sendReaction(content, schema?)React to the current message
sendMarkdownReply(markdown)Reply with Markdown
sendTextReply(text)Reply with text
getSenderAddress()Resolve the sender’s first account identifier
const agent: Agent<unknown>
agent
.
NodeJS.EventEmitter<EventHandlerMap<unknown>>.on<"message">(eventName: keyof EventEmitterEventMap | "actions" | "attachment" | "conversation" | "group-update" | "dm" | "group" | "inline-attachment" | "intent" | "leave-request" | "markdown" | "message" | "multi-attachment" | "reaction" | "read-receipt" | "reply" | "start" | "stop" | "text" | "transaction-reference" | "unhandledError" | "unknownMessage" | "wallet-send-calls", listener: (ctx: MessageContext<unknown, unknown>) => void): Agent<unknown>

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

@since ― v0.1.101

@param ― eventName The name of the event.

@param ― listener The callback function

on
("message", async (
ctx: MessageContext<unknown, unknown>
ctx
) => {
if (
ctx: MessageContext<unknown, unknown>
ctx
.
MessageContext<unknown, unknown>.isText(): this is MessageContext<string, unknown>

Narrow this context to a text message.

isText
()) {
const
const sender: string | undefined
sender
= await
ctx: MessageContext<string, unknown>
ctx
.
MessageContext<string, unknown>.getSenderAddress(): Promise<string | undefined>

Return the first identity identifier for the sender inbox.

getSenderAddress
();
await
ctx: MessageContext<string, unknown>
ctx
.
MessageContext<string, unknown>.sendTextReply(text: string): Promise<void>

Send a text reply to this message with push disabled.

sendTextReply
(`Hello ${
const sender: string | undefined
sender
?? "there"}`);
}
});
await
const agent: Agent<unknown>
agent
.
Agent<unknown>.start(options?: AgentStreamingOptions): Promise<void>

Start conversation and message streams. Calling this while running is a no-op.

start
();

The context exposes the underlying conversation. Use its SDK methods for members, messages, metadata, and content-type send helpers.

For a text event, read the string from ctx.content. ctx.message.content is a tagged record, such as { kind: "text", value: "hello" }. For a reply, ctx.content is the reply record with referenceId and body. Read-receipt contexts have undefined content.

Assigning ctx.content changes the value used by later middleware. It does not change ctx.message. The command router uses this property to pass command arguments to its handler.

sendReaction(), sendTextReply(), and sendMarkdownReply() disable push notifications. Use the underlying conversation send methods when you need other send options.