Skip to content

Commit

Permalink
Escaping characters before being sent
Browse files Browse the repository at this point in the history
  • Loading branch information
vscode iu2frl.it committed Nov 5, 2024
1 parent ffcbab5 commit f138284
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions frlbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down

0 comments on commit f138284

Please sign in to comment.