diff --git a/telegram_handler.py b/telegram_handler.py index ffd0a12..a64bbd7 100644 --- a/telegram_handler.py +++ b/telegram_handler.py @@ -3,6 +3,8 @@ import asyncio import threading import yaml import telegram +import re + ITEM_TEXT = "*Artículo*: {}\n" \ "*Descripción*: {}\n" \ @@ -21,11 +23,16 @@ class TelegramHandler: config_file = 'config.yaml' with open(config_file, 'r') as file: config = yaml.safe_load(file) - print(config) token = config['telegram_token'] telegram_channel = config['telegram_channel'] return token, telegram_channel + + def escape_markdown(self, text): + special_chars = r'_[\]()~`>#\+\-=|{}.!]' + escaped_text = re.sub(f'([{re.escape(special_chars)}])', r'\\\1', text) + return escaped_text + def send_telegram_article(self, article): self._loop.run_until_complete(self.send_telegram_article_async(article)) @@ -33,4 +40,5 @@ class TelegramHandler: message = ITEM_TEXT.format(article.get_title(), article.get_description(), article.get_price(), article.get_currency(), article.get_url()) - await self._bot.send_message(self._channel, text=message, parse_mode="MARKDOWN") \ No newline at end of file + escaped_message = self.escape_markdown(message) + await self._bot.send_message(self._channel, text=escaped_message, parse_mode="MarkdownV2") \ No newline at end of file