From 566d4e461e849946b8cf4bfd4b15da7d39c61fd2 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 19 Feb 2022 22:42:34 +0100 Subject: [PATCH] Telegram: Various bug fixes --- telegram/burrow-telegram.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/telegram/burrow-telegram.py b/telegram/burrow-telegram.py index 608fd11..11de929 100755 --- a/telegram/burrow-telegram.py +++ b/telegram/burrow-telegram.py @@ -15,9 +15,10 @@ import sys config = ConfigParser() config.read('/usr/local/etc/burrow-telegram') API_TOKEN = config['telegram']['api_token'] -CHATS = map(int, config['telegram']['chats'].split(' ')) +CHATS = list(map(int, config['telegram']['chats'].split(' '))) -formatter = logging.Formatter(fmt="%(asctime)s %(name)s.%(levelname)s: %(message)s", datefmt='%Y-%m-%d %H:%M:%S') +# formatter = logging.Formatter(fmt="%(asctime)s %(name)s.%(levelname)s: %(message)s", datefmt='%Y-%m-%d %H:%M:%S') +formatter = logging.Formatter(fmt="%(message)s") # systemd will handle the rest log_handler = logging.StreamHandler(stream=sys.stdout) log_handler.setFormatter(formatter) logger = logging.getLogger() @@ -37,6 +38,8 @@ async def send_welcome(message: types.Message): @dispatcher.message_handler() async def echo(message: types.Message): print(message) + if message.text.startswith("xyzzy"): + await send_msg("Nothing happens.") # await message.answer(message.text) @@ -143,14 +146,13 @@ async def mqtt_watcher(): await mqtt_loop() except asyncio_mqtt.MqttError as error: logger.error(f"MQTT error: {error}") - finally: - await asyncio.sleep(10) + await asyncio.sleep(10) async def fortunes(): await asyncio.sleep(5*60) while True: - proc = await asyncio.create_subprocess_exec('fortune', stdout=asyncio.subprocess.PIPE) + proc = await asyncio.create_subprocess_exec('/usr/games/fortune', stdout=asyncio.subprocess.PIPE) out, err = await proc.communicate() if proc.returncode == 0: await send_msg(out.decode()) @@ -161,10 +163,14 @@ async def fortunes(): async def main(): loop = asyncio.get_event_loop() - t1 = loop.create_task(mqtt_watcher()) - t2 = loop.create_task(dispatcher.start_polling(timeout=20, relax=0.01, fast=True, allowed_updates=None)) - t3 = loop.create_task(fortunes()) - await asyncio.wait((t1, t2, t3)) + 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(fortunes()), + ] + for coro in asyncio.as_completed(coros): + done = await coro + done.result() # The coroutine probably died of an exception, which is raised here. asyncio.run(main()) -- 2.39.2