Mirror operator replies to Telegram
This commit is contained in:
@@ -939,6 +939,7 @@ async function createOperatorMessage(conversation, body) {
|
|||||||
markConversationSeen(conversation.id);
|
markConversationSeen(conversation.id);
|
||||||
const updated = findConversation(conversation.public_id);
|
const updated = findConversation(conversation.public_id);
|
||||||
publishConversation(updated, { type: "message:new", payload: conversationDetails(updated) });
|
publishConversation(updated, { type: "message:new", payload: conversationDetails(updated) });
|
||||||
|
notifyTelegramOperatorMessage(updated, messageId).catch((error) => console.error("telegram_operator_notify_failed", error.message));
|
||||||
return updated;
|
return updated;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1243,6 +1244,19 @@ async function notifyTelegramVisitorMessage(conversation, messageId) {
|
|||||||
`).run(conversation.site_id, conversation.id, telegramChatId, telegramMessageId, message.id);
|
`).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) {
|
async function handleTelegramUpdate(update, settings) {
|
||||||
if (update?.callback_query) {
|
if (update?.callback_query) {
|
||||||
await handleTelegramCallback(update.callback_query, settings);
|
await handleTelegramCallback(update.callback_query, settings);
|
||||||
@@ -1275,7 +1289,6 @@ async function handleTelegramUpdate(update, settings) {
|
|||||||
senderName: settings.operatorName
|
senderName: settings.operatorName
|
||||||
});
|
});
|
||||||
clearTelegramReplyContext(chatId, message.from?.id);
|
clearTelegramReplyContext(chatId, message.from?.id);
|
||||||
await telegramSendMessage(settings, { chatId, text: "MaalFlows: odpoved odeslana zakaznikovi." });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleTelegramCallback(callbackQuery, settings) {
|
async function handleTelegramCallback(callbackQuery, settings) {
|
||||||
@@ -1349,6 +1362,7 @@ async function telegramSendMessage(settings, options) {
|
|||||||
chat_id: chatId,
|
chat_id: chatId,
|
||||||
text: options.text,
|
text: options.text,
|
||||||
disable_web_page_preview: true,
|
disable_web_page_preview: true,
|
||||||
|
disable_notification: Boolean(options.disableNotification),
|
||||||
reply_markup: options.replyMarkup
|
reply_markup: options.replyMarkup
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
@@ -1406,6 +1420,18 @@ function telegramVisitorMessageText(conversation, message, attachmentCount) {
|
|||||||
return lines.filter((line) => line !== "").join("\n");
|
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) {
|
function shortText(value, maxLength) {
|
||||||
const text = String(value || "").replace(/\s+/g, " ").trim();
|
const text = String(value || "").replace(/\s+/g, " ").trim();
|
||||||
return text.length > maxLength ? `${text.slice(0, maxLength - 3)}...` : text;
|
return text.length > maxLength ? `${text.slice(0, maxLength - 3)}...` : text;
|
||||||
|
|||||||
Reference in New Issue
Block a user