Z User commited on
Commit
31a92da
·
1 Parent(s): 20f5934

fix: restore WEIXIN_TOKEN from persisted account file on startup

Browse files

After container rebuild, WEIXIN_TOKEN was lost because HF Space Secrets
don't include it (it's obtained via QR scan). The gateway's own auto-restore
logic requires WEIXIN_ACCOUNT_ID + account JSON file, but token was empty
because the account file may have been corrupted during crash loops.

Now start.sh explicitly reads the token from the persisted account JSON
file (/data/hermes/weixin/accounts/<id>.json) and exports WEIXIN_TOKEN
before the gateway starts, ensuring the weixin adapter has valid credentials.

Files changed (1) hide show
  1. start.sh +31 -12
start.sh CHANGED
@@ -33,22 +33,41 @@ if [ ! -L "$WEIXIN_DIR" ]; then
33
  echo "Symlink: weixin -> /data/hermes/weixin"
34
  fi
35
 
36
- # Auto-discover WeChat account_id from persisted session files
37
  # The QR login flow saves credentials to ~/.hermes/weixin/accounts/<account_id>.json
38
- # We export it as WEIXIN_ACCOUNT_ID so the gateway picks it up without manual config
39
- if [ -z "$WEIXIN_ACCOUNT_ID" ]; then
40
- ACCOUNTS_DIR="/data/hermes/weixin/accounts"
41
- if [ -d "$ACCOUNTS_DIR" ]; then
42
- LATEST=$(find "$ACCOUNTS_DIR" -name "*.json" ! -name "*.context-tokens.json" ! -name "*.sync.json" -type f -printf '%T@ %p\n' 2>/dev/null | sort -rn | head -1 | awk '{print $2}')
43
- if [ -n "$LATEST" ]; then
44
- DISCOVERED_ID=$(basename "$LATEST" .json)
45
- export WEIXIN_ACCOUNT_ID="$DISCOVERED_ID"
46
- echo "Auto-discovered WEIXIN_ACCOUNT_ID=$DISCOVERED_ID"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  else
48
- echo "No WeChat account files found in $ACCOUNTS_DIR"
49
  fi
50
  else
51
- echo "WeChat accounts directory not found (first-time QR login needed)"
52
  fi
53
  fi
54
 
 
33
  echo "Symlink: weixin -> /data/hermes/weixin"
34
  fi
35
 
36
+ # Auto-discover WeChat account_id AND token from persisted session files
37
  # The QR login flow saves credentials to ~/.hermes/weixin/accounts/<account_id>.json
38
+ # We export both WEIXIN_ACCOUNT_ID and WEIXIN_TOKEN so the gateway picks them up
39
+ ACCOUNTS_DIR="/data/hermes/weixin/accounts"
40
+ if [ -z "$WEIXIN_ACCOUNT_ID" ] && [ -d "$ACCOUNTS_DIR" ]; then
41
+ LATEST=$(find "$ACCOUNTS_DIR" -name "*.json" ! -name "*.context-tokens.json" ! -name "*.sync.json" -type f -printf '%T@ %p\n' 2>/dev/null | sort -rn | head -1 | awk '{print $2}')
42
+ if [ -n "$LATEST" ]; then
43
+ DISCOVERED_ID=$(basename "$LATEST" .json)
44
+ export WEIXIN_ACCOUNT_ID="$DISCOVERED_ID"
45
+ echo "Auto-discovered WEIXIN_ACCOUNT_ID=$DISCOVERED_ID"
46
+ else
47
+ echo "No WeChat account files found in $ACCOUNTS_DIR"
48
+ fi
49
+ fi
50
+
51
+ # Also restore WEIXIN_TOKEN from persisted account file if not already set
52
+ # This is needed because .env generated from HF Space Secrets doesn't contain WEIXIN_TOKEN
53
+ if [ -z "$WEIXIN_TOKEN" ] && [ -n "$WEIXIN_ACCOUNT_ID" ]; then
54
+ ACCOUNT_FILE="$ACCOUNTS_DIR/${WEIXIN_ACCOUNT_ID}.json"
55
+ if [ -f "$ACCOUNT_FILE" ]; then
56
+ DISCOVERED_TOKEN=$(python3 -c "
57
+ import json, sys
58
+ try:
59
+ d = json.load(open('$ACCOUNT_FILE'))
60
+ print(d.get('token', ''))
61
+ except: print('')
62
+ " 2>/dev/null)
63
+ if [ -n "$DISCOVERED_TOKEN" ]; then
64
+ export WEIXIN_TOKEN="$DISCOVERED_TOKEN"
65
+ echo "Restored WEIXIN_TOKEN from persisted account file"
66
  else
67
+ echo "WARNING: WeChat account file found but token is empty — QR re-scan needed"
68
  fi
69
  else
70
+ echo "WARNING: WeChat account file $ACCOUNT_FILE not found QR scan needed"
71
  fi
72
  fi
73