-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathappservice_websocket.py
67 lines (58 loc) · 2.8 KB
/
appservice_websocket.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
import asyncio
import aiohttp
import json
import logging
from mautrix.types import Event
logger = logging.getLogger(__name__)
class AppserviceWebsocket:
def __init__(self, url, token, callback):
self.url = url + "/_matrix/client/unstable/fi.mau.as_sync"
self.headers = {
"Authorization": f"Bearer {token}",
"X-Mautrix-Websocket-Version": "3",
}
self.callback = callback
async def start(self):
asyncio.create_task(self._loop())
async def _loop(self):
while True:
try:
logger.info(f"Connecting to {self.url}...")
async with aiohttp.ClientSession(headers=self.headers) as sess:
async with sess.ws_connect(self.url) as ws:
logger.info("Websocket connected.")
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
logger.debug(f"Received websocket message: {msg.data}")
data = msg.json()
if data["status"] == "ok" and data["command"] == "transaction":
logger.debug(f"Websocket transaction {data['txn_id']}")
for event in data["events"]:
try:
logger.debug(f"Processing event: {event}")
await self.callback(Event.deserialize(event))
except Exception as e:
logger.error(f"Error processing event: {e}", exc_info=True)
await ws.send_str(
json.dumps(
{
"command": "response",
"id": data["id"],
"data": {},
}
)
)
else:
logger.warn("Unhandled WS command: %s", data)
else:
logger.debug(f"Unhandled WS message type: {msg.type}")
logger.info("Websocket disconnected.")
except asyncio.CancelledError:
logger.info("Websocket was cancelled.")
return
except Exception as e:
logger.error(f"Websocket error: {e}", exc_info=True)
try:
await asyncio.sleep(5)
except asyncio.CancelledError:
return