Skip to content

Commit

Permalink
add old messages reject
Browse files Browse the repository at this point in the history
  • Loading branch information
TimurRin committed Aug 3, 2024
1 parent 6d278db commit 4a1b27e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 50 deletions.
4 changes: 2 additions & 2 deletions anca.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
4 changes: 2 additions & 2 deletions src/cinnabar.ts
Original file line number Diff line number Diff line change
@@ -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";
91 changes: 45 additions & 46 deletions src/telegram.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable sonarjs/no-duplicate-string */
import { sendTelegramMessage } from "cinnagram";
import https from "https";

Expand All @@ -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;

Expand Down Expand Up @@ -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);
}
}

0 comments on commit 4a1b27e

Please sign in to comment.