From 0adcd8d37fedd71c84d5fccc8028240fd72c7b6e Mon Sep 17 00:00:00 2001 From: rajchales-monito Date: Thu, 30 Jul 2026 12:58:03 +0200 Subject: [PATCH] Mirror operator replies to Telegram --- server.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index 7e15634..45844fd 100644 --- a/server.js +++ b/server.js @@ -939,6 +939,7 @@ async function createOperatorMessage(conversation, body) { markConversationSeen(conversation.id); const updated = findConversation(conversation.public_id); publishConversation(updated, { type: "message:new", payload: conversationDetails(updated) }); + notifyTelegramOperatorMessage(updated, messageId).catch((error) => console.error("telegram_operator_notify_failed", error.message)); return updated; } @@ -1243,6 +1244,19 @@ async function notifyTelegramVisitorMessage(conversation, messageId) { `).run(conversation.site_id, conversation.id, telegramChatId, telegramMessageId, message.id); } +async function notifyTelegramOperatorMessage(conversation, messageId) { + const settings = getTelegramSettings(); + if (!settings.enabled || !settings.botToken || !settings.chatId) return; + const message = db.prepare("SELECT * FROM messages WHERE id = ? AND conversation_id = ?").get(messageId, conversation.id); + if (!message || message.sender_type !== "admin") return; + const details = conversationDetails(findConversation(conversation.public_id)); + const attachmentCount = details.messages.find((item) => item.id === message.id)?.attachments?.length || 0; + await telegramSendMessage(settings, { + text: telegramOperatorMessageText(details.conversation, message, attachmentCount), + disableNotification: true + }); +} + async function handleTelegramUpdate(update, settings) { if (update?.callback_query) { await handleTelegramCallback(update.callback_query, settings); @@ -1275,7 +1289,6 @@ async function handleTelegramUpdate(update, settings) { senderName: settings.operatorName }); clearTelegramReplyContext(chatId, message.from?.id); - await telegramSendMessage(settings, { chatId, text: "MaalFlows: odpoved odeslana zakaznikovi." }); } async function handleTelegramCallback(callbackQuery, settings) { @@ -1349,6 +1362,7 @@ async function telegramSendMessage(settings, options) { chat_id: chatId, text: options.text, disable_web_page_preview: true, + disable_notification: Boolean(options.disableNotification), reply_markup: options.replyMarkup }) }); @@ -1406,6 +1420,18 @@ function telegramVisitorMessageText(conversation, message, attachmentCount) { return lines.filter((line) => line !== "").join("\n"); } +function telegramOperatorMessageText(conversation, message, attachmentCount) { + const lines = [ + `MaalFlows: ${message.sender_name || "Operator"} odpovedel`, + `Zakaznik: ${[conversation.countryFlag, conversation.visitorLabel || "Navstevnik"].filter(Boolean).join(" ")}`, + "", + message.body ? `Zprava: ${message.body}` : (attachmentCount ? "Zprava: (poslana fotka)" : ""), + message.translated_body ? `\nPreklad pro zakaznika: ${message.translated_body}` : "", + attachmentCount ? `\nPrilohy: ${attachmentCount} fotka/fotek` : "" + ]; + return lines.filter((line) => line !== "").join("\n"); +} + function shortText(value, maxLength) { const text = String(value || "").replace(/\s+/g, " ").trim(); return text.length > maxLength ? `${text.slice(0, maxLength - 3)}...` : text;