diff --git a/webhook/webhook.py b/webhook/webhook.py index 5eb22cb..29da419 100644 --- a/webhook/webhook.py +++ b/webhook/webhook.py @@ -4,12 +4,10 @@ import sqlite3 from datetime import datetime app = Flask(__name__) - DB_PATH = '/data/weather.sqlite' - -# 환경변수에서 DOMAIN 읽기, 없으면 기본값 지정 (로컬 테스트용) DOMAIN = os.getenv('DOMAIN', 'http://localhost:5000') + def get_rain_data(date): conn = sqlite3.connect(DB_PATH) curs = conn.cursor() @@ -24,39 +22,41 @@ 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') - response_text = "" - - 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}") - - lines.append(f"\n영업시간 내 총 강수량은 {total_rainfall:.1f}mm 입니다.") - response_text = '\n'.join(lines) - - except Exception as e: - response_text = f"데이터 조회 중 오류가 발생했습니다: {e}" - image_filename = f"weather_capture_{today}.png" image_path = f"/data/{image_filename}" + image_url = f"{DOMAIN}/data/{image_filename}" - outputs = [{ - "simpleText": { - "text": response_text - } - }] + response_text = "" - if os.path.isfile(image_path): - image_url = f"{DOMAIN}/data/{image_filename}" + if "날씨" in utterance or "강수" in utterance or "비" in utterance: + 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}") + + lines.append(f"\n영업시간 내 총 강수량은 {total_rainfall:.1f}mm 입니다.") + response_text = '\n'.join(lines) + + except Exception as e: + response_text = f"데이터 조회 중 오류가 발생했습니다: {e}" + else: + response_text = "원하시는 정보를 다시 말씀해 주세요. 예: '오늘 비 와요?', '강수량 알려줘' 등" + + outputs = [{"simpleText": {"text": response_text}}] + + if os.path.isfile(image_path) and ("날씨" in utterance or "강수" in utterance or "비" in utterance): outputs.append({ "image": { "imageUrl": image_url, @@ -71,5 +71,6 @@ def webhook(): } }) + if __name__ == '__main__': app.run(host='0.0.0.0', port=5000)