Files
skills_repo/event-publisher/scripts/upload_event.sh
jeremygan2021 f5117a90d1 first commit
2026-03-04 19:24:05 +08:00

232 lines
6.8 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/bin/bash
# 活动上传脚本 - 活动发布 skill
# 支持:上传图片 -> 发布活动
# 获取脚本所在目录的绝对路径
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SKILL_DIR="$(dirname "$SCRIPT_DIR")"
ASSETS_DIR="$SKILL_DIR/assets"
# 加载环境变量
if [ -f "$SCRIPT_DIR/.env" ]; then
source "$SCRIPT_DIR/.env"
elif [ -f "$SKILL_DIR/.env" ]; then
source "$SKILL_DIR/.env"
fi
# 默认配置
UPLOAD_API_URL="${UPLOAD_API_URL:-https://data.tangledup-ai.com/upload?folder=images%2Factivity_banner}"
ACTIVITY_API_URL="${ACTIVITY_API_URL:-https://market.quant-speed.com/api/community/admin-publish/publish_activity/}"
APIKEY="${APIKEY:-123quant-speed}"
PHONE_NUMBER="${PHONE_NUMBER:-18585164448}"
CSRF_TOKEN="${CSRF_TOKEN:-SLbtIrU87YwAvDxE8PfmdG84RqQPp6HeRSEFDvnQEf9fgsElipoRKNJO5oCgabcJ}"
# 上传图片到OSS
upload_image() {
local image_file="$1"
echo "======================================"
echo "📤 步骤1: 上传图片到OSS"
echo "======================================"
echo ""
if [ ! -f "$image_file" ]; then
echo "❌ 图片文件不存在: $image_file"
return 1
fi
echo ">>> 正在上传: $image_file"
# 上传图片
response=$(curl -s -X POST "$UPLOAD_API_URL" \
-H "accept: application/json" \
-H "Content-Type: multipart/form-data" \
-F "file=@$image_file")
# 解析响应
echo "$response" | python3 -c "
import sys, json
try:
data = json.load(sys.stdin)
if data.get('success'):
print('✅ 图片上传成功!')
print(' File URL:', data.get('file_url'))
print(' Object Key:', data.get('object_key'))
else:
print('❌ 图片上传失败:', data.get('message'))
except:
print('❌ 解析响应失败')
print(response)
"
# 提取URL
local file_url=$(echo "$response" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('file_url', ''))" 2>/dev/null)
if [ -n "$file_url" ]; then
echo ""
echo "UPLOAD_URL=$file_url"
return 0
else
return 1
fi
}
# 发布活动 - 使用文件作为description
publish_activity() {
local banner_url="$1"
local title="$2"
local description_file="$3"
local start_time="$4"
local end_time="$5"
local location="$6"
local max_participants="${7:-100}"
echo "======================================"
echo "📝 步骤2: 发布活动"
echo "======================================"
echo ""
echo ">>> 活动标题: $title"
echo ">>> 活动时间: $start_time - $end_time"
echo ">>> 活动地点: $location"
echo ">>> 海报URL: $banner_url"
# 读取description文件内容
local description=""
if [ -f "$description_file" ]; then
description=$(cat "$description_file")
echo ">>> 活动描述文件: $description_file (已读取)"
else
description="$description_file"
fi
# 转义description中的特殊字符处理JSON
description=$(echo "$description" | python3 -c "import sys,json; print(json.dumps(sys.stdin.read()))" | sed 's/^"//' | sed 's/"$//')
echo ""
# 构建活动数据
local activity_json="{
\"signup_form_config\": \"\",
\"description\": \"$description\",
\"title\": \"$title\",
\"banner\": null,
\"banner_url\": \"$banner_url\",
\"start_time\": \"$start_time\",
\"end_time\": \"$end_time\",
\"location\": \"$location\",
\"max_participants\": $max_participants,
\"is_paid\": false,
\"price\": \"0\",
\"is_active\": true,
\"is_visible\": true,
\"auto_confirm\": true,
\"ask_name\": true,
\"ask_phone\": true,
\"ask_wechat\": true,
\"ask_company\": true
}"
# 发布活动
response=$(curl -s -X POST "$ACTIVITY_API_URL?apikey=$APIKEY&phone_number=$PHONE_NUMBER" \
-H "accept: */*" \
-H "Content-Type: application/json" \
-H "X-CSRFTOKEN: $csrf_token" \
-d "$activity_json")
echo "$response" | python3 -c "
import sys, json
try:
data = json.load(sys.stdin)
if data.get('id'):
print('✅ 活动发布成功!')
print(' 活动ID:', data.get('id'))
print(' 活动标题:', data.get('title'))
print(' 创建时间:', data.get('created_at'))
else:
print('❌ 活动发布失败')
print(response)
except:
print('❌ 解析响应失败')
print(response)
"
# 提取活动ID
local activity_id=$(echo "$response" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('id', ''))" 2>/dev/null)
if [ -n "$activity_id" ]; then
echo ""
echo "ACTIVITY_ID=$activity_id"
return 0
else
return 1
fi
}
# 完整流程:上传图片并发布活动
upload_and_publish() {
local image_file="$1"
local title="$2"
local description_file="$3"
local start_time="$4"
local end_time="$5"
local location="$6"
local max_participants="${7:-100}"
echo "======================================"
echo "🎉 开始发布活动"
echo "======================================"
echo ""
# 步骤1: 上传图片
local upload_result=$(upload_image "$image_file")
local banner_url=$(echo "$upload_result" | grep "UPLOAD_URL=" | sed 's/UPLOAD_URL=//')
if [ -z "$banner_url" ]; then
echo "❌ 图片上传失败,无法继续发布活动"
return 1
fi
echo ""
# 步骤2: 发布活动传递description文件路径
publish_activity "$banner_url" "$title" "$description_file" "$start_time" "$end_time" "$location" "$max_participants"
echo ""
echo "======================================"
echo "✅ 活动发布完成!"
echo "======================================"
}
# 根据参数调用对应函数
case "$1" in
upload)
upload_image "$2"
;;
publish)
publish_activity "$2" "$3" "$4" "$5" "$6" "$7" "$8"
;;
all)
upload_and_publish "$2" "$3" "$4" "$5" "$6" "$7"
;;
*)
echo "======================================"
echo "📤 活动发布工具"
echo "======================================"
echo ""
echo "用法: $0 <upload|publish|all> [参数]"
echo ""
echo "命令:"
echo " upload <图片文件> - 仅上传图片"
echo " all <图片文件> <标题> <描述文件> <开始时间> <结束时间> <地点> [最大人数]"
echo " - 上传图片并发布活动"
echo ""
echo "时间格式: 2026-03-04T05:30:03Z"
echo ""
echo "示例: "
echo ' $0 upload /path/to/image.jpg'
echo ' $0 all ./assets/image.jpg "春节游园会" "./assets/活动文案.md" "2026-03-15T10:00:00Z" "2026-03-15T16:00:00Z" "市中心广场"'
;;
esac