1
- import {
2
- XMTP ,
3
- xmtpClient ,
4
- type Client ,
5
- type Message ,
6
- } from "@xmtp/agent-starter" ;
1
+ import { xmtpClient , type Client , type Message } from "@xmtp/agent-starter" ;
7
2
import { Alchemy , Network } from "alchemy-sdk" ;
8
- import express from "express" ;
3
+ import express , { type Request , type Response } from "express" ;
9
4
10
5
const settings = {
11
6
apiKey : process . env . ALCHEMY_API_KEY , // Replace with your Alchemy API key
@@ -44,20 +39,23 @@ async function main() {
44
39
// Endpoint to add wallet address to a group from an external source
45
40
const app = express ( ) ;
46
41
app . use ( express . json ( ) ) ;
47
- app . post ( "/add-wallet" , async ( req , res ) => {
48
- try {
49
- const { walletAddress, groupId } = req . body ;
50
- const verified = true ; // (await checkNft(walletAddress, "XMTPeople"));
51
- if ( ! verified ) {
52
- console . log ( "User cant be added to the group" ) ;
53
- return ;
54
- } else {
55
- await addToGroup ( groupId , agent . client as Client , walletAddress , true ) ;
42
+ app . post ( "/add-wallet" , ( req : Request , res : Response ) => {
43
+ const { walletAddress, groupId } = req . body as {
44
+ walletAddress : string ;
45
+ groupId : string ;
46
+ } ;
47
+ // const verified = true; // (await checkNft(walletAddress, "XMTPeople"));
48
+ // if (!verified) {
49
+ // console.log("User cant be added to the group");
50
+ // return;
51
+ // } else {
52
+ addToGroup ( groupId , agent . client as Client , walletAddress , true )
53
+ . then ( ( ) => {
56
54
res . status ( 200 ) . send ( "success" ) ;
57
- }
58
- } catch ( error : any ) {
59
- res . status ( 400 ) . send ( error . message ) ;
60
- }
55
+ } )
56
+ . catch ( ( error : unknown ) => {
57
+ res . status ( 400 ) . send ( ( error as Error ) . message ) ;
58
+ } ) ;
61
59
} ) ;
62
60
// Start the servfalcheer
63
61
const PORT = process . env . PORT || 3000 ;
@@ -85,15 +83,15 @@ export async function createGroup(
85
83
try {
86
84
let senderInboxId = "" ;
87
85
await client . conversations . sync ( ) ;
88
- const conversations = await client . conversations . list ( ) ;
86
+ const conversations = client . conversations . list ( ) ;
89
87
console . log ( "Conversations" , conversations . length ) ;
90
88
const group = await client . conversations . newGroup ( [
91
89
senderAddress ,
92
90
clientAddress ,
93
91
] ) ;
94
92
console . log ( "Group created" , group . id ) ;
95
93
const members = await group . members ( ) ;
96
- const senderMember = members . find ( ( member : any ) =>
94
+ const senderMember = members . find ( ( member ) =>
97
95
member . accountAddresses . includes ( senderAddress . toLowerCase ( ) ) ,
98
96
) ;
99
97
if ( senderMember ) {
@@ -103,10 +101,7 @@ export async function createGroup(
103
101
console . log ( "Sender not found in members list" ) ;
104
102
}
105
103
await group . addSuperAdmin ( senderInboxId ) ;
106
- console . log (
107
- "Sender is superAdmin" ,
108
- await group . isSuperAdmin ( senderInboxId ) ,
109
- ) ;
104
+ console . log ( "Sender is superAdmin" , group . isSuperAdmin ( senderInboxId ) ) ;
110
105
await group . send ( `Welcome to the new group!` ) ;
111
106
await group . send ( `You are now the admin of this group as well as the bot` ) ;
112
107
return group ;
@@ -125,12 +120,11 @@ export async function removeFromGroup(
125
120
const lowerAddress = senderAddress . toLowerCase ( ) ;
126
121
const isOnXMTP = await client . canMessage ( [ lowerAddress ] ) ;
127
122
console . warn ( "Checking if on XMTP: " , isOnXMTP ) ;
128
- if ( ! isOnXMTP ) {
123
+ if ( ! isOnXMTP . get ( lowerAddress ) ) {
129
124
console . error ( "You don't seem to have a v3 identity " ) ;
130
125
return ;
131
126
}
132
- const conversation =
133
- await client . conversations . getConversationById ( groupId ) ;
127
+ const conversation = client . conversations . getConversationById ( groupId ) ;
134
128
console . warn ( "removing from group" , conversation ?. id ) ;
135
129
await conversation ?. sync ( ) ;
136
130
await conversation ?. removeMembers ( [ lowerAddress ] ) ;
@@ -169,11 +163,11 @@ export async function addToGroup(
169
163
try {
170
164
const lowerAddress = address . toLowerCase ( ) ;
171
165
const isOnXMTP = await client . canMessage ( [ lowerAddress ] ) ;
172
- if ( ! isOnXMTP ) {
166
+ if ( ! isOnXMTP . get ( lowerAddress ) ) {
173
167
console . error ( "You don't seem to have a v3 identity " ) ;
174
168
return ;
175
169
}
176
- const group = await client . conversations . getConversationById ( groupId ) ;
170
+ const group = client . conversations . getConversationById ( groupId ) ;
177
171
console . warn ( "Adding to group" , group ?. id ) ;
178
172
await group ?. sync ( ) ;
179
173
await group ?. addMembers ( [ lowerAddress ] ) ;
@@ -209,8 +203,8 @@ export async function checkNft(
209
203
const nfts = await alchemy . nft . getNftsForOwner ( walletAddress ) ;
210
204
211
205
const ownsNft = nfts . ownedNfts . some (
212
- ( nft : any ) =>
213
- nft . contract . name . toLowerCase ( ) === collectionSlug . toLowerCase ( ) ,
206
+ ( nft ) =>
207
+ nft . contract . name ? .toLowerCase ( ) === collectionSlug . toLowerCase ( ) ,
214
208
) ;
215
209
console . log ( "is the nft owned: " , ownsNft ) ;
216
210
return ownsNft ;
0 commit comments