From 4a1b27ed8057e094bd619254c599919af3214c95 Mon Sep 17 00:00:00 2001 From: Timur Moziev Date: Sat, 3 Aug 2024 21:29:14 +0000 Subject: [PATCH] add old messages reject --- anca.json | 4 +-- src/cinnabar.ts | 4 +-- src/telegram.ts | 91 ++++++++++++++++++++++++------------------------- 3 files changed, 49 insertions(+), 50 deletions(-) diff --git a/anca.json b/anca.json index f3acd6c..4daf2e3 100644 --- a/anca.json +++ b/anca.json @@ -12,8 +12,8 @@ "dataVersion": 0, "version": { "latest": "0.1.0", - "latestNext": "0.1.0+next.20240803_204513", - "timestamp": 1722717913 + "latestNext": "0.1.0+next.20240803_212634", + "timestamp": 1722720394 }, "files": [ { diff --git a/src/cinnabar.ts b/src/cinnabar.ts index 168c92f..7c6d199 100644 --- a/src/cinnabar.ts +++ b/src/cinnabar.ts @@ -1,4 +1,4 @@ // This file was generated by Cinnabar Meta. Do not edit. -export const CINNABAR_PROJECT_TIMESTAMP = 1722717913; -export const CINNABAR_PROJECT_VERSION = "0.1.0+next.20240803_204513"; +export const CINNABAR_PROJECT_TIMESTAMP = 1722720394; +export const CINNABAR_PROJECT_VERSION = "0.1.0+next.20240803_212634"; diff --git a/src/telegram.ts b/src/telegram.ts index 580b9fd..50f4c9a 100644 --- a/src/telegram.ts +++ b/src/telegram.ts @@ -1,3 +1,4 @@ +/* eslint-disable sonarjs/no-duplicate-string */ import { sendTelegramMessage } from "cinnagram"; import https from "https"; @@ -12,6 +13,7 @@ const TELEGRAM_API_URL = `https://api.telegram.org/bot${process.env.TELEGRAM_BOT const SPECIFIC_CHAT_ID = parseInt(process.env.TELEGRAM_CHAT_ID || "") || 0; const TELEGRAM_API_POLLING_TIME = parseInt(process.env.TELEGRAM_API_POLLING_TIME || "30") || 30; +const APP_START_TIME = Date.now() / 1000; let lastUpdateId = 0; @@ -153,54 +155,51 @@ function getTimeLeft(future: number) { * @param config */ function handleUpdate(update: any, config: Config) { - if (update.message && update.message.text) { - const messageText = processMessageText(update.message.text); - const chatId = update.message.chat.id; - const senderInfo = getSenderInfo(update); - - if (!SPECIFIC_CHAT_ID || chatId === SPECIFIC_CHAT_ID) { - console.log(`Accept ${senderInfo}: ${messageText}`); - const acceptableTriggers: { group: TriggerGroup; trigger: string }[] = []; - - for (const group of config.groups) { - for (const word of group.triggers) { - // eslint-disable-next-line security/detect-non-literal-regexp - const regex = new RegExp(`(^|\\P{L})${word}($|\\P{L})`, "iu"); - if (regex.test(messageText)) { - if ( - group.futureTrigger && - Date.now() < group.futureTrigger[chatId] - ) { - console.log( - `Skipping group ${group.name} due to time threshold (~${getTimeLeft(group.futureTrigger[chatId])} minutes left)`, - ); - break; - } - console.log( - `Found acceptable trigger: ${word} in group ${group.name}`, - ); - acceptableTriggers.push({ group, trigger: word }); - } - } - } + if (!update.message || !update.message.text) { + return; + } - if (acceptableTriggers.length > 0) { - const selectedTrigger = - acceptableTriggers[ - Math.floor(Math.random() * acceptableTriggers.length) - ]; - console.log( - `Selected trigger ${selectedTrigger.trigger} from group ${selectedTrigger.group.name}`, - ); - sendReply( - config, - selectedTrigger.group, - chatId, - update.message.message_id, - ); + const senderInfo = getSenderInfo(update); + + if (update.message.date <= APP_START_TIME) { + console.log("Reject outdated message from", senderInfo); + return; + } + + const messageText = processMessageText(update.message.text); + const chatId = update.message.chat.id; + + if (SPECIFIC_CHAT_ID && chatId !== SPECIFIC_CHAT_ID) { + console.log("Reject", senderInfo); + return; + } + + console.log(`Accept ${senderInfo}: ${messageText}`); + const acceptableTriggers: { group: TriggerGroup; trigger: string }[] = []; + + for (const group of config.groups) { + for (const word of group.triggers) { + // eslint-disable-next-line security/detect-non-literal-regexp + const regex = new RegExp(`(^|\\P{L})${word}($|\\P{L})`, "iu"); + if (regex.test(messageText)) { + if (group.futureTrigger && Date.now() < group.futureTrigger[chatId]) { + console.log( + `Skipping group ${group.name} due to time threshold (~${getTimeLeft(group.futureTrigger[chatId])} minutes left)`, + ); + break; + } + console.log(`Found acceptable trigger: ${word} in group ${group.name}`); + acceptableTriggers.push({ group, trigger: word }); } - } else { - console.log("Reject", senderInfo); } } + + if (acceptableTriggers.length > 0) { + const selectedTrigger = + acceptableTriggers[Math.floor(Math.random() * acceptableTriggers.length)]; + console.log( + `Selected trigger ${selectedTrigger.trigger} from group ${selectedTrigger.group.name}`, + ); + sendReply(config, selectedTrigger.group, chatId, update.message.message_id); + } }