You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.2 KiB

4 years ago
  1. #!/usr/bin/env sh
  2. #Support Telegram Bots
  3. #TELEGRAM_BOT_APITOKEN=""
  4. #TELEGRAM_BOT_CHATID=""
  5. #TELEGRAM_BOT_URLBASE=""
  6. telegram_send() {
  7. _subject="$1"
  8. _content="$2"
  9. _statusCode="$3" #0: success, 1: error 2($RENEW_SKIP): skipped
  10. _debug "_statusCode" "$_statusCode"
  11. TELEGRAM_BOT_APITOKEN="${TELEGRAM_BOT_APITOKEN:-$(_readaccountconf_mutable TELEGRAM_BOT_APITOKEN)}"
  12. if [ -z "$TELEGRAM_BOT_APITOKEN" ]; then
  13. TELEGRAM_BOT_APITOKEN=""
  14. _err "You didn't specify a Telegram BOT API Token TELEGRAM_BOT_APITOKEN yet."
  15. return 1
  16. fi
  17. _saveaccountconf_mutable TELEGRAM_BOT_APITOKEN "$TELEGRAM_BOT_APITOKEN"
  18. TELEGRAM_BOT_CHATID="${TELEGRAM_BOT_CHATID:-$(_readaccountconf_mutable TELEGRAM_BOT_CHATID)}"
  19. if [ -z "$TELEGRAM_BOT_CHATID" ]; then
  20. TELEGRAM_BOT_CHATID=""
  21. _err "You didn't specify a Telegram Chat id TELEGRAM_BOT_CHATID yet."
  22. return 1
  23. fi
  24. _saveaccountconf_mutable TELEGRAM_BOT_CHATID "$TELEGRAM_BOT_CHATID"
  25. TELEGRAM_BOT_URLBASE="${TELEGRAM_BOT_URLBASE:-$(_readaccountconf_mutable TELEGRAM_BOT_URLBASE)}"
  26. if [ -z "$TELEGRAM_BOT_URLBASE" ]; then
  27. TELEGRAM_BOT_URLBASE="https://api.telegram.org"
  28. fi
  29. _saveaccountconf_mutable TELEGRAM_BOT_URLBASE "$TELEGRAM_BOT_URLBASE"
  30. _subject="$(printf "%s" "$_subject" | sed 's/\\/\\\\\\\\/g' | sed 's/\]/\\\\\]/g' | sed 's/\([_*[()~`>#+--=|{}.!]\)/\\\\\1/g')"
  31. _content="$(printf "%s" "$_content" | sed 's/\\/\\\\\\\\/g' | sed 's/\]/\\\\\]/g' | sed 's/\([_*[()~`>#+--=|{}.!]\)/\\\\\1/g')"
  32. _content="$(printf "*%s*\n%s" "$_subject" "$_content" | _json_encode)"
  33. _data="{\"text\": \"$_content\", "
  34. _data="$_data\"chat_id\": \"$TELEGRAM_BOT_CHATID\", "
  35. _data="$_data\"parse_mode\": \"MarkdownV2\", "
  36. _data="$_data\"disable_web_page_preview\": \"1\"}"
  37. _debug "$_data"
  38. export _H1="Content-Type: application/json"
  39. _telegram_bot_url="${TELEGRAM_BOT_URLBASE}/bot${TELEGRAM_BOT_APITOKEN}/sendMessage"
  40. if _post "$_data" "$_telegram_bot_url" >/dev/null; then
  41. # shellcheck disable=SC2154
  42. _message=$(printf "%s\n" "$response" | sed -n 's/.*"ok":\([^,]*\).*/\1/p')
  43. if [ "$_message" = "true" ]; then
  44. _info "telegram send success."
  45. return 0
  46. fi
  47. fi
  48. _err "telegram send error."
  49. _err "$response"
  50. return 1
  51. }