-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
127 lines (119 loc) · 4.08 KB
/
index.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import "dotenv/config";
import { Client } from "@xmtp/mls-client";
import * as fs from "fs";
import { createWalletClient, http } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { mainnet } from "viem/chains";
import { toBytes } from "viem";
import { generatePrivateKey } from "viem/accounts";
// Function to send a message to a specific group
async function sendMessageToGroup(client, groupId, messageContent) {
const conversation = client.conversations.getConversationById(groupId);
if (!conversation) {
console.log(`No conversation found with ID: ${groupId}`);
return;
}
await conversation.send(messageContent);
console.log(`Message sent to group ${groupId}: ${messageContent}`);
}
// Function to create a wallet from a private key
async function createWallet() {
let key = process.env.KEY;
if (!key) {
key = generatePrivateKey();
console.error(
"KEY not set. Using random one. For using your own wallet , set the KEY environment variable.",
);
console.log("Random private key: ", key);
}
const account = privateKeyToAccount(key);
const wallet = createWalletClient({
account,
chain: mainnet,
transport: http(),
});
console.log(`Init wallet ${account.address}`);
return wallet;
}
// Function to create and setup the XMTP client
async function setupClient(wallet, config = {}) {
let initialConfig = {
env: "production",
};
const finalConfig = { ...initialConfig, ...config };
const client = await Client.create(wallet.account?.address, finalConfig);
console.log("Inbox id: ", client.inboxId);
return client;
}
// Function to register the client if not already registered
async function registerClient(client, wallet) {
// register identity
if (!client.isRegistered && client.signatureText) {
const signatureText = await client.signatureText();
if (signatureText) {
const signature = await wallet.signMessage({
message: signatureText,
});
const signatureBytes = toBytes(signature);
if (signatureBytes) {
client.addSignature(signatureBytes);
}
}
await client.registerIdentity();
}
}
// Function to handle conversations
async function handleConversations(client) {
await client.conversations.sync();
const conversations = await client.conversations.list();
console.log(`Total conversations: ${conversations.length}`);
for (const conv of conversations) {
console.log(`Handling conversation with ID: ${conv.id}`);
await conv.sync();
const messages = await conv.messages();
console.log(`Total messages in conversation: ${messages.length}`);
for (let i = 0; i < messages.length; i++) {
console.log(`Message ${i}: ${messages[i].content}`);
}
}
}
// Function to stream all messages and respond to new ones
async function streamAndRespond(client) {
console.log("Started streaming messages");
const stream = await client.conversations.streamAllMessages();
for await (const message of stream) {
console.log(`Streamed message: ${message.content}`);
if (message.senderInboxId !== client.inboxId) {
sendMessageToGroup(client, message.conversationId, "gm");
}
}
}
async function createGroupConversation(client) {
const conversation = await client.conversations.newConversation([
"0x277C0dd35520dB4aaDDB45d4690aB79353D3368b",
"0x13956e5424b9ce4E6C3ca8C070AFff329B371784",
]);
console.log(conversation.id);
}
// Main function to run the application
async function main() {
// Create a new wallet instance
const wallet = await createWallet();
// Set up the XMTP client with the wallet and database path
if (!fs.existsSync(`.data`)) {
fs.mkdirSync(`.data`);
}
const client = await setupClient(wallet, {
dbPath: `.data/${wallet.account?.address}-${"prod"}`,
});
// Register the client with the XMTP network if not already registered
await registerClient(client, wallet);
// Handle existing conversations
await handleConversations(client);
// Run message streaming in a parallel thread to respond to new messages
(async () => {
await streamAndRespond(client);
})();
}
// Example usage
main();