send_daily_report.sh 2.35 KB
#!/bin/bash
# send_daily_report.sh - 发送日报到飞书群聊

set -e

CHAT_ID="$1"
REPORT_FILE="$2"

if [ -z "$CHAT_ID" ] || [ -z "$REPORT_FILE" ]; then
    echo "Usage: $0 <chat_id> <report_file>"
    echo "  report_file: Markdown or text file path"
    exit 1
fi

if [ ! -f "$REPORT_FILE" ]; then
    echo "Error: Report file not found: $REPORT_FILE"
    exit 1
fi

# 获取 token
APP_ID="${FEISHU_APP_ID:-cli_a91b01debab81cd1}"
APP_SECRET="${FE...
if [ -z "$APP_SECRET" ] && [ -f "$HOME/.hermes/.env" ]; then
    APP_SECRET=$(grep "FEISHU_APP_SECRET" "$HOME/.hermes/.env" | cut -d'=' -f2)
fi

if [ -z "$APP_SECRET" ]; then
    echo "Error: FEISHU_APP_SECRET not set"
    exit 1
fi

# 获取 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\"}")

TOKEN=$(echo "$RESPONSE" | grep -o '"tenant_access_token":"[^"]*"' | cut -d'"' -f4)

if [ -z "$TOKEN" ]; then
    echo "Error: Failed to get token"
    echo "$RESPONSE"
    exit 1
fi

# 使用 Python 发送富文本消息
python3 -c "
import json
import urllib.request

# 读取报告内容
with open('$REPORT_FILE', 'r', encoding='utf-8') as f:
    report_content = f.read()

# 提取标题(第一行)
title = report_content.split('\n')[0].replace('#', '').strip()

# 构建富文本内容
content = json.dumps({
    'zh_cn': {
        'title': title,
        'content': [
            [{'tag': 'text', 'text': report_content[:2000]}]  # 限制长度
        ]
    }
})

# 构建请求体
request_body = {
    'receive_id': '$CHAT_ID',
    'msg_type': 'post',
    'content': content
}

# 发送请求
req = urllib.request.Request(
    'https://open.feishu.cn/open-apis/im/v1/messages?receive_id_type=chat_id',
    data=json.dumps(request_body).encode('utf-8'),
    headers={
        'Authorization': 'Bearer $TOKEN',
        'Content-Type': 'application/json'
    },
    method='POST'
)

try:
    with urllib.request.urlopen(req, timeout=30) as response:
        result = json.loads(response.read().decode('utf-8'))
        print(json.dumps(result, indent=2, ensure_ascii=False))
        if result.get('code') == 0:
            print('Daily report sent successfully!')
        else:
            print(f'Error: {result.get(\"msg\")}')
except Exception as e:
    print(f'Error: {e}')
"