Matrix-eavesdrop: handle notices

This will log the gerritbot messages

Change-Id: Ic777bf8f8b7d48ae177ea432071df232a39ee9a2
This commit is contained in:
James E. Blair 2021-08-20 09:35:38 -07:00
parent 8ad47150e7
commit 0536be7c2c

View File

@ -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: