Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/handlers/errorHandler.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { LEXICON_EN } from '../lexicon/lexicon_en.js';
import { menuKeyboard } from '../keyboards/keyboards.js';

class ErrorHandler {
responseError(ctx, handler) {
return (error) => {
console.log(`${error.name} ${handler}: ${error.message}`);
console.log(`${error.name} ${handler}: ${error.message}`,
menuKeyboard);
ctx.reply(
`${LEXICON_EN['noResponce']}\n\n${error.name}: ${error.message}`,
);
Expand Down
26 changes: 19 additions & 7 deletions src/handlers/openaiHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { LEXICON_EN } from '../lexicon/lexicon_en.js';
import { openai } from '../openai.js';
import { converter } from '../converter.js';

import { menuKeyboard } from '../keyboards/keyboards.js';
import { createInitialSession } from '../utils/createSession.js';
import { checkAccess } from '../utils/checkAccess.js';

Expand All @@ -17,7 +18,9 @@ class OpenAIHandlers {
const sessionId = ctx.message.chat.id;
sessions[sessionId] ??= createInitialSession();

const processing = await ctx.reply(code(LEXICON_EN['processingText']));
const processing = await ctx.reply(
code(LEXICON_EN['processingText']),
menuKeyboard);

const text = ctx.message.text;

Expand All @@ -36,10 +39,14 @@ class OpenAIHandlers {
});
}

await ctx.reply(response.content, { parse_mode: 'Markdown' });
})
.catch(ErrorHandler.responseError(ctx, 'textHandler'))
.finally(async () => {
await ctx
.reply(
response.content,
{ parse_mode: 'Markdown' },
menuKeyboard);
},
).catch(ErrorHandler.responseError(ctx, 'textHandler'),
).finally(async () => {
await ctx.deleteMessage(processing.message_id);
});
};
Expand All @@ -52,7 +59,9 @@ class OpenAIHandlers {
const sessionId = ctx.message.chat.id;
sessions[sessionId] ??= createInitialSession();

const processing = await ctx.reply(code(LEXICON_EN['processingVoice']));
const processing = await ctx.reply(
code(LEXICON_EN['processingVoice']),
menuKeyboard);

const link = await ctx.telegram.getFileLink(ctx.message.voice.file_id);
const userId = String(ctx.message.from.id);
Expand Down Expand Up @@ -91,7 +100,9 @@ class OpenAIHandlers {
return async (ctx) => {
if (await checkAccess(config, ctx)) return;

const processing = await ctx.reply(code(LEXICON_EN['processingImage']));
const processing = await ctx.reply(
code(LEXICON_EN['processingImage']),
menuKeyboard);

const requestText = ctx.message.text.replace('/image', '').trim();

Expand All @@ -109,6 +120,7 @@ class OpenAIHandlers {
if (response) {
await ctx.replyWithPhoto(
{ url: response }, { caption: requestText },
menuKeyboard,
);
return;
}
Expand Down
12 changes: 7 additions & 5 deletions src/handlers/userHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import {
LEXICON_EN, getIDs,
getHelp, printPassword,
} from '../lexicon/lexicon_en.js';
import { createMenuKeyboard } from '../keyboards/keyboards.js';
import { menuKeyboard } from '../keyboards/keyboards.js';

import { createInitialSession } from '../utils/createSession.js';
import { checkAccess } from '../utils/checkAccess.js';
import { generatePassword } from '../utils/generatePassword.js';

const menuKeyboard = createMenuKeyboard();

class UserHandlers {
startHandler = (sessions) => {
return async (ctx) => {
Expand Down Expand Up @@ -42,7 +40,10 @@ class UserHandlers {
passwordHandler = () => {
return async (ctx) => {
const password = await generatePassword();
await ctx.reply(await printPassword(password), { parse_mode: 'HTML' });
await ctx.reply(
await printPassword(password),
{ parse_mode: 'HTML' },
menuKeyboard);
};
};

Expand All @@ -52,7 +53,8 @@ class UserHandlers {

const sessionId = ctx.message.chat.id;
sessions[sessionId] = createInitialSession();
await ctx.reply(LEXICON_EN['reset'], menuKeyboard);
await ctx.reply(LEXICON_EN['reset'],
menuKeyboard);
};
};
}
Expand Down
2 changes: 2 additions & 0 deletions src/keyboards/keyboards.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ export function createMenuKeyboard() {
[LEXICON_EN['password_btn'], LEXICON_EN['getIDs_btn']]],
).resize();
}

export const menuKeyboard = createMenuKeyboard();