From 9ebac80f35373bff54bbc3229f32e931deb9f419 Mon Sep 17 00:00:00 2001 From: Steve Dogiakos Date: Wed, 11 Mar 2026 15:30:31 -0600 Subject: [PATCH] feat: add webhook integration for new guestbook submissions Posts signup data as JSON to WEBHOOK_URL (e.g. an n8n Webhook node) in a daemon thread so it never blocks the visitor-facing response. --- app.py | 24 ++++++++++++++++++++++++ example.env | 4 +++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index c22f268..d9aa7da 100644 --- a/app.py +++ b/app.py @@ -2,6 +2,7 @@ import logging import os import re import sqlite3 +import threading from email_validator import validate_email, EmailNotValidError from flask import Flask, render_template, request, redirect, url_for, jsonify, abort @@ -173,6 +174,22 @@ def is_valid_email(email): with app.app_context(): migrate_db() +# --------------------------------------------------------------------------- +# Webhook +# --------------------------------------------------------------------------- + +def _fire_webhook(payload): + url = os.environ.get("WEBHOOK_URL", "") + if not url: + return + try: + import urllib.request, json as _json + data = _json.dumps(payload).encode() + req = urllib.request.Request(url, data=data, headers={"Content-Type": "application/json"}) + urllib.request.urlopen(req, timeout=5) + except Exception as e: + logger.warning("Webhook delivery failed: %s", e) + # --------------------------------------------------------------------------- # Public routes # --------------------------------------------------------------------------- @@ -230,6 +247,13 @@ def index(): error="Unable to save your entry. Please try again.", guests=[]) logger.info("Added guest: %s %s from %s", first_name, last_name, location) + threading.Thread(target=_fire_webhook, args=({ + "first_name": first_name, + "last_name": last_name, + "email": email, + "location": location, + "newsletter_opt_in": newsletter_opt_in, + },), daemon=True).start() return redirect(url_for('index')) try: diff --git a/example.env b/example.env index d5d6db1..29c5305 100644 --- a/example.env +++ b/example.env @@ -12,4 +12,6 @@ SITE_TITLE="The Montana Dinosaur Center Visitor Log" LOGO_URL="/static/images/logo.png" ADMIN_USER=admin ADMIN_PASSWORD=changeme -SECRET_KEY=change-this-to-a-random-secret-key \ No newline at end of file +SECRET_KEY=change-this-to-a-random-secret-key +# Optional: POST new signups as JSON to this URL (e.g. an n8n Webhook node) +WEBHOOK_URL= \ No newline at end of file