Telegram handler fixes

This commit is contained in:
Kifixo
2024-02-13 12:52:10 +01:00
parent 619be0d82b
commit 6032b33874

View File

@@ -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")
escaped_message = self.escape_markdown(message)
await self._bot.send_message(self._channel, text=escaped_message, parse_mode="MarkdownV2")