aboutsummaryrefslogtreecommitdiff
path: root/src/telegram_handler.py
blob: 07f6f061149972b74a99cfc29d57421652a53d30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import asyncio
import logging

import telegram


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


class Telegram:
    def __init__(self, config: dict):
        self.config: dict = config
        self.bot = telegram.Bot(config['token'])
        asyncio.run(self.init_bot())

    def send_message(self, text):
        asyncio.run(self.async_send_message(text))


    async def async_send_message(self, text):
        async with self.bot:
            await self.bot.send_message(text=text, chat_id=self.config['chat_id'])


    async def init_bot(self):
        async with self.bot:
            logger.debug(f'Telegram bot initialized {await self.bot.get_me()}')