diff --git a/examples/gated-group/index.ts b/examples/gated-group/index.ts index d404bd8..cb941e4 100644 --- a/examples/gated-group/index.ts +++ b/examples/gated-group/index.ts @@ -1,4 +1,3 @@ -import { ContentTypeText } from "@xmtp/content-type-text"; import { Client, type XmtpEnv } from "@xmtp/node-sdk"; import { Alchemy, Network } from "alchemy-sdk"; import { createSigner, getEncryptionKeyFromHex } from "@/helpers"; @@ -41,16 +40,9 @@ async function main() { for await (const message of await stream) { if ( - !message || - !message.contentType || - !ContentTypeText.sameAs(message.contentType) + message?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() || + message?.contentType?.typeId !== "text" ) { - console.log("Invalid message, skipping", message?.contentType?.typeId); - continue; - } - - // Ignore own messages - if (message.senderInboxId === client.inboxId) { continue; } diff --git a/examples/gm/README.md b/examples/gm/README.md index ac8c3af..d4729b9 100644 --- a/examples/gm/README.md +++ b/examples/gm/README.md @@ -2,14 +2,13 @@ This agent replies `gm` -> Try XMTP using [xmtp.chat](https://xmtp.chat) +> Try XMTP using [xmtp.chat](https://xmtp.chat) and sending a message to `gm.xmtp.eth` ![](/media/gm.png) -## Basic usage +## Overview ```tsx -import { ContentTypeText } from "@xmtp/content-type-text"; import { Client, type XmtpEnv } from "@xmtp/node-sdk"; import { createSigner, getEncryptionKeyFromHex } from "@/helpers"; @@ -23,16 +22,20 @@ if (!ENCRYPTION_KEY) { throw new Error("ENCRYPTION_KEY must be set"); } +/* Create the signer using viem and parse the encryption key for the local db */ const signer = createSigner(WALLET_KEY); const encryptionKey = getEncryptionKeyFromHex(ENCRYPTION_KEY); +/* Set the environment to dev or production */ const env: XmtpEnv = "dev"; async function main() { console.log(`Creating client on the '${env}' network...`); + /* Initialize the xmtp client */ const client = await Client.create(signer, encryptionKey, { env }); console.log("Syncing conversations..."); + /* Sync the conversations from the network to update the local db */ await client.conversations.sync(); console.log( @@ -40,20 +43,15 @@ async function main() { ); console.log("Waiting for messages..."); + /* Stream all messages from the network */ const stream = client.conversations.streamAllMessages(); for await (const message of await stream) { + /* Ignore messages from the same agent or non-text messages */ if ( - !message || - !message.contentType || - !ContentTypeText.sameAs(message.contentType) + message?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() || + message?.contentType?.typeId !== "text" ) { - console.log("Invalid message, skipping", message); - continue; - } - - // Ignore own messages - if (message.senderInboxId === client.inboxId) { continue; } @@ -61,6 +59,7 @@ async function main() { `Received message: ${message.content as string} by ${message.senderInboxId}`, ); + /* Get the conversation by id */ const conversation = client.conversations.getConversationById( message.conversationId, ); @@ -71,6 +70,7 @@ async function main() { } console.log(`Sending "gm" response...`); + /* Send a message to the conversation */ await conversation.send("gm"); console.log("Waiting for messages..."); diff --git a/examples/gm/index.ts b/examples/gm/index.ts index 9c575a7..59ab25a 100644 --- a/examples/gm/index.ts +++ b/examples/gm/index.ts @@ -1,4 +1,3 @@ -import { ContentTypeText } from "@xmtp/content-type-text"; import { Client, type XmtpEnv } from "@xmtp/node-sdk"; import { createSigner, getEncryptionKeyFromHex } from "@/helpers"; @@ -12,16 +11,20 @@ if (!ENCRYPTION_KEY) { throw new Error("ENCRYPTION_KEY must be set"); } +/* Create the signer using viem and parse the encryption key for the local db */ const signer = createSigner(WALLET_KEY); const encryptionKey = getEncryptionKeyFromHex(ENCRYPTION_KEY); +/* Set the environment to dev or production */ const env: XmtpEnv = "dev"; async function main() { console.log(`Creating client on the '${env}' network...`); + /* Initialize the xmtp client */ const client = await Client.create(signer, encryptionKey, { env }); console.log("Syncing conversations..."); + /* Sync the conversations from the network to update the local db */ await client.conversations.sync(); console.log( @@ -29,20 +32,15 @@ async function main() { ); console.log("Waiting for messages..."); + /* Stream all messages from the network */ const stream = client.conversations.streamAllMessages(); for await (const message of await stream) { + /* Ignore messages from the same agent or non-text messages */ if ( - !message || - !message.contentType || - !ContentTypeText.sameAs(message.contentType) + message?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() || + message?.contentType?.typeId !== "text" ) { - console.log("Invalid message, skipping", message); - continue; - } - - // Ignore own messages - if (message.senderInboxId === client.inboxId) { continue; } @@ -50,6 +48,7 @@ async function main() { `Received message: ${message.content as string} by ${message.senderInboxId}`, ); + /* Get the conversation by id */ const conversation = client.conversations.getConversationById( message.conversationId, ); @@ -60,6 +59,7 @@ async function main() { } console.log(`Sending "gm" response...`); + /* Send a message to the conversation */ await conversation.send("gm"); console.log("Waiting for messages..."); diff --git a/examples/gpt/README.md b/examples/gpt/README.md index 9581925..512064b 100644 --- a/examples/gpt/README.md +++ b/examples/gpt/README.md @@ -24,7 +24,6 @@ yarn gen:keys ## Usage ```tsx -import { ContentTypeText } from "@xmtp/content-type-text"; import { Client, type XmtpEnv } from "@xmtp/node-sdk"; import OpenAI from "openai"; import { createSigner, getEncryptionKeyFromHex } from "@/helpers"; @@ -67,16 +66,9 @@ async function main() { for await (const message of await stream) { if ( - !message || - !message.contentType || - !ContentTypeText.sameAs(message.contentType) + message?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() || + message?.contentType?.typeId !== "text" ) { - console.log("Invalid message, skipping", message); - continue; - } - - // Ignore own messages - if (message.senderInboxId === client.inboxId) { continue; } diff --git a/examples/gpt/index.ts b/examples/gpt/index.ts index 953bef6..6f11a0d 100644 --- a/examples/gpt/index.ts +++ b/examples/gpt/index.ts @@ -1,4 +1,3 @@ -import { ContentTypeText } from "@xmtp/content-type-text"; import { Client, type XmtpEnv } from "@xmtp/node-sdk"; import OpenAI from "openai"; import { createSigner, getEncryptionKeyFromHex } from "@/helpers"; @@ -41,16 +40,9 @@ async function main() { for await (const message of await stream) { if ( - !message || - !message.contentType || - !ContentTypeText.sameAs(message.contentType) + message?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() || + message?.contentType?.typeId !== "text" ) { - console.log("Invalid message, skipping", message); - continue; - } - - // Ignore own messages - if (message.senderInboxId === client.inboxId) { continue; }