Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

向 Telegram bot 发送证书续签通知 #400

Open
leic4u opened this issue May 19, 2024 · 0 comments
Open

向 Telegram bot 发送证书续签通知 #400

leic4u opened this issue May 19, 2024 · 0 comments
Labels
duplicate This issue or pull request already exists enhancement New feature or request

Comments

@leic4u
Copy link
Contributor

leic4u commented May 19, 2024

因为 #391 这个问题,续签失败的通知只发送到面板,但我最近两个星期没有登录面板,导致我没有及时收到通知,结果签发证书被限流了,对 Let's Encrypt 的服务器也造成了一点点微不足道但可以避免的压力。

希望可以支持将续签结果通知发送到 Telegram bot 或其他如邮箱等强通知的渠道。

自己不会从头写,只会看着改,所以只能提供一段参考代码,期待实现。

发送 Telegram 通知 Shell 脚本

## 发送 Telegram 通知,$1:消息内容
notify_telegram () {
    local message="$(echo -e $1 | sed 's!&!%26!g')"
    ## local tg_api_url="https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage"
    local send_result err_code err_message
    local cmd_proxy_user cmd_proxy

## 使用 Telegram API 反代地址替代原始 API 地址
    if [ -z "$TG_API_HOST" ]; then
        local tg_api_url="https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage"
    else
        if [[ "${TG_API_HOST: -1}" != "/" ]]; then
            TG_API_HOST="${TG_API_HOST}/"
        fi
        if [[ "http://" == "${TG_API_HOST:0:7}" || "https://" == "${TG_API_HOST:0:8}" ]]; then
            local tg_api_url="${TG_API_HOST}bot${TG_BOT_TOKEN}/sendMessage"
        else
            local tg_api_url="https://${TG_API_HOST}bot${TG_BOT_TOKEN}/sendMessage"
        fi
    fi

    [[ $TG_PROXY_USER ]] && cmd_proxy_user="--proxy-user ${TG_PROXY_USER}" || cmd_proxy_user=""
    [[ $TG_PROXY_ADDRESS ]] && cmd_proxy="--proxy $TG_PROXY_ADDRESS $cmd_proxy_user" || cmd_proxy=""

    send_result=$(curl -Ssk $cmd_proxy -H "Content-Type:application/x-www-form-urlencoded" -X POST -d "chat_id=${TG_USER_ID}&text=${message}&disable_web_page_preview=true" "$tg_api_url")
    send_result=$(echo "$send_result" | sed '{:label; N; s/\n/\\n/g; b label; s/\\n$//}')
    err_code=$(echo "$send_result" | jq .ok)
    if [[ $err_code == true ]]; then
        echo -e "(N) $(date +'%Y-%m-%dT%H:%M:%S') - [$(basename $0)] 发送Telegram通知成功\n"
    else
        err_code=$(echo "$send_result" | jq .err_code)
        [[ err_code == 400 ]] && err_message="请主动给bot发送一条消息并检查接收用户ID是否正确。"
        [[ err_code == 401 ]] && err_message="Telegram Bot Token 填写错误。"
        echo -e "(W) $(date +'%Y-%m-%dT%H:%M:%S') - [$(basename $0)] 发送Telegram通知失败,错误代码:$err_code,错误消息:$err_message\n"
    fi
}
@leic4u leic4u added the enhancement New feature or request label May 19, 2024
@0xJacky 0xJacky added the duplicate This issue or pull request already exists label Jun 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants