fix: handle matrix alias encoding
This commit is contained in:
parent
1dbc2de39d
commit
9fb614772f
@ -2,6 +2,7 @@ import asyncio
|
||||
import logging
|
||||
import time
|
||||
from typing import Any
|
||||
from urllib.parse import quote
|
||||
|
||||
import httpx
|
||||
|
||||
@ -31,12 +32,20 @@ class MatrixClient:
|
||||
return data.get("access_token", "")
|
||||
|
||||
async def resolve_room(self, token: str) -> str:
|
||||
url = f"{self._settings.matrix_base}/_matrix/client/v3/directory/room/{self._settings.room_alias}"
|
||||
alias = quote(self._settings.room_alias, safe="")
|
||||
url = f"{self._settings.matrix_base}/_matrix/client/v3/directory/room/{alias}"
|
||||
headers = {"Authorization": f"Bearer {token}"}
|
||||
async with httpx.AsyncClient(timeout=15.0) as client:
|
||||
try:
|
||||
resp = await client.get(url, headers=headers)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
except httpx.HTTPError as exc:
|
||||
log.warning(
|
||||
"matrix resolve_room failed",
|
||||
extra={"extra": {"error": str(exc), "alias": self._settings.room_alias}},
|
||||
)
|
||||
return ""
|
||||
return data.get("room_id", "")
|
||||
|
||||
async def join_room(self, token: str, room_id: str) -> None:
|
||||
@ -77,10 +86,18 @@ class MatrixBot:
|
||||
self._answer_handler = answer_handler
|
||||
|
||||
async def run(self) -> None:
|
||||
while True:
|
||||
try:
|
||||
token = await self._client.login()
|
||||
room_id = await self._client.resolve_room(token)
|
||||
if room_id:
|
||||
await self._client.join_room(token, room_id)
|
||||
await self._sync_loop(token)
|
||||
except Exception as exc:
|
||||
log.warning("matrix bootstrap failed", extra={"extra": {"error": str(exc)}})
|
||||
await asyncio.sleep(10)
|
||||
|
||||
async def _sync_loop(self, token: str) -> None:
|
||||
since = None
|
||||
while True:
|
||||
try:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user