Make Telegram group replies more reliable

This commit is contained in:
rajchales-monito
2026-07-30 12:43:48 +02:00
parent deb090a882
commit 5ef09f25d9
+51 -10
View File
@@ -44,6 +44,7 @@ seedDefaults();
const streams = new Map();
const operatorPresence = new Map();
const telegramReplyContexts = new Map();
const server = http.createServer(async (req, res) => {
try {
@@ -1248,19 +1249,19 @@ async function handleTelegramUpdate(update, settings) {
return;
}
const message = update?.message;
if (!message || !message.text || !message.reply_to_message) return;
if (!message || !message.text) return;
const chatId = String(message.chat?.id || "");
if (String(settings.chatId) && chatId !== String(settings.chatId)) return;
const replyToMessageId = Number(message.reply_to_message.message_id);
const link = db.prepare(`
SELECT l.*, c.public_id
FROM telegram_message_links l
JOIN conversations c ON c.id = l.conversation_id
WHERE l.telegram_chat_id = ? AND l.telegram_message_id = ?
LIMIT 1
`).get(chatId, replyToMessageId);
let link = null;
if (message.reply_to_message) {
const replyToMessageId = Number(message.reply_to_message.message_id);
link = telegramMessageLink(chatId, replyToMessageId);
}
if (!link) link = telegramReplyContext(chatId, message.from?.id);
if (!link) {
await telegramSendMessage(settings, { chatId, text: "MaalFlows: odpoved nepatri k zadne aktivni notifikaci." });
if (message.reply_to_message) {
await telegramSendMessage(settings, { chatId, text: "MaalFlows: odpoved nepatri k zadne aktivni notifikaci." });
}
return;
}
const conversation = findConversation(link.public_id);
@@ -1273,6 +1274,7 @@ async function handleTelegramUpdate(update, settings) {
attachments: [],
senderName: settings.operatorName
});
clearTelegramReplyContext(chatId, message.from?.id);
await telegramSendMessage(settings, { chatId, text: "MaalFlows: odpoved odeslana zakaznikovi." });
}
@@ -1284,6 +1286,7 @@ async function handleTelegramCallback(callbackQuery, settings) {
const conversation = findConversation(data.slice("reply:".length));
await telegramAnswerCallback(settings, callbackQuery.id, conversation ? "Napis odpoved." : "Konverzace nenalezena.");
if (!conversation) return;
setTelegramReplyContext(chatId, callbackQuery.from?.id, conversation);
const sent = await telegramSendMessage(settings, {
chatId,
text: "Napis odpoved pro zakaznika:",
@@ -1298,6 +1301,44 @@ async function handleTelegramCallback(callbackQuery, settings) {
`).run(conversation.site_id, conversation.id, chatId, promptMessageId, null);
}
function telegramMessageLink(chatId, messageId) {
return db.prepare(`
SELECT l.*, c.public_id
FROM telegram_message_links l
JOIN conversations c ON c.id = l.conversation_id
WHERE l.telegram_chat_id = ? AND l.telegram_message_id = ?
LIMIT 1
`).get(chatId, messageId);
}
function setTelegramReplyContext(chatId, telegramUserId, conversation) {
if (!telegramUserId) return;
cleanupTelegramReplyContexts();
telegramReplyContexts.set(`${chatId}:${telegramUserId}`, {
public_id: conversation.public_id,
expiresAt: Date.now() + 5 * 60_000
});
}
function telegramReplyContext(chatId, telegramUserId) {
if (!telegramUserId) return null;
cleanupTelegramReplyContexts();
const context = telegramReplyContexts.get(`${chatId}:${telegramUserId}`);
return context ? { public_id: context.public_id } : null;
}
function clearTelegramReplyContext(chatId, telegramUserId) {
if (!telegramUserId) return;
telegramReplyContexts.delete(`${chatId}:${telegramUserId}`);
}
function cleanupTelegramReplyContexts() {
const now = Date.now();
for (const [key, context] of telegramReplyContexts.entries()) {
if (now > context.expiresAt) telegramReplyContexts.delete(key);
}
}
async function telegramSendMessage(settings, options) {
const token = settings.botToken;
const chatId = options.chatId || settings.chatId;