@@ -11,26 +11,32 @@ if (!ENCRYPTION_KEY) {
11
11
throw new Error ( "ENCRYPTION_KEY must be set" ) ;
12
12
}
13
13
14
+ /* Create the signer using viem and parse the encryption key for the local db */
14
15
const signer = createSigner ( WALLET_KEY ) ;
15
16
const encryptionKey = getEncryptionKeyFromHex ( ENCRYPTION_KEY ) ;
16
17
18
+ /* Set the environment to dev or production */
17
19
const env : XmtpEnv = "dev" ;
18
20
19
21
async function main ( ) {
20
22
console . log ( `Creating client on the '${ env } ' network...` ) ;
23
+ /* Initialize the xmtp client */
21
24
const client = await Client . create ( signer , encryptionKey , { env } ) ;
22
25
23
26
console . log ( "Syncing conversations..." ) ;
27
+ /* Sync the conversations from the network to update the local db */
24
28
await client . conversations . sync ( ) ;
25
29
26
30
console . log (
27
31
`Agent initialized on ${ client . accountAddress } \nSend a message on http://xmtp.chat/dm/${ client . accountAddress } ?env=${ env } ` ,
28
32
) ;
29
33
30
34
console . log ( "Waiting for messages..." ) ;
35
+ /* Stream all messages from the network */
31
36
const stream = client . conversations . streamAllMessages ( ) ;
32
37
33
38
for await ( const message of await stream ) {
39
+ /* Ignore messages from the same account or non-text messages */
34
40
if (
35
41
message ?. senderInboxId . toLowerCase ( ) === client . inboxId . toLowerCase ( ) ||
36
42
message ?. contentType ?. typeId !== "text"
@@ -42,6 +48,7 @@ async function main() {
42
48
`Received message: ${ message . content as string } by ${ message . senderInboxId } ` ,
43
49
) ;
44
50
51
+ /* Get the conversation by id */
45
52
const conversation = client . conversations . getConversationById (
46
53
message . conversationId ,
47
54
) ;
@@ -52,6 +59,7 @@ async function main() {
52
59
}
53
60
54
61
console . log ( `Sending "gm" response...` ) ;
62
+ /* Send a message to the conversation */
55
63
await conversation . send ( "gm" ) ;
56
64
57
65
console . log ( "Waiting for messages..." ) ;
0 commit comments