Skip to content

Instantly share code, notes, and snippets.

@pd12bbf7608ae1
Created December 24, 2022 08:13
Show Gist options
  • Save pd12bbf7608ae1/0d7115aba42d26fd7f5c9e195acd5d48 to your computer and use it in GitHub Desktop.
Save pd12bbf7608ae1/0d7115aba42d26fd7f5c9e195acd5d48 to your computer and use it in GitHub Desktop.
telegram_cfwork For acme.sh
#!/usr/bin/bash
#Support Telegram Bots
#Modify for CF Worker
# export TELEGRAM_CFWORK_BOT_APITOKEN TELEGRAM_CFWORK_BOT_CHATID CFWORK_ENDPOINT CFWORK_AUTHORIZATION
#TELEGRAM_CFWORK_BOT_APITOKEN=""
#TELEGRAM_CFWORK_BOT_CHATID=""
#CFWORK_ENDPOINT=""
#CFWORK_AUTHORIZATION=""
telegram_cfwork_send() {
_subject="$1"
_content="$2"
_statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
_debug "_statusCode" "$_statusCode"
TELEGRAM_CFWORK_BOT_APITOKEN="${TELEGRAM_CFWORK_BOT_APITOKEN:-$(_readaccountconf_mutable TELEGRAM_CFWORK_BOT_APITOKEN)}"
if [ -z "$TELEGRAM_CFWORK_BOT_APITOKEN" ]; then
TELEGRAM_CFWORK_BOT_APITOKEN=""
_err "You didn't specify a Telegram BOT API Token TELEGRAM_CFWORK_BOT_APITOKEN yet."
return 1
fi
_saveaccountconf_mutable TELEGRAM_CFWORK_BOT_APITOKEN "$TELEGRAM_CFWORK_BOT_APITOKEN"
TELEGRAM_CFWORK_BOT_CHATID="${TELEGRAM_CFWORK_BOT_CHATID:-$(_readaccountconf_mutable TELEGRAM_CFWORK_BOT_CHATID)}"
if [ -z "$TELEGRAM_CFWORK_BOT_CHATID" ]; then
TELEGRAM_CFWORK_BOT_CHATID=""
_err "You didn't specify a Telegram Chat id TELEGRAM_CFWORK_BOT_CHATID yet."
return 1
fi
_saveaccountconf_mutable TELEGRAM_CFWORK_BOT_CHATID "$TELEGRAM_CFWORK_BOT_CHATID"
CFWORK_ENDPOINT="${CFWORK_ENDPOINT:-$(_readaccountconf_mutable CFWORK_ENDPOINT)}"
if [ -z "$CFWORK_ENDPOINT" ]; then
CFWORK_ENDPOINT=""
_err "You didn't specify a Cloudflare Work Endpoint CFWORK_ENDPOINT yet."
return 1
fi
_saveaccountconf_mutable CFWORK_ENDPOINT "$CFWORK_ENDPOINT"
CFWORK_AUTHORIZATION="${CFWORK_AUTHORIZATION:-$(_readaccountconf_mutable CFWORK_AUTHORIZATION)}"
if [ -z "$CFWORK_AUTHORIZATION" ]; then
CFWORK_AUTHORIZATION=""
_err "You didn't specify a Cloudflare Work Endpoint Key CFWORK_AUTHORIZATION yet."
return 1
fi
_saveaccountconf_mutable CFWORK_AUTHORIZATION "$CFWORK_AUTHORIZATION"
_content="$(printf "%s" "$_content" | sed -e 's/\([_*`\[]\)/\\\\\1/g')"
_content="$(printf "*%s*\n%s" "$_subject" "$_content" | _json_encode)"
_data="{\"text\": \"$_content\", "
_data="$_data\"chat_id\": \"$TELEGRAM_CFWORK_BOT_CHATID\", "
_data="$_data\"parse_mode\": \"markdown\", "
_data="$_data\"disable_web_page_preview\": \"1\"}"
_debug "$_data"
export _H1="Content-Type: application/json"
export _H2="apihost: api.telegram.org"
export _H3="apiauthorization: ${CFWORK_AUTHORIZATION}"
_telegram_bot_url="https://${CFWORK_ENDPOINT}/bot${TELEGRAM_CFWORK_BOT_APITOKEN}/sendMessage"
if _post "$_data" "$_telegram_bot_url" >/dev/null; then
# shellcheck disable=SC2154
_message=$(printf "%s\n" "$response" | sed -n 's/.*"ok":\([^,]*\).*/\1/p')
if [ "$_message" = "true" ]; then
_info "telegram send success."
return 0
fi
fi
_err "telegram send error."
_err "$response"
return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment