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, replies10 $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
- Loyalty: Handle loyalty commands like
/points
,/leaderboard
.
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.
â âââ loyalty.ts # Handles loyalty.
â âââ 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 } from "@xmtp/message-kit";
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.startsWith("@agent")) {
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