import json, time, requests, websocket
BASE = "http://127.0.0.1:8081"
MAIN_CHAIN = "f557fb5a-ff64-493d-a1f6-3b3fa41cd94a"
token = requests.post(f"{BASE}/api/login", json={"username":"cxbsoft","password":"cf5584aca177a811ffda8f4789e2e143"}).json()["data"]["token"]
ws = websocket.create_connection(f"ws://127.0.0.1:8081/api/user/friend-agent/ws", header={"Authorization": f"Bearer {token}"}, timeout=60)
msg = {"type":"req","request_id":"kbtest-1","method":"chains.send","payload":{"chain_id":MAIN_CHAIN,"content":"hi"}}
ws.send(json.dumps(msg))
end = time.time() + 15
while time.time() < end:
    try:
        ws.settimeout(5); raw = ws.recv()
    except websocket.WebSocketTimeoutException: continue
    except Exception as e: print("ws err",e); break
    frame = json.loads(raw)
    if frame.get("type")=="res" and frame.get("request_id")=="kbtest-1":
        print("FULL RES:", json.dumps(frame, ensure_ascii=False))
        break
ws.close()
