@@ -6,7 +6,7 @@ This agent replies `gm`
6
6
7
7
![ ] ( /media/gm.png )
8
8
9
- ## Basic usage
9
+ ## Overview
10
10
11
11
``` tsx
12
12
import { Client , type XmtpEnv } from " @xmtp/node-sdk" ;
@@ -22,26 +22,32 @@ if (!ENCRYPTION_KEY) {
22
22
throw new Error (" ENCRYPTION_KEY must be set" );
23
23
}
24
24
25
+ /* Create the signer using viem and parse the encryption key for the local db */
25
26
const signer = createSigner (WALLET_KEY );
26
27
const encryptionKey = getEncryptionKeyFromHex (ENCRYPTION_KEY );
27
28
29
+ /* Set the environment to dev or production */
28
30
const env: XmtpEnv = " dev" ;
29
31
30
32
async function main() {
31
33
console .log (` Creating client on the '${env }' network... ` );
34
+ /* Initialize the xmtp client */
32
35
const client = await Client .create (signer , encryptionKey , { env });
33
36
34
37
console .log (" Syncing conversations..." );
38
+ /* Sync the conversations from the network to update the local db */
35
39
await client .conversations .sync ();
36
40
37
41
console .log (
38
42
` Agent initialized on ${client .accountAddress }\n Send a message on http://xmtp.chat/dm/${client .accountAddress }?env=${env } ` ,
39
43
);
40
44
41
45
console .log (" Waiting for messages..." );
46
+ /* Stream all messages from the network */
42
47
const stream = client .conversations .streamAllMessages ();
43
48
44
49
for await (const message of await stream ) {
50
+ /* Ignore messages from the same agent or non-text messages */
45
51
if (
46
52
message ?.senderInboxId .toLowerCase () === client .inboxId .toLowerCase () ||
47
53
message ?.contentType ?.typeId !== " text"
@@ -53,6 +59,7 @@ async function main() {
53
59
` Received message: ${message .content as string } by ${message .senderInboxId } ` ,
54
60
);
55
61
62
+ /* Get the conversation by id */
56
63
const conversation = client .conversations .getConversationById (
57
64
message .conversationId ,
58
65
);
@@ -63,6 +70,7 @@ async function main() {
63
70
}
64
71
65
72
console .log (` Sending "gm" response... ` );
73
+ /* Send a message to the conversation */
66
74
await conversation .send (" gm" );
67
75
68
76
console .log (" Waiting for messages..." );
0 commit comments