Make Telegram group replies more reliable
This commit is contained in:
@@ -44,6 +44,7 @@ seedDefaults();
|
|||||||
|
|
||||||
const streams = new Map();
|
const streams = new Map();
|
||||||
const operatorPresence = new Map();
|
const operatorPresence = new Map();
|
||||||
|
const telegramReplyContexts = new Map();
|
||||||
|
|
||||||
const server = http.createServer(async (req, res) => {
|
const server = http.createServer(async (req, res) => {
|
||||||
try {
|
try {
|
||||||
@@ -1248,19 +1249,19 @@ async function handleTelegramUpdate(update, settings) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
const message = update?.message;
|
const message = update?.message;
|
||||||
if (!message || !message.text || !message.reply_to_message) return;
|
if (!message || !message.text) return;
|
||||||
const chatId = String(message.chat?.id || "");
|
const chatId = String(message.chat?.id || "");
|
||||||
if (String(settings.chatId) && chatId !== String(settings.chatId)) return;
|
if (String(settings.chatId) && chatId !== String(settings.chatId)) return;
|
||||||
const replyToMessageId = Number(message.reply_to_message.message_id);
|
let link = null;
|
||||||
const link = db.prepare(`
|
if (message.reply_to_message) {
|
||||||
SELECT l.*, c.public_id
|
const replyToMessageId = Number(message.reply_to_message.message_id);
|
||||||
FROM telegram_message_links l
|
link = telegramMessageLink(chatId, replyToMessageId);
|
||||||
JOIN conversations c ON c.id = l.conversation_id
|
}
|
||||||
WHERE l.telegram_chat_id = ? AND l.telegram_message_id = ?
|
if (!link) link = telegramReplyContext(chatId, message.from?.id);
|
||||||
LIMIT 1
|
|
||||||
`).get(chatId, replyToMessageId);
|
|
||||||
if (!link) {
|
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;
|
return;
|
||||||
}
|
}
|
||||||
const conversation = findConversation(link.public_id);
|
const conversation = findConversation(link.public_id);
|
||||||
@@ -1273,6 +1274,7 @@ async function handleTelegramUpdate(update, settings) {
|
|||||||
attachments: [],
|
attachments: [],
|
||||||
senderName: settings.operatorName
|
senderName: settings.operatorName
|
||||||
});
|
});
|
||||||
|
clearTelegramReplyContext(chatId, message.from?.id);
|
||||||
await telegramSendMessage(settings, { chatId, text: "MaalFlows: odpoved odeslana zakaznikovi." });
|
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));
|
const conversation = findConversation(data.slice("reply:".length));
|
||||||
await telegramAnswerCallback(settings, callbackQuery.id, conversation ? "Napis odpoved." : "Konverzace nenalezena.");
|
await telegramAnswerCallback(settings, callbackQuery.id, conversation ? "Napis odpoved." : "Konverzace nenalezena.");
|
||||||
if (!conversation) return;
|
if (!conversation) return;
|
||||||
|
setTelegramReplyContext(chatId, callbackQuery.from?.id, conversation);
|
||||||
const sent = await telegramSendMessage(settings, {
|
const sent = await telegramSendMessage(settings, {
|
||||||
chatId,
|
chatId,
|
||||||
text: "Napis odpoved pro zakaznika:",
|
text: "Napis odpoved pro zakaznika:",
|
||||||
@@ -1298,6 +1301,44 @@ async function handleTelegramCallback(callbackQuery, settings) {
|
|||||||
`).run(conversation.site_id, conversation.id, chatId, promptMessageId, null);
|
`).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) {
|
async function telegramSendMessage(settings, options) {
|
||||||
const token = settings.botToken;
|
const token = settings.botToken;
|
||||||
const chatId = options.chatId || settings.chatId;
|
const chatId = options.chatId || settings.chatId;
|
||||||
|
|||||||
Reference in New Issue
Block a user