Skip to content

Support reactions with your agent built with XMTP

Use the reaction content type to support reactions with your agent. A reaction is a quick and often emoji-based way to respond to a message. Reactions are usually limited to a predefined set of emojis or symbols provided by the chat app.

The reaction content type is built into the Agent SDK. No installation is required.

Send a reaction

With XMTP, reactions are represented as objects with the following keys:

  • reference: ID of the message being reacted to

  • referenceInboxId: (Optional) Inbox ID of the sender of the message being reacted to. This helps with message attribution in group conversations.

  • action: Action of the reaction (added or removed)

  • content: String representation of the reaction (smile, for example) to be interpreted by clients

  • schema: Schema of the reaction (Unicode, shortcode, or custom)

Node
import { ReactionAction, ReactionSchema, type Reaction } from '@xmtp/agent-sdk';
 
// Simple
await ctx.sendReaction('❤️');
 
// With all fields
const reaction: Reaction = {
  reference: ctx.message.id,
  referenceInboxId: ctx.message.senderInboxId, // Optional: Inbox ID of the message sender
  action: ReactionAction.Added,
  content: '❤️',
  schema: ReactionSchema.Unicode,
};
 
await ctx.conversation.sendReaction(reaction);

Receive a reaction

Node
agent.on('reaction', async (ctx) => {
  const message = ctx.message;
  const reactionContent = message.content;
  console.log(`New reaction: ${reactionContent.content}`);
});

Filter a reaction

Now that you can send a reaction, you need a way to receive a reaction. For example:

Node
import type { Reaction } from '@xmtp/agent-sdk';
 
if (ctx.isReaction()) {
  // We've got a reaction.
  const reaction: Reaction = ctx.message.content;
}