This repository has been archived by the owner on May 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
68 lines (54 loc) · 1.84 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env python3
import logging
import asyncio
from nio import (
AsyncClient,
AsyncClientConfig,
RoomMessageText,
InviteEvent,
SyncError,
)
from callbacks import Callbacks
from config import Config
from storage import Storage
from sync_token import SyncToken
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)
client.add_event_callback(callbacks.message, (RoomMessageText,))
client.add_event_callback(callbacks.invite, (InviteEvent,))
# Create a new sync token, attempting to load one from the database if it has one already
sync_token = SyncToken(store)
# Sync loop
while True:
# Sync with the server
sync_response = await client.sync(timeout=30000, full_state=True,
since=sync_token.token)
# Check if the sync had an error
if type(sync_response) == SyncError:
logger.warning("Error in client sync: %s", sync_response.message)
continue
# Save the latest sync token to the database
token = sync_response.next_batch
if token:
sync_token.update(token)
asyncio.get_event_loop().run_until_complete(main())