@@ -2,7 +2,6 @@ import { Client, type XmtpEnv } from "@xmtp/node-sdk";
2
2
import { Alchemy , Network } from "alchemy-sdk" ;
3
3
import { createSigner , getEncryptionKeyFromHex } from "@/helpers" ;
4
4
5
- /* Set the Alchemy API key and network */
6
5
const settings = {
7
6
apiKey : process . env . ALCHEMY_API_KEY ,
8
7
network : Network . BASE_MAINNET ,
@@ -28,26 +27,20 @@ const encryptionKey = getEncryptionKeyFromHex(ENCRYPTION_KEY);
28
27
/* Set the environment to dev or production */
29
28
const env : XmtpEnv = "dev" ;
30
29
31
- /**
32
- * Main function to run the agent
33
- */
34
30
async function main ( ) {
35
31
console . log ( `Creating client on the '${ env } ' network...` ) ;
36
- /* Initialize the xmtp client */
37
32
const client = await Client . create ( signer , encryptionKey , {
38
33
env,
39
34
} ) ;
40
35
41
36
console . log ( "Syncing conversations..." ) ;
42
- /* Sync the conversations from the network to update the local db */
43
37
await client . conversations . sync ( ) ;
44
38
45
39
console . log (
46
40
`Agent initialized on ${ client . accountAddress } \nSend a message on http://xmtp.chat/dm/${ client . accountAddress } ` ,
47
41
) ;
48
42
49
43
console . log ( "Waiting for messages..." ) ;
50
- /* Stream all messages from the network */
51
44
const stream = client . conversations . streamAllMessages ( ) ;
52
45
53
46
for await ( const message of await stream ) {
@@ -63,7 +56,6 @@ async function main() {
63
56
`Received message: ${ message . content as string } by ${ message . senderInboxId } ` ,
64
57
) ;
65
58
66
- /* Get the conversation from the local db */
67
59
const conversation = client . conversations . getConversationById (
68
60
message . conversationId ,
69
61
) ;
@@ -73,9 +65,11 @@ async function main() {
73
65
continue ;
74
66
}
75
67
76
- /* If the message is to create a new group */
68
+ /* This example works by parsing slash commands to create a new group or add a member to a group
69
+ * /create - create a new group
70
+ * /add <group_id> <wallet_address> - add a member to a group */
71
+
77
72
if ( message . content === "/create" ) {
78
- /* Create a new group */
79
73
console . log ( "Creating group" ) ;
80
74
const group = await client . conversations . newGroup ( [ ] ) ;
81
75
console . log ( "Group created" , group . id ) ;
@@ -99,38 +93,32 @@ async function main() {
99
93
typeof message . content === "string" &&
100
94
message . content . startsWith ( "/add" )
101
95
) {
102
- /* Extract the group id and wallet address from the message */
103
96
const groupId = message . content . split ( " " ) [ 1 ] ;
104
97
if ( ! groupId ) {
105
98
await conversation . send ( "Please provide a group id" ) ;
106
99
return ;
107
100
}
108
- /* Get the group from the local db */
109
101
const group = client . conversations . getConversationById ( groupId ) ;
110
102
if ( ! group ) {
111
103
await conversation . send ( "Please provide a valid group id" ) ;
112
104
return ;
113
105
}
114
- /* Extract the wallet address from the message */
115
106
const walletAddress = message . content . split ( " " ) [ 2 ] ;
116
107
if ( ! walletAddress ) {
117
108
await conversation . send ( "Please provide a wallet address" ) ;
118
109
return ;
119
110
}
120
- /* Check if the user has the NFT */
121
111
const result = await checkNft ( walletAddress , "XMTPeople" ) ;
122
112
if ( ! result ) {
123
113
console . log ( "User can't be added to the group" ) ;
124
114
return ;
125
115
} else {
126
- /* Add the user to the group */
127
116
await group . addMembers ( [ walletAddress ] ) ;
128
117
await conversation . send (
129
118
`User added to the group\n- Group ID: ${ groupId } \n- Wallet Address: ${ walletAddress } ` ,
130
119
) ;
131
120
}
132
121
} else {
133
- /* Send a welcome message to the user */
134
122
await conversation . send (
135
123
"👋 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!" ,
136
124
) ;
0 commit comments