]> mj.ucw.cz Git - home-hw.git/commitdiff
burrow-telegram: Compatibility with newer libs
authorMartin Mareš <mj@ucw.cz>
Sat, 16 Aug 2025 16:37:16 +0000 (18:37 +0200)
committerMartin Mareš <mj@ucw.cz>
Sat, 16 Aug 2025 16:37:16 +0000 (18:37 +0200)
telegram/burrow-telegram.py

index 22fa145eda4f97ef4cacb5cb859b5c4891f18d69..97a371576cb2d66afbd0b556e507287028643759 100755 (executable)
@@ -2,14 +2,14 @@
 # A simple daemon for sending Telegram notifications about failures in the Burrow
 # (c) 2022 Martin Mareš <mj@ucw.cz>
 
-from aiogram import Bot, Dispatcher, executor, types
+from aiogram import Bot, Dispatcher, types
 import asyncio
 import aiomqtt
 from configparser import ConfigParser
 from datetime import datetime, timedelta
 import logging
 from logging.handlers import SysLogHandler
-import signal
+import magic_filter
 import ssl
 import sys
 
@@ -32,19 +32,15 @@ logger.setLevel(logging.INFO)
 logger.addHandler(log_handler)
 
 bot = Bot(token=API_TOKEN)
-dispatcher = Dispatcher(bot)
+dispatcher = Dispatcher()
 
 
-@dispatcher.message_handler(commands=['start', 'help'])
-async def send_welcome(message: types.Message):
-    logger.info(f'Start from {message.chat}')
-    await message.reply("Brum!\nI'm BurrowBot!\n")
-
-
-@dispatcher.message_handler()
-async def echo(message: types.Message):
+@dispatcher.message()
+async def message_handler(message: types.Message):
     print(message)
-    if message.text.startswith("xyzzy"):
+    if message.text in ('start', 'help'):
+        await message.reply("Brum!\nI'm BurrowBot!\n")
+    elif message.text.startswith("xyzzy"):
         await send_msg("Nothing happens.")
     # await message.answer(message.text)
 
@@ -137,14 +133,11 @@ async def mqtt_loop():
     sctx.load_cert_chain('/etc/burrow-mqtt/client.crt', '/etc/burrow-mqtt/client.key')
     sctx.load_verify_locations(cafile='/etc/burrow-mqtt/ca.crt')
 
-    mqtt = aiomqtt.Client(client_id='telegram', hostname="burrow-mqtt", port=8883, tls_context=sctx)
-    await mqtt.connect()
-
-    async with mqtt.messages() as messages:
+    async with aiomqtt.Client(hostname="burrow-mqtt", port=8883, tls_context=sctx) as mqtt:
         await mqtt.subscribe("burrow/heating/#")
         await mqtt.subscribe("burrow/temp/#")
-        async for msg in messages:
-            await mqtt_process_msg(msg.topic.value, msg.payload.decode())
+        async for msg in mqtt.messages:
+                await mqtt_process_msg(msg.topic.value, msg.payload.decode())
 
 
 async def mqtt_watcher():
@@ -173,7 +166,7 @@ async def main():
     loop = asyncio.get_event_loop()
     coros = [
         loop.create_task(mqtt_watcher()),
-        loop.create_task(dispatcher.start_polling(timeout=60, relax=0.01, fast=True, allowed_updates=None)),
+        loop.create_task(dispatcher.start_polling(bot, polling_timeout=60, allowed_updates=None)),
         loop.create_task(fortunes()),
     ]
     for coro in asyncio.as_completed(coros):