matrix-room-exporter/main.py

61 lines
1.8 KiB
Python

import argparse
import asyncio
import logging
import os
from pathlib import Path
import yaml
from nio import InviteMemberEvent, RoomMessageText
from exporter.callbacks import MatrixBotCallbacks
from exporter.config import DEFAULT_CONFIG
from exporter.matrix import MatrixClientHelper
SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__))
async def main(args):
logging.basicConfig()
logger = logging.getLogger('MatrixGPT')
with open(args.config) as stream:
try:
yaml_config = yaml.safe_load(stream)
except yaml.YAMLError as exc:
logger.critical(f'Failed to load config: {exc}')
quit(1)
config = DEFAULT_CONFIG | yaml_config
logger.setLevel(logging.DEBUG if args.debug else logging.INFO)
logger.debug('Debug logging enabled!')
matrix_helper = MatrixClientHelper(
user_id=config['auth']['username'],
passwd=config['auth']['password'],
homeserver=config['auth']['homeserver'],
store_path=config['store_path'],
)
client = matrix_helper.client
login_success, login_response = await matrix_helper.login()
if not login_success:
logger.critical(f'Failed to login: {login_response}')
quit(1)
logger.info(f'Logged in as {client.user_id}')
callbacks = MatrixBotCallbacks(client, config)
client.add_event_callback(callbacks.handle_invite, InviteMemberEvent)
client.add_event_callback(callbacks.handle_message, RoomMessageText)
await client.sync_forever(timeout=30000)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Export a Matrix room.')
parser.add_argument('--config', default=Path(SCRIPT_PATH) / 'config.yml')
parser.add_argument('--debug', '-d', action='store_true', help='Enable debug logging.')
args = parser.parse_args()
asyncio.run(main(args))