import json, requests

conf = json.load(open('/root/.openclaw/credentials/xiaoxi/config.json'))
app = conf['apps'][0]

# Get tenant access token
r = requests.post("https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal", json={
    "app_id": app['appId'],
    "app_secret": app['appSecret']
})
token = r.json().get('tenant_access_token', '')
print(f"Token: {'OK' if token else 'FAILED'}")

# Check data dict doc
for doc_id, name in [("YEk6dWmsHoT02lxlO3tcNBrHnXe", "数据字典"), ("HmxMd58OeoNXlsxpICLccorhnve", "数据地图")]:
    r = requests.get(f"https://open.feishu.cn/open-apis/docx/v1/documents/{doc_id}/raw_content",
                     headers={"Authorization": f"Bearer {token}"})
    d = r.json()
    if d.get('code') == 0:
        blocks = d['data'].get('blocks', [])
        print(f"\n[{name}] Blocks: {len(blocks)}")
        for b in blocks[:3]:
            bt = b.get('block_type', '?')
            content = b.get('text') or b.get('heading1') or b.get('heading2') or b.get('heading3') or b.get('heading4') or {}
            if isinstance(content, dict):
                txt = ''.join(e.get('text_run', {}).get('content', '') for e in content.get('elements', []))
                print(f"  [{bt}] {txt[:100]}")
        if len(blocks) > 3:
            print(f"  ... ({len(blocks)} blocks total)")
    else:
        print(f"\n[{name}] Error: {d.get('code')} {d.get('msg')}")
