from app import config_parser
import telegram
from importlib.metadata import version
import asyncio

config = config_parser.ConfigParser().loadConfig()

class telegramBot:
    bot = None

    async def sendMessage(self, message):
        ver = version("python-telegram-bot")
        major = int(ver.split(".")[0])


        token = config["telegram"]["token"]
        chat_id = config["telegram"]["chat_id"]

        # --------------------
        # PTB v20+ (async)
        # --------------------
        if major >= 20:
            self.bot = telegram.Bot(token)
            await self.bot.send_message(chat_id=chat_id, text=str(message))
            return

        # --------------------
        # PTB v12/v13 (sync)
        # --------------------
        else:
            self.bot = telegram.Bot(token)

            # ejecuta el método sync en un hilo para no romper asyncio.run()
            loop = asyncio.get_running_loop()
            await loop.run_in_executor(
                None,
                lambda: self.bot.send_message(chat_id=chat_id, text=str(message))
            )
