From 15404d91a3c2d324808c640a1e605984a2fb7730 Mon Sep 17 00:00:00 2001 From: Alter-xyz <88554920+alterxyz@users.noreply.github.com> Date: Sat, 29 Jun 2024 08:58:49 -0400 Subject: [PATCH] feat: skip update duplicate message --- handlers/__init__.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/handlers/__init__.py b/handlers/__init__.py index db934e1..0f5d961 100644 --- a/handlers/__init__.py +++ b/handlers/__init__.py @@ -15,6 +15,7 @@ from telebot.util import smart_split import telegramify_markdown from telegramify_markdown.customize import markdown_symbol from urlextract import URLExtract +from expiringdict import ExpiringDict markdown_symbol.head_level_1 = "📌" # If you want, Customizing the head level 1 symbol markdown_symbol.link = "🔗" # If you want, Customizing the link symbol @@ -23,6 +24,8 @@ T = TypeVar("T", bound=Callable) BOT_MESSAGE_LENGTH = 4000 +REPLY_MESSAGE_CACHE = ExpiringDict(max_len=1000, max_age_seconds=300) + def bot_reply_first(message: Message, who: str, bot: TeleBot) -> Message: """Create the first reply message which make user feel the bot is working.""" @@ -44,6 +47,11 @@ def bot_reply_markdown( it will fallback to plain text in case of any failure """ try: + cache_key = f"{reply_id.chat.id}_{reply_id.message_id}" + if cache_key in REPLY_MESSAGE_CACHE and REPLY_MESSAGE_CACHE[cache_key] == text: + print(f"Skipping duplicate message for {cache_key}") + return True + REPLY_MESSAGE_CACHE[cache_key] = text if len(text.encode("utf-8")) <= BOT_MESSAGE_LENGTH or not split_text: bot.edit_message_text( f"*{who}*:\n{telegramify_markdown.convert(text)}",