migrate from gotify to ntfy with image attachment support
- Replace GotifyNotifier with NtfyNotifier for push notifications - Add support for sending email image attachments to ntfy - Add NTFY_URL, NTFY_TOKEN, NTFY_TOPIC environment variables - Add get_mailpit_image() and get_mailpit_image_thumb() helpers - Sanitize message content for HTTP headers (remove newlines) - Remove all gotify references 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
80
app/main.py
80
app/main.py
@@ -24,8 +24,11 @@ load_dotenv()
|
||||
|
||||
MAILPIT_API = os.getenv("MAILPIT_API", "http://localhost:8025")
|
||||
MAILPIT_TOKEN = os.getenv("MAILPIT_TOKEN", "")
|
||||
GOTIFY_URL = os.getenv("GOTIFY_URL","")
|
||||
GOTIFY_TOKEN = os.getenv("GOTIFY_TOKEN","")
|
||||
|
||||
NTFY_URL = os.getenv("NTFY_URL", "")
|
||||
NTFY_TOKEN = os.getenv("NTFY_TOKEN", "")
|
||||
NTFY_TOPIC = os.getenv("NTFY_TOPIC", "")
|
||||
|
||||
LOG_LEVEL = os.getenv("LOG_LEVEL", "INFO").upper()
|
||||
|
||||
logging.basicConfig(
|
||||
@@ -36,7 +39,7 @@ log = logging.getLogger("mailpit-hook")
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
from utils.gotify_api import GotifyNotifier
|
||||
from utils.ntfy_api import NtfyNotifier
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
# Helpers
|
||||
@@ -53,16 +56,46 @@ def get_mailpit_message(message_id: str) -> Dict[str, Any]:
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
|
||||
def get_mailpit_image_thumb(message_id: str, part_id: str) -> bytes:
|
||||
"""Retrieve image thumbnail from Mailpit REST API as binary data."""
|
||||
url = f"{MAILPIT_API}/api/v1/message/{message_id}/part/{part_id}/thumb"
|
||||
headers = {}
|
||||
if MAILPIT_TOKEN:
|
||||
headers["Authorization"] = f"Bearer {MAILPIT_TOKEN}"
|
||||
|
||||
def send_gotify(title: str, message: str, priority: int = 5) -> None:
|
||||
"""Send a message to Gotify."""
|
||||
notify = GotifyNotifier(GOTIFY_URL, GOTIFY_TOKEN)
|
||||
|
||||
result = notify.gotify(title=title,markdown=message,priority=5)
|
||||
if not result:
|
||||
log.warning("Gotify push failed")
|
||||
resp = requests.get(url, headers=headers, timeout=10)
|
||||
resp.raise_for_status()
|
||||
return resp.content
|
||||
|
||||
def get_mailpit_image(message_id: str, part_id: str) -> bytes:
|
||||
"""Retrieve image thumbnail from Mailpit REST API as binary data."""
|
||||
url = f"{MAILPIT_API}/api/v1/message/{message_id}/part/{part_id}"
|
||||
headers = {}
|
||||
if MAILPIT_TOKEN:
|
||||
headers["Authorization"] = f"Bearer {MAILPIT_TOKEN}"
|
||||
|
||||
resp = requests.get(url, headers=headers, timeout=10)
|
||||
resp.raise_for_status()
|
||||
return resp.content
|
||||
|
||||
def send_notification(title: str, message: str, priority: int = 5,
|
||||
image_data: bytes = None, filename: str = "image.jpg") -> bool:
|
||||
"""Send a notification to Ntfy, optionally with an image attachment."""
|
||||
notify = NtfyNotifier(NTFY_URL, NTFY_TOKEN, NTFY_TOPIC)
|
||||
|
||||
if image_data:
|
||||
result = notify.send_with_image(
|
||||
title=title, message=message, image_data=image_data,
|
||||
filename=filename, priority=priority
|
||||
)
|
||||
else:
|
||||
log.info("Gotify push OK")
|
||||
result = notify.send(title=title, message=message, priority=priority)
|
||||
|
||||
if not result:
|
||||
log.warning("Ntfy push failed")
|
||||
else:
|
||||
log.info("Ntfy push OK")
|
||||
return result
|
||||
|
||||
|
||||
# ------------------------------------------------------------------ #
|
||||
@@ -97,16 +130,35 @@ def hook():
|
||||
return jsonify({"error":"Error sending webhook"}), 500
|
||||
|
||||
|
||||
def handle_hook(msg_id:str):
|
||||
def handle_hook(msg_id: str):
|
||||
try:
|
||||
msg = get_mailpit_message(msg_id)
|
||||
subject = msg.get("Subject", "(no subject)")
|
||||
text = msg.get("Text", "") or msg.get("HTML", "")
|
||||
attachments = msg.get("Attachments") or []
|
||||
|
||||
# Build preview text
|
||||
preview = (text or "")
|
||||
if len(preview) > 200:
|
||||
preview = preview[:200] + "..."
|
||||
|
||||
send_gotify(subject, preview)
|
||||
# Check for image attachments and fetch thumbnail
|
||||
image_data = None
|
||||
filename = "image.jpg"
|
||||
if attachments:
|
||||
first_attachment = attachments[0]
|
||||
part_id = first_attachment.get("PartID")
|
||||
content_type = first_attachment.get("ContentType", "")
|
||||
if part_id and content_type.startswith("image/"):
|
||||
try:
|
||||
# image_data = get_mailpit_image_thumb(msg_id, part_id)
|
||||
image_data = get_mailpit_image(msg_id, part_id)
|
||||
# Extract filename from attachment if available
|
||||
filename = first_attachment.get("FileName", "image.jpg")
|
||||
except Exception as e:
|
||||
log.warning("Failed to fetch image thumbnail: %s", e)
|
||||
|
||||
send_notification(subject, preview, image_data=image_data, filename=filename)
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
@@ -119,6 +171,4 @@ def handle_hook(msg_id:str):
|
||||
# ------------------------------------------------------------------ #
|
||||
|
||||
if __name__ == "__main__":
|
||||
# msg_id = "ZTUUK57e7kUoaviua6TCgP@mailpit"
|
||||
# msg = get_mailpit_message(msg_id)
|
||||
app.run(host="0.0.0.0", port=8088)
|
||||
|
||||
Reference in New Issue
Block a user