Skip to content

Commit 60a49fa

Browse files
committed
updated comments and readme
1 parent b118256 commit 60a49fa

File tree

6 files changed

+132
-6
lines changed

6 files changed

+132
-6
lines changed

examples/gated-group/README.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,17 @@ async function checkNft(
9797
3. Once you have the group ID, you can add members using `/add <group_id> <wallet_address>`
9898
4. The bot will verify NFT ownership and add the wallet if they own the required NFT
9999

100-
The bot will automatically make the group creator a super admin and can optionally make new members admins as well.
100+
## Run the agent
101+
102+
```bash
103+
# git clone repo
104+
git clone https://github.com/ephemeraHQ/xmtp-agent-examples.git
105+
# go to the folder
106+
cd xmtp-agent-examples
107+
# install packages
108+
yarn
109+
# generate random keys (optional)
110+
yarn gen:keys
111+
# run the example
112+
yarn examples:gpt
113+
```

examples/gated-group/index.ts

+31-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@ import { Client, type XmtpEnv } from "@xmtp/node-sdk";
22
import { Alchemy, Network } from "alchemy-sdk";
33
import { createSigner, getEncryptionKeyFromHex } from "@/helpers";
44

5+
/* Set the Alchemy API key and network */
56
const settings = {
6-
apiKey: process.env.ALCHEMY_API_KEY, // Replace with your Alchemy API key
7-
network: Network.BASE_MAINNET, // Use the appropriate network
7+
apiKey: process.env.ALCHEMY_API_KEY,
8+
network: Network.BASE_MAINNET,
89
};
910

11+
/* Get the wallet key associated to the public key of
12+
* the agent and the encryption key for the local db
13+
* that stores your agent's messages */
1014
const { WALLET_KEY, ENCRYPTION_KEY } = process.env;
1115

1216
if (!WALLET_KEY) {
@@ -17,28 +21,37 @@ if (!ENCRYPTION_KEY) {
1721
throw new Error("ENCRYPTION_KEY must be set");
1822
}
1923

24+
/* Create the signer using viem and parse the encryption key for the local db */
2025
const signer = createSigner(WALLET_KEY);
2126
const encryptionKey = getEncryptionKeyFromHex(ENCRYPTION_KEY);
2227

28+
/* Set the environment to dev or production */
2329
const env: XmtpEnv = "dev";
2430

31+
/**
32+
* Main function to run the agent
33+
*/
2534
async function main() {
2635
console.log(`Creating client on the '${env}' network...`);
36+
/* Initialize the xmtp client */
2737
const client = await Client.create(signer, encryptionKey, {
2838
env,
2939
});
3040

3141
console.log("Syncing conversations...");
42+
/* Sync the conversations from the network to update the local db */
3243
await client.conversations.sync();
3344

3445
console.log(
3546
`Agent initialized on ${client.accountAddress}\nSend a message on http://xmtp.chat/dm/${client.accountAddress}`,
3647
);
3748

3849
console.log("Waiting for messages...");
50+
/* Stream all messages from the network */
3951
const stream = client.conversations.streamAllMessages();
4052

4153
for await (const message of await stream) {
54+
/* Ignore messages from the same agent or non-text messages */
4255
if (
4356
message?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() ||
4457
message?.contentType?.typeId !== "text"
@@ -50,6 +63,7 @@ async function main() {
5063
`Received message: ${message.content as string} by ${message.senderInboxId}`,
5164
);
5265

66+
/* Get the conversation from the local db */
5367
const conversation = client.conversations.getConversationById(
5468
message.conversationId,
5569
);
@@ -58,7 +72,10 @@ async function main() {
5872
console.log("Unable to find conversation, skipping");
5973
continue;
6074
}
75+
76+
/* If the message is to create a new group */
6177
if (message.content === "/create") {
78+
/* Create a new group */
6279
console.log("Creating group");
6380
const group = await client.conversations.newGroup([]);
6481
console.log("Group created", group.id);
@@ -82,33 +99,38 @@ async function main() {
8299
typeof message.content === "string" &&
83100
message.content.startsWith("/add")
84101
) {
102+
/* Extract the group id and wallet address from the message */
85103
const groupId = message.content.split(" ")[1];
86104
if (!groupId) {
87105
await conversation.send("Please provide a group id");
88106
return;
89107
}
108+
/* Get the group from the local db */
90109
const group = client.conversations.getConversationById(groupId);
91110
if (!group) {
92111
await conversation.send("Please provide a valid group id");
93112
return;
94113
}
114+
/* Extract the wallet address from the message */
95115
const walletAddress = message.content.split(" ")[2];
96116
if (!walletAddress) {
97117
await conversation.send("Please provide a wallet address");
98118
return;
99119
}
100-
120+
/* Check if the user has the NFT */
101121
const result = await checkNft(walletAddress, "XMTPeople");
102122
if (!result) {
103123
console.log("User can't be added to the group");
104124
return;
105125
} else {
126+
/* Add the user to the group */
106127
await group.addMembers([walletAddress]);
107128
await conversation.send(
108129
`User added to the group\n- Group ID: ${groupId}\n- Wallet Address: ${walletAddress}`,
109130
);
110131
}
111132
} else {
133+
/* Send a welcome message to the user */
112134
await conversation.send(
113135
"👋 Welcome to the Gated Bot Group!\nTo get started, type /create to set up a new group. 🚀\nThis example will check if the user has a particular nft and add them to the group if they do.\nOnce your group is created, you'll receive a unique Group ID and URL.\nShare the URL with friends to invite them to join your group!",
114136
);
@@ -118,6 +140,12 @@ async function main() {
118140

119141
main().catch(console.error);
120142

143+
/**
144+
* Check if the user has the NFT
145+
* @param walletAddress - The wallet address of the user
146+
* @param collectionSlug - The slug of the collection
147+
* @returns true if the user has the NFT, false otherwise
148+
*/
121149
async function checkNft(
122150
walletAddress: string,
123151
collectionSlug: string,

examples/gm/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,18 @@ async function main() {
7979

8080
main().catch(console.error);
8181
```
82+
83+
## Run the agent
84+
85+
```bash
86+
# git clone repo
87+
git clone https://github.com/ephemeraHQ/xmtp-agent-examples.git
88+
# go to the folder
89+
cd xmtp-agent-examples
90+
# install packages
91+
yarn
92+
# generate random keys (optional)
93+
yarn gen:keys
94+
# run the example
95+
yarn examples:gpt
96+
```

examples/gpt/README.md

+33-2
Original file line numberDiff line numberDiff line change
@@ -30,41 +30,55 @@ import { createSigner, getEncryptionKeyFromHex } from "@/helpers";
3030

3131
const { WALLET_KEY, ENCRYPTION_KEY, OPENAI_API_KEY } = process.env;
3232

33+
/* Check if the environment variables are set */
3334
if (!WALLET_KEY) {
3435
throw new Error("WALLET_KEY must be set");
3536
}
3637

38+
/* Check if the encryption key is set */
3739
if (!ENCRYPTION_KEY) {
3840
throw new Error("ENCRYPTION_KEY must be set");
3941
}
4042

43+
/* Check if the OpenAI API key is set */
4144
if (!OPENAI_API_KEY) {
4245
throw new Error("OPENAI_API_KEY must be set");
4346
}
4447

48+
/* Create the signer using viem and parse the encryption key for the local db */
4549
const signer = createSigner(WALLET_KEY);
4650
const encryptionKey = getEncryptionKeyFromHex(ENCRYPTION_KEY);
51+
52+
/* Initialize the OpenAI client */
4753
const openai = new OpenAI({ apiKey: OPENAI_API_KEY });
4854

55+
/* Set the environment to dev or production */
4956
const env: XmtpEnv = "dev";
5057

58+
/**
59+
* Main function to run the agent
60+
*/
5161
async function main() {
5262
console.log(`Creating client on the '${env}' network...`);
63+
/* Initialize the xmtp client */
5364
const client = await Client.create(signer, encryptionKey, {
5465
env,
5566
});
5667

5768
console.log("Syncing conversations...");
69+
/* Sync the conversations from the network to update the local db */
5870
await client.conversations.sync();
5971

6072
console.log(
6173
`Agent initialized on ${client.accountAddress}\nSend a message on http://xmtp.chat/dm/${client.accountAddress}`,
6274
);
6375

6476
console.log("Waiting for messages...");
77+
/* Stream all messages from the network */
6578
const stream = client.conversations.streamAllMessages();
6679

6780
for await (const message of await stream) {
81+
/* Ignore messages from the same agent or non-text messages */
6882
if (
6983
message?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() ||
7084
message?.contentType?.typeId !== "text"
@@ -76,25 +90,31 @@ async function main() {
7690
`Received message: ${message.content as string} by ${message.senderInboxId}`,
7791
);
7892

93+
/* Get the conversation from the local db */
7994
const conversation = client.conversations.getConversationById(
8095
message.conversationId,
8196
);
8297

98+
/* If the conversation is not found, skip the message */
8399
if (!conversation) {
84100
console.log("Unable to find conversation, skipping");
85101
continue;
86102
}
87103

88104
try {
105+
/* Get the AI response */
89106
const completion = await openai.chat.completions.create({
90107
messages: [{ role: "user", content: message.content as string }],
91108
model: "gpt-3.5-turbo",
92109
});
93110

111+
/* Get the AI response */
94112
const response =
95113
completion.choices[0]?.message?.content ||
96114
"I'm not sure how to respond to that.";
115+
97116
console.log(`Sending AI response: ${response}`);
117+
/* Send the AI response to the conversation */
98118
await conversation.send(response);
99119
} catch (error) {
100120
console.error("Error getting AI response:", error);
@@ -110,6 +130,17 @@ async function main() {
110130
main().catch(console.error);
111131
```
112132

113-
Run the agent and send a test message from [xmtp.chat](https://xmtp.chat).
133+
## Run the agent
114134

115-
Enjoy your GPT-powered XMTP agent!
135+
```bash
136+
# git clone repo
137+
git clone https://github.com/ephemeraHQ/xmtp-agent-examples.git
138+
# go to the folder
139+
cd xmtp-agent-examples
140+
# install packages
141+
yarn
142+
# generate random keys (optional)
143+
yarn gen:keys
144+
# run the example
145+
yarn examples:gpt
146+
```

examples/gpt/index.ts

+20
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,55 @@ import { createSigner, getEncryptionKeyFromHex } from "@/helpers";
44

55
const { WALLET_KEY, ENCRYPTION_KEY, OPENAI_API_KEY } = process.env;
66

7+
/* Check if the environment variables are set */
78
if (!WALLET_KEY) {
89
throw new Error("WALLET_KEY must be set");
910
}
1011

12+
/* Check if the encryption key is set */
1113
if (!ENCRYPTION_KEY) {
1214
throw new Error("ENCRYPTION_KEY must be set");
1315
}
1416

17+
/* Check if the OpenAI API key is set */
1518
if (!OPENAI_API_KEY) {
1619
throw new Error("OPENAI_API_KEY must be set");
1720
}
1821

22+
/* Create the signer using viem and parse the encryption key for the local db */
1923
const signer = createSigner(WALLET_KEY);
2024
const encryptionKey = getEncryptionKeyFromHex(ENCRYPTION_KEY);
25+
26+
/* Initialize the OpenAI client */
2127
const openai = new OpenAI({ apiKey: OPENAI_API_KEY });
2228

29+
/* Set the environment to dev or production */
2330
const env: XmtpEnv = "dev";
2431

32+
/**
33+
* Main function to run the agent
34+
*/
2535
async function main() {
2636
console.log(`Creating client on the '${env}' network...`);
37+
/* Initialize the xmtp client */
2738
const client = await Client.create(signer, encryptionKey, {
2839
env,
2940
});
3041

3142
console.log("Syncing conversations...");
43+
/* Sync the conversations from the network to update the local db */
3244
await client.conversations.sync();
3345

3446
console.log(
3547
`Agent initialized on ${client.accountAddress}\nSend a message on http://xmtp.chat/dm/${client.accountAddress}`,
3648
);
3749

3850
console.log("Waiting for messages...");
51+
/* Stream all messages from the network */
3952
const stream = client.conversations.streamAllMessages();
4053

4154
for await (const message of await stream) {
55+
/* Ignore messages from the same agent or non-text messages */
4256
if (
4357
message?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() ||
4458
message?.contentType?.typeId !== "text"
@@ -50,25 +64,31 @@ async function main() {
5064
`Received message: ${message.content as string} by ${message.senderInboxId}`,
5165
);
5266

67+
/* Get the conversation from the local db */
5368
const conversation = client.conversations.getConversationById(
5469
message.conversationId,
5570
);
5671

72+
/* If the conversation is not found, skip the message */
5773
if (!conversation) {
5874
console.log("Unable to find conversation, skipping");
5975
continue;
6076
}
6177

6278
try {
79+
/* Get the AI response */
6380
const completion = await openai.chat.completions.create({
6481
messages: [{ role: "user", content: message.content as string }],
6582
model: "gpt-3.5-turbo",
6683
});
6784

85+
/* Get the AI response */
6886
const response =
6987
completion.choices[0]?.message?.content ||
7088
"I'm not sure how to respond to that.";
89+
7190
console.log(`Sending AI response: ${response}`);
91+
/* Send the AI response to the conversation */
7292
await conversation.send(response);
7393
} catch (error) {
7494
console.error("Error getting AI response:", error);

0 commit comments

Comments
 (0)