From 0536be7c2c2c4dc8e6232333fcb2152a0d8589b0 Mon Sep 17 00:00:00 2001 From: "James E. Blair" Date: Fri, 20 Aug 2021 09:35:38 -0700 Subject: [PATCH] Matrix-eavesdrop: handle notices This will log the gerritbot messages Change-Id: Ic777bf8f8b7d48ae177ea432071df232a39ee9a2 --- docker/matrix-eavesdrop/src/eavesdrop/bot.py | 23 +++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/docker/matrix-eavesdrop/src/eavesdrop/bot.py b/docker/matrix-eavesdrop/src/eavesdrop/bot.py index 01e7a16e85..f7426bcebf 100644 --- a/docker/matrix-eavesdrop/src/eavesdrop/bot.py +++ b/docker/matrix-eavesdrop/src/eavesdrop/bot.py @@ -28,7 +28,10 @@ import datetime logging.basicConfig(level=logging.INFO) -from nio import AsyncClient, AsyncClientConfig, LoginResponse, RoomMessageText +from nio import ( + AsyncClient, AsyncClientConfig, LoginResponse, + RoomMessageText, RoomMessageNotice +) from nio.store.database import DefaultStore @@ -135,10 +138,28 @@ class Bot: with open(logpath, 'a') as f: f.write(line) + async def notice_callback(self, room, event): + config_room = self.room_map.get(room.room_id) + if not config_room: + return + room_name = config_room['id'].split(':')[0] + ts = datetime.datetime.utcfromtimestamp(event.server_timestamp/1000.0) + event_date = str(ts.date()) + event_time = str(ts.time())[:8] + room_path = self.get_room_path(config_room) + filename = f'{room_name}.{event_date}.log' + logpath = os.path.join(room_path, filename) + body = event.body + line = f'{event_date}T{event_time} -{event.sender}- {body}\n' + self.log.info('Logging %s %s', room.room_id, line[:-1]) + with open(logpath, 'a') as f: + f.write(line) + async def run(self): await self.login() await self.join_rooms() self.client.add_event_callback(self.message_callback, RoomMessageText) + self.client.add_event_callback(self.notice_callback, RoomMessageNotice) try: await self.client.sync_forever(timeout=30000, full_state=True) finally: