-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
83 lines (73 loc) · 2.31 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
require("dotenv").config();
const { App } = require("@slack/bolt");
const OpenAI = require("openai");
const fs = require('fs');
const AI_MODEL = "gemma-7b";
const AI_API_KEY = process.env.AI_API_KEY;
const app = new App({
token: process.env.SLACK_BOT_OAUTH_TOKEN,
signingSecret: process.env.SLACK_SIGNING_SECRET,
socketMode: true,
appToken: process.env.APP_TOKEN,
});
const AI_CLIENT = new OpenAI({
apiKey: AI_API_KEY,
baseURL: process.env.BASE_URL,
});
const promptTemplate = fs.readFileSync('./prompt.md', 'utf-8');
const getPostContent = async (PROMPT) => {
const chatCompletion = await AI_CLIENT.chat.completions.create({
messages: [{ role: "user", content: PROMPT }],
model: AI_MODEL,
});
const content = chatCompletion.choices[0]?.message?.content ?? "";
console.log(
`the content from ai is::: \n ${content}`
);
return content;
};
// ------------------------messages to reply to, here !--------------------------------//
app.message(/adios||Adios/, async ({ event, message, say }) => {
try {
const contentToShow = await getPostContent(`${(promptTemplate || process.env.TEMPLATE)+"{"+message.text+"}"}`).catch((e) => {
console.log(`the error in getting content from contentToShow is: ${e}`);
});
const contentToShowActually = contentToShow?.toString().trim();
if(contentToShowActually.length == ""){
await say("Thank you for talking to Adios!")
return;
}
await say({
text:contentToShowActually,
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": contentToShowActually
}
}
]
});
} catch (error) {
console.log("err");
console.error(error);
}
});
// This will match any message that contains 👋
app.message(":wave:", async ({ message, say }) => {
// Handle only newly posted messages here
if (
message.subtype === undefined ||
message.subtype === "bot_message" ||
message.subtype === "file_share" ||
message.subtype === "thread_broadcast"
) {
await say(`Hello, <@${message.user}>`);
}
});
(async () => {
const port = 3008;
await app.start(process.env.PORT || port);
console.log("Bolt app started!! at port: ", process.env.PORT);
})();