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.

Install the package

In some SDKs, the ReactionCodec is already included in the SDK. If not, you can install the package using the following command:

npm
npm i @xmtp/content-type-reaction

Send a reaction

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

  • reference: ID of the message being reacted to

  • 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
await ctx.sendReaction('❤️');

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 { ReactionCodec } from '@xmtp/content-type-reaction';
import type { Reaction } from '@xmtp/content-type-reaction';
 
if (ctx.usesCodec(ReactionCodec)) {
  // We've got a reaction.
  const reaction: Reaction = ctx.message.content;
}