-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.ts
61 lines (45 loc) · 1.6 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { Client, type XmtpEnv } from "@xmtp/node-sdk";
import { createSigner, getEncryptionKeyFromHex } from "@/helpers";
const { WALLET_KEY, ENCRYPTION_KEY } = process.env;
if (!WALLET_KEY) {
throw new Error("WALLET_KEY must be set");
}
if (!ENCRYPTION_KEY) {
throw new Error("ENCRYPTION_KEY must be set");
}
const signer = createSigner(WALLET_KEY);
const encryptionKey = getEncryptionKeyFromHex(ENCRYPTION_KEY);
const env: XmtpEnv = "dev";
async function main() {
console.log(`Creating client on the '${env}' network...`);
const client = await Client.create(signer, encryptionKey, { env });
console.log("Syncing conversations...");
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...");
const stream = client.conversations.streamAllMessages();
for await (const message of await stream) {
if (
message?.senderInboxId.toLowerCase() === client.inboxId.toLowerCase() ||
message?.contentType?.typeId !== "text"
) {
continue;
}
console.log(
`Received message: ${message.content as string} by ${message.senderInboxId}`,
);
const conversation = client.conversations.getConversationById(
message.conversationId,
);
if (!conversation) {
console.log("Unable to find conversation, skipping");
continue;
}
console.log(`Sending "gm" response...`);
await conversation.send("gm");
console.log("Waiting for messages...");
}
}
main().catch(console.error);