File size: 4,114 Bytes
3d325be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/bin/bash
# G5: feishu-setup.sh - 5机器人通道 + 5Agent绑定

set -e

echo "[Feishu] Starting Feishu setup..."

# 配置目录
STATE_DIR="${OPENCLAW_STATE_DIR:-/root/.openclaw}"
CREDENTIALS_DIR="$STATE_DIR/credentials"
mkdir -p "$CREDENTIALS_DIR"

# 飞书凭证文件
FEISHU_CRED_FILE="$CREDENTIALS_DIR/feishu.json"

# 检查是否有飞书配置
has_feishu_config=false
for i in 1 2 3 4 5; do
    app_id_var="FEISHU_APP_ID_$i"
    app_secret_var="FEISHU_SECRET_$i"
    
    if [ -n "${!app_id_var:-}" ] && [ -n "${!app_secret_var:-}" ]; then
        has_feishu_config=true
        break
    fi
done

if [ "$has_feishu_config" = false ]; then
    echo "[Feishu] No Feishu configuration found, skipping setup"
    echo "[Feishu] To enable, set FEISHU_APP_ID_1..5 and FEISHU_SECRET_1..5"
    exit 0
fi

# 初始化JSON数组
FEISHU_BOTS="["
FIRST=true

# 处理5个飞书机器人
for i in 1 2 3 4 5; do
    app_id_var="FEISHU_APP_ID_$i"
    app_secret_var="FEISHU_SECRET_$i"
    encrypt_key_var="FEISHU_ENCRYPT_KEY_$i"
    verification_token_var="FEISHU_VERIFICATION_TOKEN_$i"
    webhook_var="FEISHU_WEBHOOK_$i"
    bind_agent_var="FEISHU_BIND_AGENT_$i"
    
    app_id="${!app_id_var:-}"
    app_secret="${!app_secret_var:-}"
    
    # 跳过未配置的机器人
    if [ -z "$app_id" ] || [ -z "$app_secret" ]; then
        echo "[Feishu] Bot $i: Not configured, skipping"
        continue
    fi
    
    echo "[Feishu] Bot $i: Configuring..."
    
    # 获取tenant_access_token
    TOKEN_RESPONSE=$(curl -s -X POST \
        "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal" \
        -H "Content-Type: application/json" \
        -d "{\"app_id\":\"$app_id\",\"app_secret\":\"$app_secret\"}" 2>/dev/null || echo '{}')
    
    TENANT_TOKEN=$(echo "$TOKEN_RESPONSE" | jq -r '.tenant_access_token // empty')
    CODE=$(echo "$TOKEN_RESPONSE" | jq -r '.code // empty')
    
    if [ -n "$TENANT_TOKEN" ] && [ "$CODE" = "0" ]; then
        echo "[Feishu] Bot $i: Token obtained successfully"
        
        # 获取机器人信息
        BOT_INFO=$(curl -s -X GET \
            "https://open.feishu.cn/open-apis/bot/v3/info" \
            -H "Authorization: Bearer $TENANT_TOKEN" 2>/dev/null || echo '{}')
        
        BOT_NAME=$(echo "$BOT_INFO" | jq -r '.bot.bot_name // "Unknown"')
        echo "[Feishu] Bot $i: Name = $BOT_NAME"
        
        STATUS="active"
    else
        echo "[Feishu] Bot $i: Failed to get token, will retry later"
        TENANT_TOKEN=""
        BOT_NAME="Unknown"
        STATUS="pending"
    fi
    
    # 构建JSON对象
    if [ "$FIRST" = true ]; then
        FIRST=false
    else
        FEISHU_BOTS="$FEISHU_BOTS,"
    fi
    
    FEISHU_BOTS="$FEISHU_BOTS
    {
        \"id\": \"feishu_$i\",
        \"index\": $i,
        \"app_id\": \"$app_id\",
        \"app_secret\": \"$app_secret\",
        \"encrypt_key\": \"${!encrypt_key_var:-}\",
        \"verification_token\": \"${!verification_token_var:-}\",
        \"webhook_url\": \"${!webhook_var:-}\",
        \"tenant_access_token\": \"$TENANT_TOKEN\",
        \"bot_name\": \"$BOT_NAME\",
        \"status\": \"$STATUS\",
        \"bind_agent\": \"${!bind_agent_var:-agent_$i}\",
        \"created_at\": \"$(date -Iseconds)\"
    }"
    
    echo "[Feishu] Bot $i: Configuration saved"
done

FEISHU_BOTS="$FEISHU_BOTS
]"

# 保存凭证文件
cat > "$FEISHU_CRED_FILE" << EOF
{
    "type": "feishu",
    "enabled": true,
    "bots": $FEISHU_BOTS,
    "updated_at": "$(date -Iseconds)"
}
EOF

echo "[Feishu] Setup completed!"
echo "[Feishu] Credentials saved to $FEISHU_CRED_FILE"

# 显示配置摘要
echo ""
echo "[Feishu] Configuration Summary:"
echo "$FEISHU_BOTS" | jq -r '.[] | "  Bot \(.index): \(.bot_name) [\(.status)] -> Agent: \(.bind_agent)"'

# 创建Agent-通道绑定映射
BINDINGS_FILE="$STATE_DIR/agent_bindings.json"
echo "$FEISHU_BOTS" | jq 'map({
    agent_id: .bind_agent,
    channel_id: .id,
    channel_type: "feishu",
    bot_name: .bot_name,
    status: .status
})' > "$BINDINGS_FILE"

echo ""
echo "[Feishu] Agent bindings saved to $BINDINGS_FILE"