summaryrefslogtreecommitdiff
path: root/src/telegram_notifier.py
blob: 870bf6cf85572202c2af93238dd3876e8f6210bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import asyncio  # Telegram library is async
import logging

import telegram

logger = logging.getLogger(__name__)  # Set the logger name, to the name of the module


class TelegramNotifier:
    def __init__(self, token: str, chat_id: int):
        self.chat_id: int = chat_id
        self.bot = telegram.Bot(token)

    async def async_send_message(self, text):
        async with self.bot:
            await self.bot.send_message(text=text, chat_id=self.chat_id,
                                        parse_mode=telegram.constants.ParseMode.MARKDOWN)

    # Send message from non async function
    def send_message(self, text):
        logger.debug(f"Sending message: {text}")
        asyncio.run(self.async_send_message(text))