2026-02-09 22:57:38 +05:30
|
|
|
import asyncio
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
2026-02-10 02:01:15 +01:00
|
|
|
from nio import AsyncClient, AsyncClientConfig, InviteEvent, MatrixRoom, RoomMessageText
|
2026-02-09 22:57:38 +05:30
|
|
|
|
|
|
|
|
from nanobot.bus.events import OutboundMessage
|
2026-02-10 02:01:15 +01:00
|
|
|
from nanobot.channels.base import BaseChannel
|
|
|
|
|
from nanobot.config.loader import get_data_dir
|
2026-02-09 22:57:38 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
class MatrixChannel(BaseChannel):
|
|
|
|
|
"""
|
|
|
|
|
Matrix (Element) channel using long-polling sync.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
name = "matrix"
|
|
|
|
|
|
|
|
|
|
def __init__(self, config: Any, bus):
|
|
|
|
|
super().__init__(config, bus)
|
|
|
|
|
self.client: AsyncClient | None = None
|
|
|
|
|
self._sync_task: asyncio.Task | None = None
|
|
|
|
|
|
|
|
|
|
async def start(self) -> None:
|
|
|
|
|
self._running = True
|
|
|
|
|
|
2026-02-10 02:01:15 +01:00
|
|
|
store_path = get_data_dir() / "matrix-store"
|
|
|
|
|
store_path.mkdir(parents=True, exist_ok=True)
|
2026-02-09 22:57:38 +05:30
|
|
|
|
2026-02-10 02:01:15 +01:00
|
|
|
self.client = AsyncClient(
|
|
|
|
|
homeserver=self.config.homeserver,
|
|
|
|
|
user=self.config.user_id,
|
|
|
|
|
store_path=store_path, # Where tokens are saved
|
|
|
|
|
config=AsyncClientConfig(
|
|
|
|
|
store_sync_tokens=True, # Auto-persists next_batch tokens
|
|
|
|
|
encryption_enabled=True,
|
|
|
|
|
),
|
2026-02-09 22:57:38 +05:30
|
|
|
)
|
|
|
|
|
|
2026-02-10 02:01:15 +01:00
|
|
|
self.client.user_id = self.config.user_id
|
|
|
|
|
self.client.access_token = self.config.access_token
|
|
|
|
|
self.client.device_id = self.config.device_id
|
|
|
|
|
|
|
|
|
|
self.client.add_event_callback(self._on_message, RoomMessageText)
|
|
|
|
|
self.client.add_event_callback(self._on_room_invite, InviteEvent)
|
|
|
|
|
|
2026-02-10 09:05:20 +01:00
|
|
|
if self.config.device_id:
|
|
|
|
|
self.client.load_store()
|
2026-02-10 02:01:15 +01:00
|
|
|
|
2026-02-09 22:57:38 +05:30
|
|
|
self._sync_task = asyncio.create_task(self._sync_loop())
|
|
|
|
|
|
|
|
|
|
async def stop(self) -> None:
|
|
|
|
|
self._running = False
|
|
|
|
|
if self._sync_task:
|
|
|
|
|
self._sync_task.cancel()
|
|
|
|
|
if self.client:
|
|
|
|
|
await self.client.close()
|
|
|
|
|
|
|
|
|
|
async def send(self, msg: OutboundMessage) -> None:
|
|
|
|
|
if not self.client:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
await self.client.room_send(
|
|
|
|
|
room_id=msg.chat_id,
|
|
|
|
|
message_type="m.room.message",
|
|
|
|
|
content={"msgtype": "m.text", "body": msg.content},
|
2026-02-10 02:01:15 +01:00
|
|
|
ignore_unverified_devices=True,
|
2026-02-09 22:57:38 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def _sync_loop(self) -> None:
|
|
|
|
|
while self._running:
|
|
|
|
|
try:
|
2026-02-10 02:01:15 +01:00
|
|
|
await self.client.sync_forever(timeout=30000, full_state=True)
|
2026-02-09 22:57:38 +05:30
|
|
|
except asyncio.CancelledError:
|
|
|
|
|
break
|
|
|
|
|
except Exception:
|
|
|
|
|
await asyncio.sleep(2)
|
|
|
|
|
|
2026-02-10 09:05:20 +01:00
|
|
|
async def _on_room_invite(self, room: MatrixRoom, event: InviteEvent) -> None:
|
|
|
|
|
allow_from = self.config.allow_from or []
|
|
|
|
|
if allow_from and event.sender not in allow_from:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
await self.client.join(room.room_id)
|
2026-02-10 02:01:15 +01:00
|
|
|
|
|
|
|
|
async def _on_message(self, room: MatrixRoom, event: RoomMessageText) -> None:
|
2026-02-09 22:57:38 +05:30
|
|
|
# Ignore self messages
|
|
|
|
|
if event.sender == self.config.user_id:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
await self._handle_message(
|
|
|
|
|
sender_id=event.sender,
|
|
|
|
|
chat_id=room.room_id,
|
|
|
|
|
content=event.body,
|
|
|
|
|
metadata={"room": room.display_name},
|
2026-02-10 02:01:15 +01:00
|
|
|
)
|