ForcePilot/backend/package/yuxi/channels/adapters/matrix/location.py
Kris 2063145ce1 feat(matrix): 新增完整的Matrix适配器工具集
新增了20+个Matrix相关工具模块,包含线程处理、自动加入、消息去重、配置管理、安全策略、媒体处理、命令解析等功能,完善Matrix适配器基础能力
2026-05-12 00:45:54 +08:00

70 lines
1.8 KiB
Python

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,
},
}