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()
@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)
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())
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())