forked from preayham/instagram-ai-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
70 lines (53 loc) · 1.58 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
62
63
64
65
66
67
68
69
70
import "dotenv/config";
import * as fs from "fs";
import { login, getAndSend, handleLatestMessage } from "./api/instagram";
import config from "./config.json";
import { clientHandle, intervalChecks, sleep } from "./util";
const client = clientHandle(() => login({ client }));
const main = async () => {
intervalChecks({ prompt: config.prompt });
let isLogin: string;
try {
isLogin = fs.readFileSync("./login.json", "utf-8");
} catch {
fs.writeFileSync("./login.json", "");
}
if (!isLogin) {
await login({ client }).catch(() => {});
}
if (isLogin) {
await client.state.deserialize(JSON.parse(isLogin));
}
console.log(`Watching for messages. (${config.waitTime} seconds)`);
let collectedThreads: { id: string; messages: string[] }[] = [];
client.realtime.on("message", async (message) => {
const newThreads = await handleLatestMessage({
client,
message,
collectedThreads,
});
if (newThreads) {
collectedThreads = newThreads;
}
});
await client.realtime.connect({
irisData: await client.feed.directInbox().request(),
});
while (true) {
if (collectedThreads.length > 0) {
collectedThreads.forEach(async (thread) => {
collectedThreads = collectedThreads.filter(
(item) => item.id !== thread.id
);
await getAndSend({
client,
thread: thread.id,
allMessages: thread.messages,
});
console.log(`\nWatching for messages. (${config.waitTime} seconds)`);
});
}
await sleep({ seconds: config.waitTime });
}
};
main();