Skip to content

Commit f9007f8

Browse files
authored
Merge pull request #11 from BLazzeD21/refactor-sending-messages
decomposition of sending messages
2 parents 5bbca1f + 1804ecb commit f9007f8

File tree

4 files changed

+35
-44
lines changed

4 files changed

+35
-44
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node.js-chatgpt-bot",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "\"\"",
55
"main": "index.js",
66
"type": "module",

src/handlers/openaiHandlers.js

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,23 @@ import { checkAccess } from '../utils/checkAccess.js';
1111
import ErrorHandler from './errorHandler.js';
1212

1313
class OpenAIHandlers {
14+
sendResponse = (ctx, sessions, sessionId) => {
15+
return async (response) => {
16+
if (response && response.content) {
17+
sessions[sessionId].messages.push({
18+
role: openai.roles.ASSISTANT,
19+
content: response.content,
20+
});
21+
}
22+
23+
await ctx.reply(
24+
response.content, { parse_mode: 'Markdown' }, menuKeyboard,
25+
).catch(
26+
async () => await ctx.reply(response.content, menuKeyboard),
27+
);
28+
};
29+
};
30+
1431
textHandler = (config, sessions) => {
1532
return async (ctx) => {
1633
if (await checkAccess(config, ctx)) return;
@@ -21,36 +38,19 @@ class OpenAIHandlers {
2138
const processing = await ctx.reply(
2239
code(LEXICON_EN['processingText']),
2340
menuKeyboard);
41+
2442
ctx.sendChatAction('typing');
2543

26-
const text = ctx.message.text;
44+
const content = ctx.message.text;
2745

2846
sessions[sessionId].messages.push({
2947
role: openai.roles.USER,
30-
content: text,
48+
content: content,
3149
});
3250

3351
openai
3452
.chat(sessions[sessionId].messages)
35-
.then(async (response) => {
36-
if (response && response.content) {
37-
sessions[sessionId].messages.push({
38-
role: openai.roles.ASSISTANT,
39-
content: response.content,
40-
});
41-
}
42-
43-
await ctx
44-
.reply(
45-
response.content,
46-
{ parse_mode: 'Markdown' },
47-
menuKeyboard)
48-
.catch(async () => {
49-
await ctx.reply(response.content,
50-
{ parse_mode: 'HTML' },
51-
);
52-
});
53-
},
53+
.then(this.sendResponse(ctx, sessions, sessionId),
5454
).catch(ErrorHandler.responseError(ctx, 'textHandler'),
5555
).finally(async () => {
5656
await ctx.deleteMessage(processing.message_id);
@@ -68,6 +68,7 @@ class OpenAIHandlers {
6868
const processing = await ctx.reply(
6969
code(LEXICON_EN['processingVoice']),
7070
menuKeyboard);
71+
7172
ctx.sendChatAction('typing');
7273

7374
const link = await ctx.telegram.getFileLink(ctx.message.voice.file_id);
@@ -86,23 +87,7 @@ class OpenAIHandlers {
8687

8788
openai
8889
.chat(sessions[sessionId].messages)
89-
.then(async (response) => {
90-
sessions[sessionId].messages.push({
91-
role: openai.roles.ASSISTANT,
92-
content: response.content,
93-
});
94-
95-
await ctx
96-
.reply(
97-
response.content,
98-
{ parse_mode: 'Markdown' },
99-
menuKeyboard)
100-
.catch(async () => {
101-
await ctx.reply(response.content,
102-
{ parse_mode: 'HTML' },
103-
);
104-
});
105-
})
90+
.then(this.sendResponse(ctx, sessions, sessionId))
10691
.catch(ErrorHandler.responseError(ctx, 'transcription'));
10792
})
10893
.catch(ErrorHandler.responseError(ctx, 'voiceHandler'))

src/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,17 @@ bot.catch(async (error, ctx) => {
7272
}
7373
});
7474

75-
setMenu(bot);
76-
deleteWebHook(bot);
75+
if (process.env.NODE_ENV === 'production') {
76+
setMenu(bot);
77+
deleteWebHook(bot);
78+
}
79+
7780

7881
process.once('SIGINT', () => bot.stop('SIGINT'));
7982
process.once('SIGTERM', () => bot.stop('SIGTERM'));
8083

8184
bot.launch({ dropPendingUpdates: true })
82-
.then(sendMessages(bot, await getUsersArray(config)));
85+
.then(
86+
process.env.NODE_ENV === 'production' ?
87+
sendMessages(bot, await getUsersArray(config)) : 0,
88+
);

0 commit comments

Comments
 (0)