feat: improve feishu notification formatting

- Refactor the message construction in `send_feishu_notification` to use `jq` for better readability and maintainability.
- Update the email and feishu notification content preparation in `main.sh` to use consistent variable names for subject and content.

These changes enhance the clarity of the notification messages and ensure that the code is easier to modify in the future. The use of `jq` also reduces the risk of syntax errors in JSON formatting.
This commit is contained in:
songtianlun 2025-01-04 18:57:33 +08:00
parent 3f4185589d
commit cce33ee405
2 changed files with 23 additions and 31 deletions

View File

@ -7,27 +7,24 @@ CONTENT="$3"
# 飞书通知
send_feishu_notification() {
local message=$(cat <<EOF
{
"msg_type": "post",
"content": {
"post": {
"zh_cn": {
"title": "$TITLE",
"content": [
[
{
"tag": "text",
"text": "$CONTENT"
}
]
]
local message=$(jq -n \
--arg title "$TITLE" \
--arg text "$CONTENT" \
'{
msg_type: "post",
content: {
post: {
zh_cn: {
title: $title,
content: [[{
tag: "text",
text: $text
}]]
}
}
}
}
}
}
EOF
)
}'
)
curl -s -X POST "$WEBHOOK_URL" -H "Content-Type: application/json" -d "$message"
}

17
main.sh
View File

@ -54,7 +54,8 @@ main() {
mirror_exit_code=$?
# 准备邮件内容
summary="GitHub to Gitea 同步报告
notice_subject="GitHub 同步$([ $mirror_exit_code -eq 0 ] && echo "成功" || echo "失败") - $(date '+%Y-%m-%d')"
notice_content="GitHub to Gitea 同步报告
运行时间: $(date '+%Y-%m-%d %H:%M:%S')
@ -98,8 +99,6 @@ $(cat "$LOG_FILE")
# 如果启用了邮件通知,调用 mail.sh
if [ "$ENABLE_MAIL" = "true" ]; then
subject="GitHub 同步$([ $mirror_exit_code -eq 0 ] && echo "成功" || echo "失败") - $(date '+%Y-%m-%d')"
bash "$SCRIPT_DIR/mail.sh" \
"$SMTP_SERVER" \
"$SMTP_PORT" \
@ -107,20 +106,16 @@ $(cat "$LOG_FILE")
"$SMTP_PASS" \
"$MAIL_TO" \
"$MAIL_FROM" \
"$subject" \
"$summary"
"$notice_subject" \
"$notice_content"
fi
# 准备飞书通知内容
feishu_title="GitHub 同步$([ $mirror_exit_code -eq 0 ] && echo "成功" || echo "失败")"
feishu_content="GitHub to Gitea 同步报告\n\n$(tail -n 50 "$LOG_FILE")"
# 如果启用了飞书通知,调用 feishu_notify.sh
if [ "$ENABLE_FEISHU" = "true" ]; then
bash "$SCRIPT_DIR/feishu_notify.sh" \
"$FEISHU_WEBHOOK_URL" \
"$feishu_title" \
"$feishu_content"
"$notice_subject" \
"$notice_content"
fi