#!/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"