Files
Lark-Snooper/larkx/region.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

75 lines
2.2 KiB
Python

"""飞书(国内版)与 Lark(国际版)的站点差异。
两版共用同一套 web 客户端代码与协议: 接口路径、proto、access_key 算法完全一致,
区别只在域名、web 端 appId(网关请求头 x-appid)和默认语言。登录时选定区域并写入凭证,
之后所有请求都按凭证里的区域取地址(cookie 只对所属域名有效,不能混用)。
"""
from dataclasses import dataclass
@dataclass(frozen=True)
class Region:
name: str
label: str
domain: str
# 页面 __feishuMeta.teaAppId 的兜底值;登录后会从 messenger 页面抓取实际值存进凭证
web_app_id: str
locale: str
@property
def ticket_url(self) -> str:
return f'https://login.{self.domain}/suite/passport/frontier_ticket/'
@property
def api_host(self) -> str:
return f'https://internal-api-lark-api.{self.domain}'
@property
def user_info_url(self) -> str:
return f'{self.api_host}/accounts/web/user'
@property
def csrf_url(self) -> str:
return f'{self.api_host}/accounts/csrf'
@property
def gateway_url(self) -> str:
return f'{self.api_host}/im/gateway/'
@property
def web_origin(self) -> str:
return f'https://open-dev.{self.domain}'
@property
def messenger_page_url(self) -> str:
return f'{self.web_origin}/messenger/'
@property
def qr_redirect_uri(self) -> str:
return f'{self.web_origin}/next/messenger'
@property
def accounts_origin(self) -> str:
return f'https://accounts.{self.domain}'
@property
def ws_url(self) -> str:
return f'wss://msg-frontier.{self.domain}/ws/v2'
@property
def file_host(self) -> str:
return f'https://internal-api-lark-file.{self.domain}'
REGIONS = {
'feishu': Region(name='feishu', label='飞书', domain='feishu.cn', web_app_id='161471', locale='zh_CN'),
'lark': Region(name='lark', label='Lark', domain='larksuite.com', web_app_id='161471', locale='en_US'),
}
def get_region(name: str) -> Region:
region = REGIONS.get((name or '').strip().lower())
if region is None:
raise ValueError(f"未知区域: {name!r},可选: {', '.join(REGIONS)}")
return region