-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
118 lines (102 loc) · 3.05 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
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
113
114
115
116
117
118
const { App } = require('@slack/bolt');
const dotenv = require('dotenv');
const axios = require('axios');
dotenv.configDotenv();
const rootPrefix = '.',
coreConstants = require(rootPrefix + '/config/coreConstants'),
OpenModal = require(rootPrefix + '/lib/slack/openModal'),
SpeechToTextWhisper = require(rootPrefix + '/lib/openAi/speechToText'),
OpenAiCompletionLib = require(rootPrefix + '/lib/openAi/completion');
// Initialize your Slack app with the bot token
const slackApp = new App({
token: coreConstants.SLACK_BOT_USER_OAUTH_TOKEN,
signingSecret: coreConstants.SLACK_SIGNING_SECRET,
});
// Event listener for file shared events
slackApp.event('file_shared', async ({ event, say, client }) => {
console.log('Received file event');
const speechToTextLib = new SpeechToTextWhisper();
await speechToTextLib.perform(event, say, client);
});
// Url verification required by slack
slackApp.event('url_verification', ({ event, context, client, next }) => {
context.res = {
status: 200,
body: event.challenge,
};
return;
});
slackApp.command('/open-modal', async ({ ack, body, context, client }) => {
console.log('Called open modal');
try {
await ack();
const openModalObj = new OpenModal();
openModalObj.perform({
triggerId: body.trigger_id,
client: client,
responseUrl: body.response_url,
});
return;
} catch (error) {
throw new Error('Error opening in slack modal: ', error);
}
});
// View submission event listener
slackApp.view('suggestion-modal', async ({ ack, body, view }) => {
try {
const messageText =
view.state.values.message_input.message_input_action.value;
const promptType =
view.state.values.message_type.message_type_action.selected_option.value;
const responseUrl = JSON.parse(view.private_metadata).responseUrl;
getOpenAiSuggestionAndSendToChannel(messageText, promptType, responseUrl);
console.log('Calling ack');
await ack();
console.log('Calling ack done');
return;
} catch (error) {
throw new Error('Error after modal submit: ', error);
}
});
async function getOpenAiSuggestionAndSendToChannel(
messageText,
promptType,
responseUrl,
) {
console.log('Calling openai lib');
messageText = messageText.trim();
const openAiCompletionLib = new OpenAiCompletionLib();
const suggestionResponse = await openAiCompletionLib.getSuggestions(
promptType,
messageText,
);
console.log('suggestionResponse: ', suggestionResponse);
const payload = {
response_type: 'ephemeral',
blocks: [
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*Input Message*: ${messageText}`,
},
},
{
type: 'divider',
},
{
type: 'section',
text: {
type: 'mrkdwn',
text: `*Formatted Response*: ${suggestionResponse.message}`,
},
},
],
};
await axios.post(responseUrl, payload);
}
// Start your slack-app
(async () => {
await slackApp.start(process.env.PORT || 3000);
console.log('Bot is running!');
})();