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.

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