-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.mjs
112 lines (103 loc) · 3.19 KB
/
index.mjs
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
import "dotenv/config";
import { ChatGPTAPI } from "chatgpt";
import bolt from "@slack/bolt";
import { appendFile, readFile, writeFile } from "fs/promises";
import { join } from "path";
const SESSION_FILE = join("data", "sessions.txt");
const app = new bolt.App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
socketMode: true,
appToken: process.env.SLACK_APP_TOKEN,
extendedErrorHandler: true,
});
const api = new ChatGPTAPI({
apiKey: process.env.OPENAI_API_KEY,
});
async function startNewSession(channel, tsOrThreadTs, receivedText) {
let res;
try {
res = await api.sendMessage(receivedText);
} catch (e) {
console.log(e);
await app.client.chat.postMessage({
channel: channel,
thread_ts: tsOrThreadTs,
text: e.message,
});
return;
}
await appendFile(
SESSION_FILE,
`${channel} ${tsOrThreadTs} ${res.conversationId} ${res.id}\n`
);
console.log(`Created session: ${channel} ${tsOrThreadTs} ${res.conversationId} ${res.id}`);
console.log(`Message received: ${receivedText}, response: ${res.text}`)
await app.client.chat.postMessage({
channel: channel,
thread_ts: tsOrThreadTs,
text: res.text,
});
}
async function findExistingSession(channel, threadTs) {
const sessions = await readFile(SESSION_FILE, "utf-8");
const session = sessions.split("\n").find((session) => {
const [channelHere, threadTsHere] = session.split(" ");
return channel === channelHere && threadTs === threadTsHere;
});
if (!session) return null
console.log(`Found session: ${session}`);
return session;
}
async function followUp(channel, threadTs, conversationId, parentMessageId, receivedText) {
let res;
try {
res = await api.sendMessage(receivedText, {
conversationId,
parentMessageId,
});
} catch (e) {
console.log(e);
await app.client.chat.postMessage({
channel,
thread_ts: threadTs,
text: e.message,
});
return;
}
const sessions = await readFile(SESSION_FILE, "utf-8");
await writeFile(
SESSION_FILE,
sessions.replace(
`${channel} ${threadTs} ${res.conversationId} ${parentMessageId}\n`,
`${channel} ${threadTs} ${res.conversationId} ${res.id}\n`
)
);
console.log(`Updated session: ${channel} ${threadTs} ${res.conversationId} ${res.id}`);
console.log(`Message received: ${receivedText}, response: ${res.text}`);
await app.client.chat.postMessage({
channel,
thread_ts: threadTs,
text: res.text,
});
}
app.event("app_mention", async ({ event }) => {
const threadTs = event.thread_ts;
if (threadTs) { // If the message is in a thread
const session = await findExistingSession(event.channel, threadTs);
if (session) {
return;
}
}
await startNewSession(event.channel, threadTs || event.ts, event.text);
});
app.event("message", async ({ event }) => {
const threadTs = event.thread_ts;
if (!threadTs) return;
const session = await findExistingSession(event.channel, threadTs);
if (!session) return;
const [channel, ts, conversationId, id] = session.split(" ");
await followUp(channel, ts, conversationId, id, event.text);
});
await app.start();
console.log("Slack ChatGPT bot is running!");