-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
103 lines (89 loc) · 2.91 KB
/
app.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
// Require the Bolt package (github.com/slackapi/bolt)
const { App } = require("@slack/bolt");
const fetch = require("node-fetch");
const { Configuration, OpenAIApi } = require("openai");
const OpenAIConversationHandler = require("./OpenAIConversationHandler");
const ErrorReply = require("./ErrorReply");
const app = new App({
token: process.env.SLACK_BOT_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
});
// All the room in the world for your code
(async () => {
// Start your app
await app.start(process.env.PORT || 3000);
console.log("⚡️ Bolt app is running!");
})();
app.message("", async ({ message, say }) => {
let outputMessage = null;
try {
//console.log(message.thread_ts);
// console.log("user ID: "+message.user)
var conversationHistory = [];
var history = 30;
//if message is in a thread
if (message.thread_ts) {
const result = await app.client.conversations.replies({
token: process.env.SLACK_BOT_TOKEN,
channel: message.channel,
ts: message.thread_ts,
});
conversationHistory = result.messages.reverse();
//if messate is new message in main chat window:
} else {
const result = await app.client.conversations.history({
token: process.env.SLACK_BOT_TOKEN,
channel: message.channel,
});
conversationHistory = result.messages;
history = 1;
}
let conversation = new OpenAIConversationHandler(
conversationHistory,
history
);
let openAIPrompt = conversation.getConversation();
outputMessage = await app.client.chat.postMessage({
token: process.env.SLACK_BOT_TOKEN,
text: ":hourglass_flowing_sand:",
channel: message.channel,
thread_ts: message.ts,
});
// Print results
console.log(openAIPrompt);
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: openAIPrompt,
max_tokens: 1000,
temperature: 0.9,
});
var textResponse = response.data.choices[0].text;
var tokens = response.data.usage.prompt_tokens;
const updateChatWitOpenAIResult = await app.client.chat.update({
token: process.env.SLACK_BOT_TOKEN,
channel: message.channel,
ts: outputMessage.ts,
text: textResponse,
});
// await console.log("Tokens used: " + tokens);
} catch (error) {
console.error(error);
let errorMessage = new ErrorReply(app, message, outputMessage);
errorMessage.postEmphemeralMessage();
}
});
/*
app.command("/chat_gpt_ask", async ({ command, ack, respond }) => {
await ack();
await respond("currently not working");
});
app.command("/chat_gpt_reset_context", async ({ command, ack, respond }) => {
//console.log(command.user_id)
await ack();
await respond("----History Purged----");
});
*/