from __future__ import annotations from typing import Any _LOCATION_TEMPLATES: dict[str, str] = { "m.location": "[位置] {body}", "geo_uri": "[位置] geo:{uri}", } def parse_location_event(event_source: dict[str, Any]) -> dict[str, Any] | None: content = event_source.get("content", {}) if content.get("msgtype") == "m.location": geo_uri = content.get("geo_uri", "") body = content.get("body", "") return { "type": "location", "body": body, "geo_uri": geo_uri, "text": format_location_text(body, geo_uri), } geo_uri = content.get("geo_uri", "") if geo_uri and geo_uri.startswith("geo:"): return { "type": "location", "geo_uri": geo_uri, "text": format_location_text("", geo_uri), } return None def format_location_text(body: str, geo_uri: str) -> str: if body: return f"[位置] {body}" + (f" ({geo_uri})" if geo_uri else "") if geo_uri: return f"[位置] {geo_uri}" return "[位置]" def extract_geo_coordinates(geo_uri: str) -> dict[str, float] | None: if not geo_uri.startswith("geo:"): return None coords = geo_uri[4:].split(";")[0] try: lat, lon = coords.split(",", 1) return {"lat": float(lat), "lon": float(lon)} except (ValueError, IndexError): return None def build_outbound_location( body: str, lat: float, lon: float, description: str = "", ) -> dict[str, Any]: return { "msgtype": "m.location", "body": body or f"{lat},{lon}", "geo_uri": f"geo:{lat},{lon}", "org.matrix.msc1767.location": { "latitude": lat, "longitude": lon, "description": description, }, }