Skip to content

Commit

Permalink
美化消息
Browse files Browse the repository at this point in the history
  • Loading branch information
frankiejun committed Dec 28, 2024
1 parent 4e11be7 commit 8cb0a5d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
TELEGRAM_USERID: ${{secrets.TELEGRAM_USERID}}
WXSENDKEY: ${{secrets.WXSENDKEY}}
SENDTYPE: ${{secrets.SENDTYPE}}
BUTTON_URL: ${{secrets.BUTTON_URL}}
run: |
chmod +x ./revive.sh
./revive.sh
11 changes: 9 additions & 2 deletions keepalive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ sendtype=$2
TELEGRAM_TOKEN="$3"
TELEGRAM_USERID="$4"
WXSENDKEY="$5"
BUTTON_URL="$6"


checkHy2Alive() {
Expand Down Expand Up @@ -58,7 +59,8 @@ makeMsgConfig(){
"telegram_token": "$TELEGRAM_TOKEN",
"telegram_userid": "$TELEGRAM_USERID",
"wxsendkey": "$WXSENDKEY",
"sendtype": "$sendtype"
"sendtype": "$sendtype",
"button_url: "$BUTTON_URL"
}
EOF
}
Expand Down Expand Up @@ -217,7 +219,12 @@ else
sendtype=$send_type
fi

export TELEGRAM_TOKEN TELEGRAM_USERID WXSENDKEY sendtype
if [ -z "$BUTTON_URL" ]; then
echo "从msg.json获取 BUTTON_URL"
BUTTON_URL=$(jq -r ".button_url // empty" msg.json)
fi

export TELEGRAM_TOKEN TELEGRAM_USERID WXSENDKEY sendtype BUTTON_URL

#echo "最终TELEGRAM_TOKEN=$TELEGRAM_TOKEN,TELEGRAM_USERID=$TELEGRAM_USERID"
host=$(hostname)
Expand Down
6 changes: 3 additions & 3 deletions revive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ for info in "${hosts_info[@]}"; do
port=$(echo $info | jq -r ".port")
pass=$(echo $info | jq -r ".password")

script="/home/$user/serv00-play/keepalive.sh autoupdate ${SENDTYPE} ${TELEGRAM_TOKEN} ${TELEGRAM_USERID} ${WXSENDKEY}"
script="/home/$user/serv00-play/keepalive.sh autoupdate ${SENDTYPE} ${TELEGRAM_TOKEN} ${TELEGRAM_USERID} ${WXSENDKEY} ${BUTTON_URL}"

output=$(sshpass -p "$pass" ssh -o StrictHostKeyChecking=no -p "$port" "$user@$host" "bash -s" <<< "$script")
output=$(sshpass -p "$pass" ssh -o StrictHostKeyChecking=no -p "$port" "$user@$host" "bash -s" <<<"$script")

echo "output:$output"
if echo "$output" | grep -q "更新完毕"; then
echo "登录成功"
else
echo "登录失败"
./tgsend.sh "主机:$host,用户名:$user,登录失败请检查!"
./tgsend.sh "Host:$host,user:$user,登录失败请检查!"
fi
done
3 changes: 2 additions & 1 deletion ssl/cronSSL.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
installpath="$HOME"
domain=$1
host="$(hostname | cut -d '.' -f 1)"
user=$(whoami)
sno=${host/s/web}
webIp=$(devil vhost list public | grep "$sno" | awk '{print $1}')
resp=$(devil ssl www add $webIp le le $domain)
Expand All @@ -18,7 +19,7 @@ if [[ "$resp" =~ .*succesfully.*$ ]]; then
TELEGRAM_TOKEN=$(jq -r ".telegram_token" "$config")
TELEGRAM_USERID=$(jq -r ".telegram_userid" "$config")
if [[ -n "$TELEGRAM_TOKEN" && -n "$TELEGRAM_USERID" ]]; then
msg="你的域名($domain)申请的SSL证书已下发,请查收!"
msg="Host:$host, user:$user, 你的域名($domain)申请的SSL证书已下发,请查收!"
cd $installpath/serv00-play
export TELEGRAM_TOKEN="$TELEGRAM_TOKEN" TELEGRAM_USERID="$TELEGRAM_USERID"
./tgsend.sh "$msg"
Expand Down
54 changes: 49 additions & 5 deletions tgsend.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,61 @@
#!/bin/bash

message_text=$1
#解析模式,可选HTML或Markdown
MODE='HTML'
#api接口

toTGMsg() {
local msg=$1
local title="*Serv00-play通知*"
local host_icon="🖥️"
local user_icon="👤"
local time_icon=""
local notify_icon="📢"

# 获取当前时间
local current_time=$(date "+%Y-%m-%d %H:%M:%S")

if [[ "$msg" != Host:* ]]; then
local formatted_msg="${title} \n\n"
formatted_msg+="${time_icon} *时间:* ${current_time} \n"
formatted_msg+="${notify_icon} *通知内容:* \n$msg \n\n"
echo -e "$formatted_msg"
return
fi

local host=$(echo "$msg" | sed -n 's/.*Host:\([^,]*\).*/\1/p' | xargs)
local user=$(echo "$msg" | sed -n 's/.*user:\([^,]*\).*/\1/p' | xargs)
local notify_content=$(echo "$msg" | sed -E 's/.*user:[^,]*,//' | xargs)

# 格式化消息内容,Markdown 换行使用两个空格 + 换行
local formatted_msg="${title} \n\n"
formatted_msg+="${host_icon} *主机:* ${host} \n"
formatted_msg+="${user_icon} *用户:* ${user} \n"
formatted_msg+="${time_icon} *时间:* ${current_time} \n\n"
formatted_msg+="${notify_icon} *通知内容:* ${notify_content} \n\n"

echo -e "$formatted_msg" # 使用 -e 选项以确保换行符生效
}

telegramBotToken=${TELEGRAM_TOKEN}
telegramBotUserId=${TELEGRAM_USERID}

formatted_msg=$(toTGMsg "$message_text")
button_url=${BUTTON_URL:-"https://www.youtube.com/@frankiejun8965"}
URL="https://api.telegram.org/bot${telegramBotToken}/sendMessage"
reply_markup='{
"inline_keyboard": [
[
{"text": "点击查看", "url": "'"${button_url}"'"}
]
]
}'

if [[ -z ${telegramBotToken} ]]; then
echo "未配置TG推送"
else
res=$(timeout 20s curl -s -X POST $URL -d chat_id=${telegramBotUserId} -d parse_mode=${MODE} -d text="${message_text}")
res=$(curl -s -X POST "https://api.telegram.org/bot${telegramBotToken}/sendMessage" \
-d chat_id="${telegramBotUserId}" \
-d parse_mode="Markdown" \
-d text="$formatted_msg" \
-d reply_markup="$reply_markup")
if [ $? == 124 ]; then
echo 'TG_api请求超时,请检查网络是否重启完成并是否能够访问TG'
exit 1
Expand Down

0 comments on commit 8cb0a5d

Please sign in to comment.