Merge "Matrix-eavesdrop: handle notices"

This commit is contained in:
Zuul 2021-08-20 17:52:34 +00:00 committed by Gerrit Code Review
commit df5322df97
1 changed files with 22 additions and 1 deletions

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: