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.

53 lines
1.9 KiB

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