Support replies with your agent built with XMTP
Use the reply content type to support quote replies with your agent. A reply is a method to directly respond to a specific message in a conversation. Users can select and reply to a particular message instead of sending a new one.
Install the package
In some SDKs, the ReplyCodec
is already included in the SDK. If not, you can install the package using the following command:
npm
npm i @xmtp/content-type-reply
Configure the content type
After importing the package, you can register the codec.
Node
import { ReplyCodec } from '@xmtp/content-type-reply';
// Create the XMTP agent
const agent = await Agent.create(signer, {
env: 'dev',
codecs: [new ReplyCodec()],
});
Send a reply
Once you've created a reply, you can send it. Replies are represented as objects with two keys:
-
reference
: ID of the message being replied to -
content
: String representation of the reply
Node
import { ContentTypeText } from '@xmtp/content-type-text';
import { ContentTypeReply } from '@xmtp/content-type-reply';
import type { Reply } from '@xmtp/content-type-reply';
const reply: Reply = {
reference: someMessageID,
contentType: ContentTypeText,
content: 'I concur',
};
await ctx.sendTextReply(reply);
Receive a reply
Node
agent.on('reply', async (ctx) => {
const message = ctx.message;
const replyContent = message.content;
console.log(`New reply: ${replyContent.content}`);
});
Filter a reply
Now that you can send a reply, you need a way to receive a reply. For example:
Node
if (message.contentType.sameAs(ContentTypeReply)) {
// We've got a reply.
const reply: Reply = message.content;
}