matrix-alertbot/main.py

67 lines
1.7 KiB
Python
Raw Normal View History

2019-09-25 14:26:29 +02:00
#!/usr/bin/env python3
import logging
import asyncio
from nio import (
AsyncClient,
AsyncClientConfig,
RoomMessageText,
InviteEvent,
2019-09-29 18:30:57 +02:00
SyncError,
2019-09-25 14:26:29 +02:00
)
from callbacks import Callbacks
from config import Config
from storage import Storage
logger = logging.getLogger(__name__)
async def main():
# Read config file
config = Config("config.yaml")
# Configure the database
store = Storage(config.database_filepath)
# Configuration options for the AsyncClient
client_config = AsyncClientConfig(
max_limit_exceeded=0,
max_timeouts=0,
)
# Initialize the matrix client
client = AsyncClient(
config.homeserver_url,
config.user_id,
device_id=config.device_id,
config=client_config,
)
# Assign an access token to the bot instead of logging in and creating a new device
client.access_token = config.access_token
# Set up event callbacks
callbacks = Callbacks(client, store, config)
2019-09-25 14:26:29 +02:00
client.add_event_callback(callbacks.message, (RoomMessageText,))
client.add_event_callback(callbacks.invite, (InviteEvent,))
# Retrieve the last sync token if it exists
token = store.get_sync_token()
# Sync loop
while True:
# Sync with the server
sync_response = await client.sync(timeout=30000, full_state=True, since=token)
2019-09-29 18:30:57 +02:00
# Check if the sync had an error
if type(sync_response) == SyncError:
logger.warning("Error in client sync: %s", sync_response.message)
continue
2019-09-25 14:26:29 +02:00
# Save the latest sync token
token = sync_response.next_batch
if token:
store.save_sync_token(token)
asyncio.get_event_loop().run_until_complete(main())