Skip to content

Group chat

This is a reference app that will allow you to understand the potential and all of its features. Through many handlers, you will be able to understand different use cases:

  • AI: Reply with GPT when called by /agent.
  • Tipping: Tip when a user reacts with the 🎩 emoji, replies 10 $degen and /tip.
  • Games: Handle frame games like /game slot, /game wordle
  • Transactions: Handle commands like /send, /swap, /show.
  • Admin: Handle admin commands like /add, /remove, /name

Structure

Check out the repository for the full code.

group/
├── src/
│   ├── index.ts              # Index file of the app.
│   ├── command.ts            # For handling commands.
│   └── handler/
│         └── agent.ts          # Handles gpt agent.
│         └── tipping.ts        # Handles tipping.
│         └── game.ts           # Handles game frames.
│         └── admin.ts          # Handles moderation.
│         └── payment.ts        # Handles split payments.
│         └── transactions.ts   # Handles the transaction frame.
│   └── lib/
│         └── openai.ts          # Handles the openai logic.
├── package.json
├── tsconfig.json
└── .env

Main code

src/index.ts
import { run, HandlerContext, CommandHandlers } from "@xmtp/message-kit";
import { commands } from "./commands.js";
import { handler as tipping } from "./handler/tipping.js";
import { handler as agent } from "./handler/agent.js";
import { handler as transaction } from "./handler/transaction.js";
import { handler as splitpayment } from "./handler/payment.js";
import { handler as games } from "./handler/game.js";
import { handler as admin } from "./handler/admin.js";
import { handler as loyalty } from "./handler/loyalty.js";
 
// Main function to run the app
run(async (context: HandlerContext) => {
  const {
    message: { typeId },
  } = context;
  try {
    switch (typeId) {
      case "reaction":
        handleReaction(context);
        loyalty(context);
        break;
      case "reply":
        handleReply(context);
        break;
      case "group_updated":
        admin(context);
        loyalty(context);
        break;
      case "remoteStaticAttachment":
        handleAttachment(context);
        break;
      case "text":
        handleTextMessage(context);
        loyalty(context, true);
        break;
      default:
        console.warn(`Unhandled message type: ${typeId}`);
    }
  } catch (error) {
    console.error("Error handling message:", error);
  }
});
 
// Handle reaction messages
async function handleReaction(context: HandlerContext) {
  const {
    content: { content: emoji, action },
  } = context.message;
 
  if (action === "added" && (emoji === "degen" || emoji === "🎩")) {
    await tipping(context);
  }
}
 
// Handle reply messages
async function handleReply(context: HandlerContext) {
  const {
    content: { content: reply },
  } = context.message;
  if (reply.includes("degen")) {
    await tipping(context);
  }
}
 
// Handle attachment messages
async function handleAttachment(context: HandlerContext) {
  await splitpayment(context);
}
 
// Handle text messages
async function handleTextMessage(context: HandlerContext) {
  const {
    content: { content: text },
  } = context.message;
  if (text.includes("/help")) {
    await helpHandler(context);
  } else if (text.includes("@bot")) {
    await agent(context);
  } else if (text.startsWith("/")) {
    await context.intent(text);
  }
}
 
async function helpHandler(context: HandlerContext) {
  const intro =
    "Available experiences:\n" +
    commands
      .flatMap((app) => app.commands)
      .map((command) => `${command.command} - ${command.description}`)
      .join("\n") +
    "\nUse these commands to interact with specific apps.";
  context.send(intro);
}

Run the app

Follow the steps below to run the app

Setup

cmd
# Clone the repo
git clone https://github.com/ephemeraHQ/message-kit
# Go to the examples/group folder
cd examples/group
# Install the dependencies
yarn install
# Run the app
yarn dev

Variables

Set up these variables in your app

cmd
KEY= # 0x... the private key of the bot wallet (with the 0x prefix)
OPEN_AI_API_KEY= # your openai api key
STACK_API_KEY= # stack api key
MSG_LOG= # true to log all messages