Skip to content

Commit

Permalink
bug: fixed issues when user may get the wrong answer to the question …
Browse files Browse the repository at this point in the history
…from the GPT (#63)
  • Loading branch information
mikita-kandratsyeu authored Oct 4, 2023
1 parent c61004a commit 07a406f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
17 changes: 17 additions & 0 deletions src/callbacks/user-conversations/user-conversations.callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,20 @@ export const deleteUserConversationMessagesCallback: DynamicUsersMenuCallbackTyp
);
}
};

export const updateUserConversationMessagesCallback: DynamicUsersMenuCallbackType = async (
ctx,
username,
) => {
try {
await mongo.updateUserConversation(username, ctx.session.user.messages);

ctx.session.user.messages = [];
} catch (error) {
logger.error(
`callbacks::sessions::updateUserConversationMessagesCallback::${JSON.stringify(
error.message,
)}`,
);
}
};
6 changes: 2 additions & 4 deletions src/commands/clear/clear.command.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { updateUserConversationMessagesCallback } from '@bot/callbacks';
import { BotCommands } from '@bot/constants';
import { inlineGoToChat } from '@bot/keyboards';
import { mongo } from '@bot/services';
import { BotType } from '@bot/types';

export const clearCommand = (bot: BotType) =>
bot.command(BotCommands.CLEAR, async (ctx) => {
const currentUsername = String(ctx.from?.username);

await mongo.updateUserConversation(currentUsername, ctx.session.user.messages);

ctx.session.user.messages = [];
await updateUserConversationMessagesCallback(ctx, currentUsername);

await ctx.reply(ctx.t('info-message-clear-current-session'), {
reply_markup: inlineGoToChat(ctx),
Expand Down
12 changes: 12 additions & 0 deletions src/commands/text/text.command.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { updateUserConversationMessagesCallback } from '@bot/callbacks';
import { TEN_MIN_MS } from '@bot/constants';
import { getGPTAnswer } from '@bot/helpers';
import { inlineShareWithContacts } from '@bot/keyboards';
import { logger } from '@bot/services';
Expand All @@ -8,6 +10,16 @@ export const textCommand = (bot: BotType) => {
try {
const messageId = Number(ctx?.message?.message_id);
const text = String(ctx?.message?.text);
const username = String(ctx?.from?.username);

const currentTimestamp = Date.now();
const lastMessageTimestamp = Math.min(
...ctx.session.user.messages.map(({ timestamp }) => new Date(timestamp).getTime()),
);

if (Math.abs(currentTimestamp - lastMessageTimestamp) >= TEN_MIN_MS) {
updateUserConversationMessagesCallback(ctx, username);
}

const gptAnswer = (await getGPTAnswer(ctx, text)) ?? '';

Expand Down
1 change: 1 addition & 0 deletions src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export enum GPTLimits {
}

export const DAY_MS = 60 * 60 * 24 * 1000;
export const TEN_MIN_MS = 60 * 10 * 1000;

// Node cache
export const TTL_DEFAULT = process.env.NODE_ENV !== 'production' ? 60 : 600;
Expand Down

0 comments on commit 07a406f

Please sign in to comment.