# 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
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)
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():
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):