Skip to content

Commit

Permalink
add wechat work webhook notify
Browse files Browse the repository at this point in the history
  • Loading branch information
shom committed Jan 23, 2025
1 parent 3420571 commit 1b57c5e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
80 changes: 80 additions & 0 deletions cmd/zed/zed.d/zed-functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ zed_notify()
[ "${rv}" -eq 0 ] && num_success=$((num_success + 1))
[ "${rv}" -eq 1 ] && num_failure=$((num_failure + 1))

zed_notify_wechat_work_webhook "${subject}" "${pathname}"; rv=$?
[ "${rv}" -eq 0 ] && num_success=$((num_success + 1))
[ "${rv}" -eq 1 ] && num_failure=$((num_failure + 1))


zed_notify_pushover "${subject}" "${pathname}"; rv=$?
[ "${rv}" -eq 0 ] && num_success=$((num_success + 1))
[ "${rv}" -eq 1 ] && num_failure=$((num_failure + 1))
Expand Down Expand Up @@ -457,6 +462,81 @@ zed_notify_slack_webhook()
return 0
}


# zed_notify_wechat_work_webhook (subject, pathname)
#
# Notification via Slack Webhook <https://developer.work.weixin.qq.com/document/path/91770>.
# The Webhook URL (ZED_WECHAT_WORK_WEBHOOK_URL) identifies this client to the
# Slack channel.
#
# Requires awk, curl, and sed executables to be installed in the standard PATH.
#
# References
# https://developer.work.weixin.qq.com/document/path/91770
#
# Arguments
# subject: notification subject
# pathname: pathname containing the notification message (OPTIONAL)
#
# Globals
# ZED_WECHAT_WORK_WEBHOOK_URL
#
# Return
# 0: notification sent
# 1: notification failed
# 2: not configured
#
zed_notify_wechat_work_webhook()
{
[ -n "${ZED_WECHAT_WORK_WEBHOOK_URL}" ] || return 2

local subject="$1"
local pathname="${2:-"/dev/null"}"
local msg_body
local msg_tag
local msg_json
local msg_out
local msg_err
local url="${ZED_WECHAT_WORK_WEBHOOK_URL}"

[ -n "${subject}" ] || return 1
if [ ! -r "${pathname}" ]; then
zed_log_err "wechat work webhook cannot read \"${pathname}\""
return 1
fi

zed_check_cmd "awk" "curl" "sed" || return 1

# Escape the following characters in the message body for JSON:
# newline, backslash, double quote, horizontal tab, vertical tab,
# and carriage return.
#
msg_body="$(awk '{ ORS="\\n" } { gsub(/\\/, "\\\\"); gsub(/"/, "\\\"");
gsub(/\t/, "\\t"); gsub(/\f/, "\\f"); gsub(/\r/, "\\r"); print }' \
"${pathname}")"

# Construct the JSON message for posting.
#
msg_json="$(printf '{"msgtype": "text", "text": { "content": "%s\\n%s" }}' "${subject}" "${msg_body}" )"

# Send the POST request and check for errors.
#
msg_out="$(curl -X POST "${url}" \
--header "Content-Type: application/json" --data-binary "${msg_json}" \
2>/dev/null)"; rv=$?
if [ "${rv}" -ne 0 ]; then
zed_log_err "curl exit=${rv}"
return 1
fi
msg_err="$(echo "${msg_out}" \
| sed -n -e 's/.*"error" *:.*"message" *: *"\([^"]*\)".*/\1/p')"
if [ -n "${msg_err}" ]; then
zed_log_err "wechat work webhook \"${msg_err}"\"
return 1
fi
return 0
}

# zed_notify_pushover (subject, pathname)
#
# Send a notification via Pushover <https://pushover.net/>.
Expand Down
8 changes: 8 additions & 0 deletions cmd/zed/zed.d/zed.rc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ ZED_EMAIL_ADDR="root"
#
#ZED_SLACK_WEBHOOK_URL=""

##
# Wechat Work Webhook URL.
# This allows posting to the given channel and includes an access token.
# <https://developer.work.weixin.qq.com/document/path/91770>
# Disabled by default; uncomment to enable.
#
#ZED_WECHAT_WORK_WEBHOOK_URL=""

##
# Pushover token.
# This defines the application from which the notification will be sent.
Expand Down

0 comments on commit 1b57c5e

Please sign in to comment.