Files
Lark-Snooper/larkx/config.py
thxzzzzz b9c2d50d62 feat: support Lark (larksuite.com) alongside Feishu
Add larkx/region.py with per-region endpoints, gateway app id and
locale. The region is chosen at login (lark auth qr/import --region,
default LARKX_REGION) and persisted in credentials; every request derives
its URLs from it. Credentials saved before this change are treated as
Feishu.

Fixes found while testing with real Lark and Feishu accounts:
- search: locale belongs on the request header, not searchContext
- storage: drop inline thumbnail bytes so content_data stays valid JSON
- send --root: set parentId, plus isReplyInThread for regular groups;
  topic-mode groups reject that flag, so chat modes are looked up and
  cached per chat
- topic groups (GROUP + chatMode THREAD/THREAD_V2): root messages anchor
  their own thread, except system notices; push handling and
  pull_history share decoders.resolve_scope
- show media/audio durations in seconds (they are milliseconds)
- render @mentions with the mentioned name via AtProperty
- lark send stores the server message id so the WS echo fills in the
  same row instead of inserting a duplicate

proto.proto gains isReplyInThread, AtProperty and PutMessageResponse;
proto_pb2.py is regenerated with grpcio-tools 1.71 (protoc 5.29.0).
2026-09-30 16:04:49 +08:00

80 lines
2.1 KiB
Python

import json
import os
from pathlib import Path
from dotenv import load_dotenv
load_dotenv()
_home_env = os.environ.get("LARKX_HOME") or str(Path.home() / ".larkx")
DATA_DIR = Path(os.path.expanduser(_home_env))
CREDENTIALS_PATH = DATA_DIR / "credentials.json"
DB_DEFAULT_PATH = DATA_DIR / "messages.db"
DEFAULTS = {
"region": "feishu",
"storage_url": f"sqlite:///{DB_DEFAULT_PATH}",
"context_scope": "anchor",
"agent_backend": "claude",
"system_prompt": "",
"mark_read": True,
"triggers": {
"prefix": "",
"chat_types": [],
"include_chats": [],
"exclude_chats": [],
"include_senders": [],
"keywords": [],
"group_at_only": False,
},
}
TRIGGER_ENV = {
"prefix": "LARKX_TRIGGER_PREFIX",
"chat_types": "LARKX_TRIGGER_CHAT_TYPES",
"include_chats": "LARKX_TRIGGER_INCLUDE_CHATS",
"exclude_chats": "LARKX_TRIGGER_EXCLUDE_CHATS",
"include_senders": "LARKX_TRIGGER_INCLUDE_SENDERS",
"keywords": "LARKX_TRIGGER_KEYWORDS",
"group_at_only": "LARKX_TRIGGER_GROUP_AT_ONLY",
}
def _as_list(v: str):
v = v.strip()
if v.startswith("["):
try:
return json.loads(v)
except Exception:
return []
return [x.strip() for x in v.split(",") if x.strip()]
def _as_bool(v: str):
return str(v).strip().lower() in ("1", "true", "yes", "on")
def load_config() -> dict:
cfg = dict(DEFAULTS)
for key in cfg:
if key == "triggers":
continue
v = os.environ.get("LARKX_" + key.upper())
if v:
cfg[key] = v
if isinstance(cfg.get("mark_read"), str):
cfg["mark_read"] = cfg["mark_read"].strip().lower() in ("1", "true", "yes", "on")
triggers = dict(DEFAULTS["triggers"])
for key, env_name in TRIGGER_ENV.items():
v = os.environ.get(env_name)
if v is None:
continue
if key == "group_at_only":
triggers[key] = _as_bool(v)
elif isinstance(DEFAULTS["triggers"][key], list):
triggers[key] = _as_list(v)
else:
triggers[key] = v
cfg["triggers"] = triggers
return cfg