-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.js
71 lines (60 loc) · 1.58 KB
/
main.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
import * as dotenv from 'dotenv'
import { WebSocket } from "ws"
import { writeFileSync } from "fs"
dotenv.config()
const REQUIRED_ENV_VARS = [
"REPLIKA_CHAT_ID",
// "REPLIKA_TOKEN",
"REPLIKA_USER_ID",
"REPLIKA_AUTH_TOKEN",
"REPLIKA_DEVICE_ID",
]
REQUIRED_ENV_VARS.forEach((envVar) => {
if (!process.env[envVar]) {
throw new Error(`Missing environment variable: ${envVar}`)
}
})
const ws = new WebSocket("wss://ws.replika.com/v17")
const query = {
event_name: "history",
payload: {
chat_id: process.env.REPLIKA_CHAT_ID,
limit: 100,
},
// token: process.env.REPLIKA_TOKEN,
token: process.env.REPLIKA_AUTH_TOKEN,
auth: {
user_id: process.env.REPLIKA_USER_ID,
auth_token: process.env.REPLIKA_AUTH_TOKEN,
device_id: process.env.REPLIKA_DEVICE_ID,
},
}
let messages = []
ws.on("open", () => {
ws.send(JSON.stringify(query))
ws.on("message", (data) => {
const json = JSON.parse(data.toString())
if (!json.payload || !json.payload.messages) {
console.error("Unexpected message", json)
process.exit(1)
}
const newMessages = json.payload.messages
if (newMessages.length > 0) {
messages = [...newMessages, ...messages]
console.log(
`${newMessages.length} new messages, total: ${messages.length}`
)
const newQuery = {
...query,
payload: {
...query.payload,
last_message_id: newMessages[0].id,
},
}
ws.send(JSON.stringify(newQuery))
} else {
writeFileSync("messages.json", JSON.stringify(messages, null, 2))
ws.close()
}
})
})