2021-01-10 23:21:46 +01:00
|
|
|
#!/usr/bin/env sh
|
2021-01-04 06:41:02 +01:00
|
|
|
|
|
|
|
#Support Telegram Bots
|
|
|
|
|
|
|
|
#TELEGRAM_BOT_APITOKEN=""
|
|
|
|
#TELEGRAM_BOT_CHATID=""
|
|
|
|
|
|
|
|
telegram_send() {
|
|
|
|
_subject="$1"
|
|
|
|
_content="$2"
|
|
|
|
_statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
|
|
|
|
_debug "_statusCode" "$_statusCode"
|
|
|
|
|
|
|
|
TELEGRAM_BOT_APITOKEN="${TELEGRAM_BOT_APITOKEN:-$(_readaccountconf_mutable TELEGRAM_BOT_APITOKEN)}"
|
|
|
|
if [ -z "$TELEGRAM_BOT_APITOKEN" ]; then
|
|
|
|
TELEGRAM_BOT_APITOKEN=""
|
|
|
|
_err "You didn't specify a Telegram BOT API Token TELEGRAM_BOT_APITOKEN yet."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
_saveaccountconf_mutable TELEGRAM_BOT_APITOKEN "$TELEGRAM_BOT_APITOKEN"
|
|
|
|
|
|
|
|
TELEGRAM_BOT_CHATID="${TELEGRAM_BOT_CHATID:-$(_readaccountconf_mutable TELEGRAM_BOT_CHATID)}"
|
|
|
|
if [ -z "$TELEGRAM_BOT_CHATID" ]; then
|
|
|
|
TELEGRAM_BOT_CHATID=""
|
|
|
|
_err "You didn't specify a Telegram Chat id TELEGRAM_BOT_CHATID yet."
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
_saveaccountconf_mutable TELEGRAM_BOT_CHATID "$TELEGRAM_BOT_CHATID"
|
|
|
|
|
2024-10-07 04:16:11 +02:00
|
|
|
_subject="$(printf "%s" "$_subject" | sed 's/\\/\\\\\\\\/g' | sed 's/\]/\\\\\]/g' | sed 's/\([_*[()~`>#+--=|{}.!]\)/\\\\\1/g')"
|
|
|
|
_content="$(printf "%s" "$_content" | sed 's/\\/\\\\\\\\/g' | sed 's/\]/\\\\\]/g' | sed 's/\([_*[()~`>#+--=|{}.!]\)/\\\\\1/g')"
|
2021-01-04 06:41:02 +01:00
|
|
|
_content="$(printf "*%s*\n%s" "$_subject" "$_content" | _json_encode)"
|
|
|
|
_data="{\"text\": \"$_content\", "
|
|
|
|
_data="$_data\"chat_id\": \"$TELEGRAM_BOT_CHATID\", "
|
2024-10-07 04:16:11 +02:00
|
|
|
_data="$_data\"parse_mode\": \"MarkdownV2\", "
|
2021-01-04 06:41:02 +01:00
|
|
|
_data="$_data\"disable_web_page_preview\": \"1\"}"
|
|
|
|
|
2021-04-08 06:37:27 +02:00
|
|
|
_debug "$_data"
|
|
|
|
|
2021-01-04 06:41:02 +01:00
|
|
|
export _H1="Content-Type: application/json"
|
|
|
|
_telegram_bot_url="https://api.telegram.org/bot${TELEGRAM_BOT_APITOKEN}/sendMessage"
|
2021-04-08 06:44:22 +02:00
|
|
|
if _post "$_data" "$_telegram_bot_url" >/dev/null; then
|
2021-01-04 06:41:02 +01:00
|
|
|
# shellcheck disable=SC2154
|
2021-01-10 23:27:39 +01:00
|
|
|
_message=$(printf "%s\n" "$response" | sed -n 's/.*"ok":\([^,]*\).*/\1/p')
|
2021-01-04 06:41:02 +01:00
|
|
|
if [ "$_message" = "true" ]; then
|
|
|
|
_info "telegram send success."
|
|
|
|
return 0
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
_err "telegram send error."
|
|
|
|
_err "$response"
|
|
|
|
return 1
|
|
|
|
}
|