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.

49 lines
1.6 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. _content="$(printf "*%s*\n%s" "$_subject" "$_content" | _json_encode)"
  25. _data="{\"text\": \"$_content\", "
  26. _data="$_data\"chat_id\": \"$TELEGRAM_BOT_CHATID\", "
  27. _data="$_data\"parse_mode\": \"markdown\", "
  28. _data="$_data\"disable_web_page_preview\": \"1\"}"
  29. export _H1="Content-Type: application/json"
  30. _telegram_bot_url="https://api.telegram.org/bot${TELEGRAM_BOT_APITOKEN}/sendMessage"
  31. if _post "$_data" "$_telegram_bot_url"; then
  32. # shellcheck disable=SC2154
  33. _message=$(printf "%s\n" "$response" | sed -n 's/.*"ok":\([^,]*\).*/\1/p')
  34. if [ "$_message" = "true" ]; then
  35. _info "telegram send success."
  36. return 0
  37. fi
  38. fi
  39. _err "telegram send error."
  40. _err "$response"
  41. return 1
  42. }