diff --git a/bot.py b/bot.py index 5138967..08e5268 100644 --- a/bot.py +++ b/bot.py @@ -14,6 +14,8 @@ from config import Config config = Config() config.WEBHOOK_URL = os.getenv("WEBHOOK_URL", config.WEBHOOK_URL) +config.TG_CHAT_ID = os.getenv("TG_CHAT_ID", config.TG_CHAT_ID) +config.TG_API_KEY = os.getenv("TG_API_KEY", config.TG_API_KEY) STATE_PATH = os.getenv("STATE_PATH", "state.pickle") @@ -64,14 +66,25 @@ def process_event(event, template): async def send_message(text): - async with aiohttp.ClientSession() as session: - result = await session.post(config.WEBHOOK_URL, json={ - "text": text, - "format": "html", - "displayName": "PUBLIC INFORMATION STREAMS & SERVICES", - "avatarUrl": config.AVATAR_URL - }) - logging.debug(f"RECV: [{result.status}] {await result.text()}") + if config.WEBHOOK_URL: + logging.debug(f"Posting via WEBHOOK.") + async with aiohttp.ClientSession() as session: + result = await session.post(config.WEBHOOK_URL, json={ + "text": text, + "format": "html", + "displayName": "PUBLIC INFORMATION STREAMS & SERVICES", + "avatarUrl": config.AVATAR_URL + }) + logging.debug(f"RECV: [{result.status}] {await result.text()}") + if config.TG_CHAT_ID and config.TG_API_KEY: + logging.debug(f"Posting via TELEGRAM.") + async with aiohttp.ClientSession() as session: + result = await session.post(f"https://api.telegram.org/bot{config.TG_API_KEY}/sendMessage", json={ + "chat_id": config.TG_CHAT_ID, + "text": text.replace('
', '\n'), + "parse_mode": "html" + }) + logging.debug(f"RECV: [{result.status}] {await result.text()}") def get_event_id(event: Event) -> str: @@ -117,8 +130,8 @@ async def main(): if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - [%(levelname)s] %(message)s') - if config.WEBHOOK_URL is None: - raise RuntimeError("WEBHOOK_URL is not defined.") + if config.WEBHOOK_URL is None and (config.TG_API_KEY is None or config.TG_CHAT_ID is None): + raise RuntimeError("One of WEBHOOK_URL, TG_API_KEY & TG_CHAT_ID must be defined.") loop = asyncio.get_event_loop() loop.run_until_complete(main()) diff --git a/config.py b/config.py index 119ae10..39a552f 100644 --- a/config.py +++ b/config.py @@ -1,5 +1,7 @@ class Config: WEBHOOK_URL = None + TG_API_KEY = None + TG_CHAT_ID = None AVATAR_URL = 'https://gitlab.com/sdbs_cz/piss-bot/-/raw/master/piss.jpg' URLS = [ 'https://sdbs.cz/wp-content/themes/sdbs/file-download.php?type=in',