Skip to content

Commit b118256

Browse files
authored
Clean up text filter (#34)
* Clean up text filter * gm comments * gm comments
1 parent 7dce702 commit b118256

File tree

5 files changed

+28
-52
lines changed

5 files changed

+28
-52
lines changed

examples/gated-group/index.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { ContentTypeText } from "@xmtp/content-type-text";
21
import { Client, type XmtpEnv } from "@xmtp/node-sdk";
32
import { Alchemy, Network } from "alchemy-sdk";
43
import { createSigner, getEncryptionKeyFromHex } from "@/helpers";
@@ -41,16 +40,9 @@ async function main() {
4140

4241
for await (const message of await stream) {
4342
if (
44-
!message ||
45-
!message.contentType ||
46-
!ContentTypeText.sameAs(message.contentType)
43+
message?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() ||
44+
message?.contentType?.typeId !== "text"
4745
) {
48-
console.log("Invalid message, skipping", message?.contentType?.typeId);
49-
continue;
50-
}
51-
52-
// Ignore own messages
53-
if (message.senderInboxId === client.inboxId) {
5446
continue;
5547
}
5648

examples/gm/README.md

+12-12
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22

33
This agent replies `gm`
44

5-
> Try XMTP using [xmtp.chat](https://xmtp.chat)
5+
> Try XMTP using [xmtp.chat](https://xmtp.chat) and sending a message to `gm.xmtp.eth`
66
77
![](/media/gm.png)
88

9-
## Basic usage
9+
## Overview
1010

1111
```tsx
12-
import { ContentTypeText } from "@xmtp/content-type-text";
1312
import { Client, type XmtpEnv } from "@xmtp/node-sdk";
1413
import { createSigner, getEncryptionKeyFromHex } from "@/helpers";
1514

@@ -23,44 +22,44 @@ if (!ENCRYPTION_KEY) {
2322
throw new Error("ENCRYPTION_KEY must be set");
2423
}
2524

25+
/* Create the signer using viem and parse the encryption key for the local db */
2626
const signer = createSigner(WALLET_KEY);
2727
const encryptionKey = getEncryptionKeyFromHex(ENCRYPTION_KEY);
2828

29+
/* Set the environment to dev or production */
2930
const env: XmtpEnv = "dev";
3031

3132
async function main() {
3233
console.log(`Creating client on the '${env}' network...`);
34+
/* Initialize the xmtp client */
3335
const client = await Client.create(signer, encryptionKey, { env });
3436

3537
console.log("Syncing conversations...");
38+
/* Sync the conversations from the network to update the local db */
3639
await client.conversations.sync();
3740

3841
console.log(
3942
`Agent initialized on ${client.accountAddress}\nSend a message on http://xmtp.chat/dm/${client.accountAddress}?env=${env}`,
4043
);
4144

4245
console.log("Waiting for messages...");
46+
/* Stream all messages from the network */
4347
const stream = client.conversations.streamAllMessages();
4448

4549
for await (const message of await stream) {
50+
/* Ignore messages from the same agent or non-text messages */
4651
if (
47-
!message ||
48-
!message.contentType ||
49-
!ContentTypeText.sameAs(message.contentType)
52+
message?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() ||
53+
message?.contentType?.typeId !== "text"
5054
) {
51-
console.log("Invalid message, skipping", message);
52-
continue;
53-
}
54-
55-
// Ignore own messages
56-
if (message.senderInboxId === client.inboxId) {
5755
continue;
5856
}
5957

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

62+
/* Get the conversation by id */
6463
const conversation = client.conversations.getConversationById(
6564
message.conversationId,
6665
);
@@ -71,6 +70,7 @@ async function main() {
7170
}
7271

7372
console.log(`Sending "gm" response...`);
73+
/* Send a message to the conversation */
7474
await conversation.send("gm");
7575

7676
console.log("Waiting for messages...");

examples/gm/index.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { ContentTypeText } from "@xmtp/content-type-text";
21
import { Client, type XmtpEnv } from "@xmtp/node-sdk";
32
import { createSigner, getEncryptionKeyFromHex } from "@/helpers";
43

@@ -12,44 +11,44 @@ if (!ENCRYPTION_KEY) {
1211
throw new Error("ENCRYPTION_KEY must be set");
1312
}
1413

14+
/* Create the signer using viem and parse the encryption key for the local db */
1515
const signer = createSigner(WALLET_KEY);
1616
const encryptionKey = getEncryptionKeyFromHex(ENCRYPTION_KEY);
1717

18+
/* Set the environment to dev or production */
1819
const env: XmtpEnv = "dev";
1920

2021
async function main() {
2122
console.log(`Creating client on the '${env}' network...`);
23+
/* Initialize the xmtp client */
2224
const client = await Client.create(signer, encryptionKey, { env });
2325

2426
console.log("Syncing conversations...");
27+
/* Sync the conversations from the network to update the local db */
2528
await client.conversations.sync();
2629

2730
console.log(
2831
`Agent initialized on ${client.accountAddress}\nSend a message on http://xmtp.chat/dm/${client.accountAddress}?env=${env}`,
2932
);
3033

3134
console.log("Waiting for messages...");
35+
/* Stream all messages from the network */
3236
const stream = client.conversations.streamAllMessages();
3337

3438
for await (const message of await stream) {
39+
/* Ignore messages from the same agent or non-text messages */
3540
if (
36-
!message ||
37-
!message.contentType ||
38-
!ContentTypeText.sameAs(message.contentType)
41+
message?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() ||
42+
message?.contentType?.typeId !== "text"
3943
) {
40-
console.log("Invalid message, skipping", message);
41-
continue;
42-
}
43-
44-
// Ignore own messages
45-
if (message.senderInboxId === client.inboxId) {
4644
continue;
4745
}
4846

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

51+
/* Get the conversation by id */
5352
const conversation = client.conversations.getConversationById(
5453
message.conversationId,
5554
);
@@ -60,6 +59,7 @@ async function main() {
6059
}
6160

6261
console.log(`Sending "gm" response...`);
62+
/* Send a message to the conversation */
6363
await conversation.send("gm");
6464

6565
console.log("Waiting for messages...");

examples/gpt/README.md

+2-10
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ yarn gen:keys
2424
## Usage
2525

2626
```tsx
27-
import { ContentTypeText } from "@xmtp/content-type-text";
2827
import { Client, type XmtpEnv } from "@xmtp/node-sdk";
2928
import OpenAI from "openai";
3029
import { createSigner, getEncryptionKeyFromHex } from "@/helpers";
@@ -67,16 +66,9 @@ async function main() {
6766

6867
for await (const message of await stream) {
6968
if (
70-
!message ||
71-
!message.contentType ||
72-
!ContentTypeText.sameAs(message.contentType)
69+
message?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() ||
70+
message?.contentType?.typeId !== "text"
7371
) {
74-
console.log("Invalid message, skipping", message);
75-
continue;
76-
}
77-
78-
// Ignore own messages
79-
if (message.senderInboxId === client.inboxId) {
8072
continue;
8173
}
8274

examples/gpt/index.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { ContentTypeText } from "@xmtp/content-type-text";
21
import { Client, type XmtpEnv } from "@xmtp/node-sdk";
32
import OpenAI from "openai";
43
import { createSigner, getEncryptionKeyFromHex } from "@/helpers";
@@ -41,16 +40,9 @@ async function main() {
4140

4241
for await (const message of await stream) {
4342
if (
44-
!message ||
45-
!message.contentType ||
46-
!ContentTypeText.sameAs(message.contentType)
43+
message?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() ||
44+
message?.contentType?.typeId !== "text"
4745
) {
48-
console.log("Invalid message, skipping", message);
49-
continue;
50-
}
51-
52-
// Ignore own messages
53-
if (message.senderInboxId === client.inboxId) {
5446
continue;
5547
}
5648

0 commit comments

Comments
 (0)