From f1382840bae481c8f264d24648feb40a5707d93d Mon Sep 17 00:00:00 2001 From: "vscode iu2frl.it" Date: Tue, 5 Nov 2024 20:19:38 +0100 Subject: [PATCH] Escaping characters before being sent --- frlbot.py | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/frlbot.py b/frlbot.py index 00c16a9..b464ee9 100644 --- a/frlbot.py +++ b/frlbot.py @@ -331,6 +331,29 @@ def remove_old_news(max_days: int = -1) -> int: logging.error("Cannot delete older news. " + str(returned_exception)) return -1 +# Escape characters that cannot be parsed +def escape_markdown_v2(text: str) -> str: + """ + Escape special characters for Telegram MarkdownV2 format. + Characters that need escaping: '_', '*', '[', ']', '(', ')', '~', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!' + + Args: + text (str): The text to escape + + Returns: + str: The escaped text safe for MarkdownV2 + """ + special_chars = ['_', '*', '[', ']', '(', ')', '~', '>', '#', '+', '-', '=', '|', '{', '}', '.', '!', '\''] + escaped_text = '' + + for char in text: + if char in special_chars: + escaped_text += f'\\{char}' + else: + escaped_text += char + + return escaped_text + # Main code def main(): """Main robot code""" @@ -370,12 +393,12 @@ def main(): emoji_calendar = emoji.emojize(":spiral_calendar:", language="alias") emoji_link = emoji.emojize(":link:", language="alias") try: - telegram_payload = f"{emoji_flag_it} {translate_text(single_news.title, 'it')}\n" + \ - f"{emoji_flag_en} {translate_text(single_news.title, 'en')}\n" + \ - f"\n{emoji_pencil} {single_news.author}\n" + \ + telegram_payload = f"{emoji_flag_it} {escape_markdown_v2(translate_text(single_news.title, 'it'))}\n" + \ + f"{emoji_flag_en} {escape_markdown_v2(translate_text(single_news.title, 'en'))}\n" + \ + f"\n{emoji_pencil} {escape_markdown_v2(single_news.author)}\n" + \ f"{emoji_calendar} {single_news.date.strftime('%Y/%m/%d, %H:%M')}\n" + \ - f"\n{emoji_flag_it} {translate_text(single_news.summary, 'it')}\n" + \ - f"\n{emoji_flag_en} {translate_text(single_news.summary, 'en')}\n" + \ + f"\n{emoji_flag_it} {escape_markdown_v2(translate_text(single_news.summary, 'it'))}\n" + \ + f"\n{emoji_flag_en} {escape_markdown_v2(translate_text(single_news.summary, 'en'))}\n" + \ f"\n{emoji_link} {single_news.link}" if not dryRun: telegramBot.send_message(get_target_chat_from_env(), telegram_payload, parse_mode="MARKDOWN") @@ -389,8 +412,13 @@ def main(): news_cnt += 1 except Exception as returned_exception: logging.error(str(returned_exception)) - exception_cnt += 1 exception_message = str(returned_exception) + if "can\'t parse entities:" in returned_exception: + logging.warning("Skipping [" + single_news.checksum + "] due to Telegram parsing error") + sql_connector.cursor().execute("INSERT INTO news(date, checksum) VALUES(?, ?)", [single_news.date, single_news.checksum]) + sql_connector.commit() + else: + exception_cnt += 1 # This message was already posted else: logging.debug("Post at [" + single_news.link + "] was already sent")