import os, sys import logging from flask import Blueprint, request, jsonify # 프로젝트 루트 경로 등록 sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) from lib.notion_api import handle_notion_event from lib.send_message import send_message_to_mattermost from lib.config import Config webhook_bp = Blueprint('webhook', __name__) @webhook_bp.route('/webhook/notion', methods=['POST']) def notion_webhook(): try: # 📌 요청 전체 헤더 및 바디 출력 logging.info("🔔 Notion 웹훅 요청 수신") logging.info(f"Headers: {dict(request.headers)}") logging.info(f"Body: {request.get_data(as_text=True)}") # JSON 파싱 event = request.get_json() if not event: logging.warning("❗️빈 JSON 요청 수신") return jsonify({"error": "Invalid JSON"}), 400 # 노션 이벤트 처리 message = handle_notion_event(event) if not message: logging.info("📭 전송할 메시지 없음 - 무시") return jsonify({"status": "ignored"}), 200 # Mattermost 메시지 전송 success = send_message_to_mattermost(message) if success: return jsonify({"status": "ok"}), 200 else: logging.error("❌ Mattermost 메시지 전송 실패") return jsonify({"error": "Failed to send message"}), 500 except Exception as e: logging.exception("🔥 웹훅 처리 중 예외 발생") return jsonify({"error": str(e)}), 500