|
| 1 | +import { ContentTypeText } from "@xmtp/content-type-text"; |
| 2 | +import { Client, type XmtpEnv } from "@xmtp/node-sdk"; |
| 3 | +import { Alchemy, Network } from "alchemy-sdk"; |
| 4 | +import { createSigner, getEncryptionKeyFromHex } from "@/helpers"; |
| 5 | + |
| 6 | +const settings = { |
| 7 | + apiKey: process.env.ALCHEMY_API_KEY, // Replace with your Alchemy API key |
| 8 | + network: Network.BASE_MAINNET, // Use the appropriate network |
| 9 | +}; |
| 10 | + |
| 11 | +const { WALLET_KEY, ENCRYPTION_KEY } = process.env; |
| 12 | + |
| 13 | +if (!WALLET_KEY) { |
| 14 | + throw new Error("WALLET_KEY must be set"); |
| 15 | +} |
| 16 | + |
| 17 | +if (!ENCRYPTION_KEY) { |
| 18 | + throw new Error("ENCRYPTION_KEY must be set"); |
| 19 | +} |
| 20 | + |
| 21 | +const signer = createSigner(WALLET_KEY); |
| 22 | +const encryptionKey = getEncryptionKeyFromHex(ENCRYPTION_KEY); |
| 23 | + |
| 24 | +const env: XmtpEnv = "dev"; |
| 25 | + |
| 26 | +async function main() { |
| 27 | + console.log(`Creating client on the '${env}' network...`); |
| 28 | + const client = await Client.create(signer, encryptionKey, { |
| 29 | + env, |
| 30 | + }); |
| 31 | + |
| 32 | + console.log("Syncing conversations..."); |
| 33 | + await client.conversations.sync(); |
| 34 | + |
| 35 | + console.log( |
| 36 | + `Agent initialized on ${client.accountAddress}\nSend a message on http://xmtp.chat/dm/${client.accountAddress}`, |
| 37 | + ); |
| 38 | + |
| 39 | + console.log("Waiting for messages..."); |
| 40 | + const stream = client.conversations.streamAllMessages(); |
| 41 | + |
| 42 | + for await (const message of await stream) { |
| 43 | + if ( |
| 44 | + !message || |
| 45 | + !message.contentType || |
| 46 | + !ContentTypeText.sameAs(message.contentType) |
| 47 | + ) { |
| 48 | + console.log("Invalid message, skipping", message?.contentType?.typeId); |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + // Ignore own messages |
| 53 | + if (message.senderInboxId === client.inboxId) { |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + console.log( |
| 58 | + `Received message: ${message.content as string} by ${message.senderInboxId}`, |
| 59 | + ); |
| 60 | + |
| 61 | + const conversation = client.conversations.getConversationById( |
| 62 | + message.conversationId, |
| 63 | + ); |
| 64 | + |
| 65 | + if (!conversation) { |
| 66 | + console.log("Unable to find conversation, skipping"); |
| 67 | + continue; |
| 68 | + } |
| 69 | + if (message.content === "/create") { |
| 70 | + console.log("Creating group"); |
| 71 | + const group = await client.conversations.newGroup([]); |
| 72 | + console.log("Group created", group.id); |
| 73 | + // First add the sender to the group |
| 74 | + await group.addMembersByInboxId([message.senderInboxId]); |
| 75 | + // Then make the sender a super admin |
| 76 | + await group.addSuperAdmin(message.senderInboxId); |
| 77 | + console.log( |
| 78 | + "Sender is superAdmin", |
| 79 | + group.isSuperAdmin(message.senderInboxId), |
| 80 | + ); |
| 81 | + await group.send( |
| 82 | + `Welcome to the new group!\nYou are now the admin of this group as well as the bot`, |
| 83 | + ); |
| 84 | + |
| 85 | + await conversation.send( |
| 86 | + `Group created!\n- ID: ${group.id}\n- Group URL: https://xmtp.chat/conversations/${group.id}: \n- This url will deeplink to the group created\n- Once in the other group you can share the invite with your friends.\n- You can add more members to the group by using the /add <group.id> <wallet-address>.`, |
| 87 | + ); |
| 88 | + return; |
| 89 | + } else if ( |
| 90 | + typeof message.content === "string" && |
| 91 | + message.content.startsWith("/add") |
| 92 | + ) { |
| 93 | + const groupId = message.content.split(" ")[1]; |
| 94 | + if (!groupId) { |
| 95 | + await conversation.send("Please provide a group id"); |
| 96 | + return; |
| 97 | + } |
| 98 | + const group = client.conversations.getConversationById(groupId); |
| 99 | + if (!group) { |
| 100 | + await conversation.send("Please provide a valid group id"); |
| 101 | + return; |
| 102 | + } |
| 103 | + const walletAddress = message.content.split(" ")[2]; |
| 104 | + if (!walletAddress) { |
| 105 | + await conversation.send("Please provide a wallet address"); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + const result = await checkNft(walletAddress, "XMTPeople"); |
| 110 | + if (!result) { |
| 111 | + console.log("User can't be added to the group"); |
| 112 | + return; |
| 113 | + } else { |
| 114 | + await group.addMembers([walletAddress]); |
| 115 | + await conversation.send( |
| 116 | + `User added to the group\n- Group ID: ${groupId}\n- Wallet Address: ${walletAddress}`, |
| 117 | + ); |
| 118 | + } |
| 119 | + } else { |
| 120 | + await conversation.send( |
| 121 | + "👋 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!", |
| 122 | + ); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +main().catch(console.error); |
| 128 | + |
| 129 | +async function checkNft( |
| 130 | + walletAddress: string, |
| 131 | + collectionSlug: string, |
| 132 | +): Promise<boolean> { |
| 133 | + const alchemy = new Alchemy(settings); |
| 134 | + try { |
| 135 | + const nfts = await alchemy.nft.getNftsForOwner(walletAddress); |
| 136 | + |
| 137 | + const ownsNft = nfts.ownedNfts.some( |
| 138 | + (nft) => |
| 139 | + nft.contract.name?.toLowerCase() === collectionSlug.toLowerCase(), |
| 140 | + ); |
| 141 | + console.log("is the nft owned: ", ownsNft); |
| 142 | + return ownsNft; |
| 143 | + } catch (error) { |
| 144 | + console.error("Error fetching NFTs from Alchemy:", error); |
| 145 | + } |
| 146 | + |
| 147 | + return false; |
| 148 | +} |
0 commit comments