노션에서 이벤트 발생 시 웹훅을 수신하고, 해당 이벤트 페이지에 대해 메시지를 보내주는 기능을 구현
This commit is contained in:
32
app/routes/webhook/notion.py
Normal file
32
app/routes/webhook/notion.py
Normal file
@ -0,0 +1,32 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from lib.notion_api import handle_notion_event
|
||||
from lib.send_message import send_message_to_mattermost
|
||||
import logging
|
||||
|
||||
notion_webhook_bp = Blueprint("notion_webhook", __name__)
|
||||
|
||||
@notion_webhook_bp.route("/webhook/notion", methods=["POST"])
|
||||
def receive_notion_webhook():
|
||||
try:
|
||||
event = request.get_json()
|
||||
if not event:
|
||||
logging.warning("빈 요청 수신됨")
|
||||
return jsonify({"error": "Invalid JSON"}), 400
|
||||
|
||||
# 메시지 생성
|
||||
message = handle_notion_event(event)
|
||||
if not message:
|
||||
logging.warning("처리 가능한 메시지 없음")
|
||||
return jsonify({"status": "ignored"}), 200
|
||||
|
||||
# Mattermost로 전송
|
||||
result = send_message_to_mattermost(message)
|
||||
if result:
|
||||
return jsonify({"status": "ok"}), 200
|
||||
else:
|
||||
logging.error("Mattermost 전송 실패")
|
||||
return jsonify({"error": "failed to send"}), 500
|
||||
|
||||
except Exception as e:
|
||||
logging.exception("노션 웹훅 처리 중 예외 발생")
|
||||
return jsonify({"error": str(e)}), 500
|
||||
44
app/views.py
Normal file
44
app/views.py
Normal file
@ -0,0 +1,44 @@
|
||||
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
|
||||
Reference in New Issue
Block a user