1
- import { Client , Message , XMTP , xmtpClient } from "@xmtp/agent-starter" ;
2
- import express from "express" ;
1
+ import { xmtpClient , type Client , type Message } from "@xmtp/agent-starter" ;
3
2
import { Alchemy , Network } from "alchemy-sdk" ;
3
+ import express , { type Request , type Response } from "express" ;
4
4
5
5
const settings = {
6
6
apiKey : process . env . ALCHEMY_API_KEY , // Replace with your Alchemy API key
@@ -13,24 +13,26 @@ async function main() {
13
13
onMessage : async ( message : Message ) => {
14
14
if ( message . typeId !== "text" ) return ;
15
15
16
- if ( message ? .content . text === "/create" ) {
16
+ if ( message . content . text === "/create" ) {
17
17
console . log ( "Creating group" ) ;
18
18
const group = await createGroup (
19
- agent ? .client ,
20
- message ? .sender ? .address as string ,
21
- agent ? .address as string ,
19
+ agent . client ,
20
+ message . sender . address ,
21
+ agent . address as string ,
22
22
) ;
23
23
console . log ( "Group created" , group ?. id ) ;
24
24
await agent . send ( {
25
25
message : `Group created!\n- ID: ${ group ?. id } \n- Group URL: https://converse.xyz/group/${ group ?. id } : \n- This url will deelink to the group inside Converse\n- Once in the other group you can share the invite with your friends.` ,
26
26
originalMessage : message ,
27
+ metadata : { } ,
27
28
} ) ;
28
29
return ;
29
30
} else {
30
31
await agent . send ( {
31
32
message :
32
33
"👋 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!" ,
33
34
originalMessage : message ,
35
+ metadata : { } ,
34
36
} ) ;
35
37
}
36
38
} ,
@@ -39,20 +41,23 @@ async function main() {
39
41
// Endpoint to add wallet address to a group from an external source
40
42
const app = express ( ) ;
41
43
app . use ( express . json ( ) ) ;
42
- app . post ( "/add-wallet" , async ( req , res ) => {
43
- try {
44
- const { walletAddress, groupId } = req . body ;
45
- const verified = true ; // (await checkNft(walletAddress, "XMTPeople"));
46
- if ( ! verified ) {
47
- console . log ( "User cant be added to the group" ) ;
48
- return ;
49
- } else {
50
- await addToGroup ( groupId , agent ?. client as Client , walletAddress , true ) ;
44
+ app . post ( "/add-wallet" , ( req : Request , res : Response ) => {
45
+ const { walletAddress, groupId } = req . body as {
46
+ walletAddress : string ;
47
+ groupId : string ;
48
+ } ;
49
+ // const verified = true; // (await checkNft(walletAddress, "XMTPeople"));
50
+ // if (!verified) {
51
+ // console.log("User cant be added to the group");
52
+ // return;
53
+ // } else {
54
+ addToGroup ( groupId , agent . client as Client , walletAddress , true )
55
+ . then ( ( ) => {
51
56
res . status ( 200 ) . send ( "success" ) ;
52
- }
53
- } catch ( error : any ) {
54
- res . status ( 400 ) . send ( error . message ) ;
55
- }
57
+ } )
58
+ . catch ( ( error : unknown ) => {
59
+ res . status ( 400 ) . send ( ( error as Error ) . message ) ;
60
+ } ) ;
56
61
} ) ;
57
62
// Start the servfalcheer
58
63
const PORT = process . env . PORT || 3000 ;
@@ -63,7 +68,7 @@ async function main() {
63
68
) ;
64
69
} ) ;
65
70
console . log (
66
- `XMTP agent initialized on ${ agent ? .address } \nSend a message on https://xmtp.chat or https://converse.xyz/dm/${ agent ? .address } ` ,
71
+ `XMTP agent initialized on ${ agent . address } \nSend a message on https://xmtp.chat or https://converse.xyz/dm/${ agent . address } ` ,
67
72
) ;
68
73
}
69
74
@@ -80,15 +85,15 @@ export async function createGroup(
80
85
try {
81
86
let senderInboxId = "" ;
82
87
await client . conversations . sync ( ) ;
83
- const conversations = await client . conversations . list ( ) ;
88
+ const conversations = client . conversations . list ( ) ;
84
89
console . log ( "Conversations" , conversations . length ) ;
85
- const group = await client ? .conversations . newGroup ( [
90
+ const group = await client . conversations . newGroup ( [
86
91
senderAddress ,
87
92
clientAddress ,
88
93
] ) ;
89
- console . log ( "Group created" , group ? .id ) ;
94
+ console . log ( "Group created" , group . id ) ;
90
95
const members = await group . members ( ) ;
91
- const senderMember = members . find ( ( member : any ) =>
96
+ const senderMember = members . find ( ( member ) =>
92
97
member . accountAddresses . includes ( senderAddress . toLowerCase ( ) ) ,
93
98
) ;
94
99
if ( senderMember ) {
@@ -98,10 +103,7 @@ export async function createGroup(
98
103
console . log ( "Sender not found in members list" ) ;
99
104
}
100
105
await group . addSuperAdmin ( senderInboxId ) ;
101
- console . log (
102
- "Sender is superAdmin" ,
103
- await group . isSuperAdmin ( senderInboxId ) ,
104
- ) ;
106
+ console . log ( "Sender is superAdmin" , group . isSuperAdmin ( senderInboxId ) ) ;
105
107
await group . send ( `Welcome to the new group!` ) ;
106
108
await group . send ( `You are now the admin of this group as well as the bot` ) ;
107
109
return group ;
@@ -117,15 +119,14 @@ export async function removeFromGroup(
117
119
senderAddress : string ,
118
120
) : Promise < void > {
119
121
try {
120
- let lowerAddress = senderAddress . toLowerCase ( ) ;
122
+ const lowerAddress = senderAddress . toLowerCase ( ) ;
121
123
const isOnXMTP = await client . canMessage ( [ lowerAddress ] ) ;
122
124
console . warn ( "Checking if on XMTP: " , isOnXMTP ) ;
123
- if ( ! isOnXMTP ) {
125
+ if ( ! isOnXMTP . get ( lowerAddress ) ) {
124
126
console . error ( "You don't seem to have a v3 identity " ) ;
125
127
return ;
126
128
}
127
- const conversation =
128
- await client . conversations . getConversationById ( groupId ) ;
129
+ const conversation = client . conversations . getConversationById ( groupId ) ;
129
130
console . warn ( "removing from group" , conversation ?. id ) ;
130
131
await conversation ?. sync ( ) ;
131
132
await conversation ?. removeMembers ( [ lowerAddress ] ) ;
@@ -137,7 +138,7 @@ export async function removeFromGroup(
137
138
let wasRemoved = true ;
138
139
if ( members ) {
139
140
for ( const member of members ) {
140
- let lowerMemberAddress = member . accountAddresses [ 0 ] . toLowerCase ( ) ;
141
+ const lowerMemberAddress = member . accountAddresses [ 0 ] . toLowerCase ( ) ;
141
142
if ( lowerMemberAddress === lowerAddress ) {
142
143
wasRemoved = false ;
143
144
break ;
@@ -162,13 +163,13 @@ export async function addToGroup(
162
163
asAdmin : boolean = false ,
163
164
) : Promise < void > {
164
165
try {
165
- let lowerAddress = address . toLowerCase ( ) ;
166
+ const lowerAddress = address . toLowerCase ( ) ;
166
167
const isOnXMTP = await client . canMessage ( [ lowerAddress ] ) ;
167
- if ( ! isOnXMTP ) {
168
+ if ( ! isOnXMTP . get ( lowerAddress ) ) {
168
169
console . error ( "You don't seem to have a v3 identity " ) ;
169
170
return ;
170
171
}
171
- const group = await client . conversations . getConversationById ( groupId ) ;
172
+ const group = client . conversations . getConversationById ( groupId ) ;
172
173
console . warn ( "Adding to group" , group ?. id ) ;
173
174
await group ?. sync ( ) ;
174
175
await group ?. addMembers ( [ lowerAddress ] ) ;
@@ -182,7 +183,7 @@ export async function addToGroup(
182
183
183
184
if ( members ) {
184
185
for ( const member of members ) {
185
- let lowerMemberAddress = member . accountAddresses [ 0 ] . toLowerCase ( ) ;
186
+ const lowerMemberAddress = member . accountAddresses [ 0 ] . toLowerCase ( ) ;
186
187
if ( lowerMemberAddress === lowerAddress ) {
187
188
console . warn ( "Member exists" , lowerMemberAddress ) ;
188
189
return ;
@@ -204,11 +205,11 @@ export async function checkNft(
204
205
const nfts = await alchemy . nft . getNftsForOwner ( walletAddress ) ;
205
206
206
207
const ownsNft = nfts . ownedNfts . some (
207
- ( nft : any ) =>
208
- nft . contract . name . toLowerCase ( ) === collectionSlug . toLowerCase ( ) ,
208
+ ( nft ) =>
209
+ nft . contract . name ? .toLowerCase ( ) === collectionSlug . toLowerCase ( ) ,
209
210
) ;
210
211
console . log ( "is the nft owned: " , ownsNft ) ;
211
- return ownsNft as boolean ;
212
+ return ownsNft ;
212
213
} catch ( error ) {
213
214
console . error ( "Error fetching NFTs from Alchemy:" , error ) ;
214
215
}
0 commit comments