From a1f629f75a291fed0fe6f6e604ccd8d6731d44a1 Mon Sep 17 00:00:00 2001 From: fabri Date: Mon, 10 Feb 2025 20:36:30 -0300 Subject: [PATCH 1/3] Clean up text filter --- examples/gated-group/index.ts | 12 ++---------- examples/gm/README.md | 12 ++---------- examples/gm/index.ts | 12 ++---------- examples/gpt/README.md | 12 ++---------- examples/gpt/index.ts | 12 ++---------- 5 files changed, 10 insertions(+), 50 deletions(-) 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..3abd328 100644 --- a/examples/gm/README.md +++ b/examples/gm/README.md @@ -9,7 +9,6 @@ This agent replies `gm` ## Basic usage ```tsx -import { ContentTypeText } from "@xmtp/content-type-text"; import { Client, type XmtpEnv } from "@xmtp/node-sdk"; import { createSigner, getEncryptionKeyFromHex } from "@/helpers"; @@ -44,16 +43,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/gm/index.ts b/examples/gm/index.ts index 9c575a7..747a8d5 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"; @@ -33,16 +32,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/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; } From de597112cfc0c10a3db7291d71aa3f64532bc6f8 Mon Sep 17 00:00:00 2001 From: fabri Date: Mon, 10 Feb 2025 21:13:00 -0300 Subject: [PATCH 2/3] gm comments --- examples/gm/README.md | 2 +- examples/gm/index.ts | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/examples/gm/README.md b/examples/gm/README.md index 3abd328..17059ad 100644 --- a/examples/gm/README.md +++ b/examples/gm/README.md @@ -2,7 +2,7 @@ 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) diff --git a/examples/gm/index.ts b/examples/gm/index.ts index 747a8d5..4d3eda2 100644 --- a/examples/gm/index.ts +++ b/examples/gm/index.ts @@ -11,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( @@ -28,9 +32,11 @@ 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 account or non-text messages */ if ( message?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() || message?.contentType?.typeId !== "text" @@ -42,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, ); @@ -52,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..."); From 3c554b3f4368f7d6a9de31b49c3c27d54d41589d Mon Sep 17 00:00:00 2001 From: fabri Date: Tue, 11 Feb 2025 14:57:51 -0300 Subject: [PATCH 3/3] gm comments --- examples/gm/README.md | 10 +++++++++- examples/gm/index.ts | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/examples/gm/README.md b/examples/gm/README.md index 17059ad..d4729b9 100644 --- a/examples/gm/README.md +++ b/examples/gm/README.md @@ -6,7 +6,7 @@ This agent replies `gm` ![](/media/gm.png) -## Basic usage +## Overview ```tsx import { Client, type XmtpEnv } from "@xmtp/node-sdk"; @@ -22,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( @@ -39,9 +43,11 @@ 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?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() || message?.contentType?.typeId !== "text" @@ -53,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, ); @@ -63,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 4d3eda2..59ab25a 100644 --- a/examples/gm/index.ts +++ b/examples/gm/index.ts @@ -36,7 +36,7 @@ async function main() { const stream = client.conversations.streamAllMessages(); for await (const message of await stream) { - /* Ignore messages from the same account or non-text messages */ + /* Ignore messages from the same agent or non-text messages */ if ( message?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() || message?.contentType?.typeId !== "text"