Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up text filter #34

Merged
merged 3 commits into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions examples/gated-group/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
}

Expand Down
24 changes: 12 additions & 12 deletions examples/gm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -23,44 +22,44 @@ 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(
`Agent initialized on ${client.accountAddress}\nSend a message on http://xmtp.chat/dm/${client.accountAddress}?env=${env}`,
);

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;
}

console.log(
`Received message: ${message.content as string} by ${message.senderInboxId}`,
);

/* Get the conversation by id */
const conversation = client.conversations.getConversationById(
message.conversationId,
);
Expand All @@ -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...");
Expand Down
20 changes: 10 additions & 10 deletions examples/gm/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ContentTypeText } from "@xmtp/content-type-text";
import { Client, type XmtpEnv } from "@xmtp/node-sdk";
import { createSigner, getEncryptionKeyFromHex } from "@/helpers";

Expand All @@ -12,44 +11,44 @@ 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(
`Agent initialized on ${client.accountAddress}\nSend a message on http://xmtp.chat/dm/${client.accountAddress}?env=${env}`,
);

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;
}

console.log(
`Received message: ${message.content as string} by ${message.senderInboxId}`,
);

/* Get the conversation by id */
const conversation = client.conversations.getConversationById(
message.conversationId,
);
Expand All @@ -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...");
Expand Down
12 changes: 2 additions & 10 deletions examples/gpt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}

Expand Down
12 changes: 2 additions & 10 deletions examples/gpt/index.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
}

Expand Down