Files
telegram-shop/src/utils/messageUtils.js
NW 6ce8da257a fix: clean chat navigation — edit messages instead of sending new ones
All callback handlers now use editOrSendCallback() to edit the existing
message in-place instead of bot.sendMessage() which creates new messages
and clutters the chat. If edit fails (message too old), the old message
is deleted and a new one sent.

Added src/utils/messageUtils.js with:
- editOrSendCallback(callbackQuery, text, options) — edit or fallback
- editOrSend(chatId, messageId, text, options) — edit or fallback
- deleteAndSend(chatId, messageId, text, options) — delete then send

Fixed handlers:
- userProductHandler: handleBuyProduct errors, handlePay validation/stock errors
- userPurchaseHandler: viewPurchase errors, handleConfirmReceived errors, handlePurchaseListPage errors
- userLocationHandler: all error paths now edit in-place
- userDeletionHandler: both error paths now edit in-place
- wallet/balanceHandler: showBalance error (text command, acceptable)
- wallet/refreshHandler: user not found and refresh errors
- wallet/topUpHandler: wallet loading error
- wallet/createHandler: invalid wallet type error
- wallet/historyHandler: both transaction history error paths
- wallet/archiveHandler: archived wallets error
2026-06-24 20:45:39 +01:00

31 lines
1.0 KiB
JavaScript

import bot from '../context/bot.js';
export async function editOrSend(chatId, messageId, text, options = {}) {
if (messageId) {
try {
const result = await bot.editMessageText(text, {
chat_id: chatId,
message_id: messageId,
...options,
});
return result;
} catch (e) {
// message too old or already edited — delete and send new
try { await bot.deleteMessage(chatId, messageId); } catch (_) {}
}
}
return bot.sendMessage(chatId, text, options);
}
export async function editOrSendCallback(callbackQuery, text, options = {}) {
const chatId = callbackQuery.message.chat.id;
const messageId = callbackQuery.message.message_id;
return editOrSend(chatId, messageId, text, options);
}
export async function deleteAndSend(chatId, messageId, text, options = {}) {
if (messageId) {
try { await bot.deleteMessage(chatId, messageId); } catch (_) {}
}
return bot.sendMessage(chatId, text, options);
}