From 72b5d14d4f0240bc90cb67ed4043a5874dfe4904 Mon Sep 17 00:00:00 2001 From: KWON Date: Mon, 30 Jun 2025 15:40:11 +0900 Subject: [PATCH] =?UTF-8?q?webhook=20=EC=84=9C=EB=B2=84=20=EB=94=94?= =?UTF-8?q?=EB=B2=84=EA=B9=85=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docker-compose.yml | 1 + webhook/webhook.py | 55 +++++++++++++++++++++++----------------------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index bc9cf32..409705d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -17,4 +17,5 @@ services: - 5151:5000 environment: - DOMAIN=https://webhook.firstgarden.co.kr + - FLASK_DEBUG=1 #디버그 활성화 restart: unless-stopped diff --git a/webhook/webhook.py b/webhook/webhook.py index 29da419..559c042 100644 --- a/webhook/webhook.py +++ b/webhook/webhook.py @@ -4,9 +4,13 @@ import sqlite3 from datetime import datetime app = Flask(__name__) + DB_PATH = '/data/weather.sqlite' DOMAIN = os.getenv('DOMAIN', 'http://localhost:5000') +# 환경변수 FLASK_DEBUG를 받아서 '1'일 때만 True, 아니면 False로 처리 (기본 False) +debug_env = os.getenv('FLASK_DEBUG', '0') +DEBUG_MODE = debug_env == '1' def get_rain_data(date): conn = sqlite3.connect(DB_PATH) @@ -22,41 +26,39 @@ def get_rain_data(date): conn.close() return time_rain_list, total_rainfall - @app.route('/webhook', methods=['POST']) def webhook(): - data = request.json - utterance = data.get("userRequest", {}).get("utterance", "").strip() today = datetime.today().strftime('%Y%m%d') - image_filename = f"weather_capture_{today}.png" - image_path = f"/data/{image_filename}" - image_url = f"{DOMAIN}/data/{image_filename}" - response_text = "" - if "날씨" in utterance or "강수" in utterance or "비" in utterance: - try: - time_rain_list, total_rainfall = get_rain_data(today) + try: + time_rain_list, total_rainfall = get_rain_data(today) - if not time_rain_list: - response_text = f"{today} 날짜의 강수량 데이터가 없습니다." - else: - lines = [] - for time_str, rain in time_rain_list: - rain_display = f"{rain}mm" if rain > 0 else "강수 없음" - lines.append(f"{time_str} → {rain_display}") + if not time_rain_list: + response_text = f"{today} 날짜의 강수량 데이터가 없습니다." + else: + lines = [] + for time_str, rain in time_rain_list: + rain_display = f"{rain}mm" if rain > 0 else "강수 없음" + lines.append(f"{time_str} → {rain_display}") - lines.append(f"\n영업시간 내 총 강수량은 {total_rainfall:.1f}mm 입니다.") - response_text = '\n'.join(lines) + lines.append(f"\n영업시간 내 총 강수량은 {total_rainfall:.1f}mm 입니다.") + response_text = '\n'.join(lines) - except Exception as e: - response_text = f"데이터 조회 중 오류가 발생했습니다: {e}" - else: - response_text = "원하시는 정보를 다시 말씀해 주세요. 예: '오늘 비 와요?', '강수량 알려줘' 등" + except Exception as e: + response_text = f"데이터 조회 중 오류가 발생했습니다: {e}" - outputs = [{"simpleText": {"text": response_text}}] + image_filename = f"weather_capture_{today}.png" + image_path = f"/data/{image_filename}" - if os.path.isfile(image_path) and ("날씨" in utterance or "강수" in utterance or "비" in utterance): + outputs = [{ + "simpleText": { + "text": response_text + } + }] + + if os.path.isfile(image_path): + image_url = f"{DOMAIN}/data/{image_filename}" outputs.append({ "image": { "imageUrl": image_url, @@ -71,6 +73,5 @@ def webhook(): } }) - if __name__ == '__main__': - app.run(host='0.0.0.0', port=5000) + app.run(host='0.0.0.0', port=5000, debug=DEBUG_MODE)