From c67d1d7f16e746d726169c6b1f092d4578d82b0d Mon Sep 17 00:00:00 2001 From: KWON Date: Mon, 30 Jun 2025 15:42:36 +0900 Subject: [PATCH] no message --- webhook/webhook.py | 60 +++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 25 deletions(-) diff --git a/webhook/webhook.py b/webhook/webhook.py index 559c042..e9d53d4 100644 --- a/webhook/webhook.py +++ b/webhook/webhook.py @@ -8,7 +8,6 @@ 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' @@ -28,10 +27,11 @@ def get_rain_data(date): @app.route('/webhook', methods=['POST']) def webhook(): - today = datetime.today().strftime('%Y%m%d') - response_text = "" - try: + data = request.get_json(silent=True) # 비어있어도 None 반환, 예외 안남 + # 필요시 data['userRequest']['utterance'] 사용 가능 + today = datetime.today().strftime('%Y%m%d') + time_rain_list, total_rainfall = get_rain_data(today) if not time_rain_list: @@ -45,33 +45,43 @@ def webhook(): 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_filename = f"weather_capture_{today}.png" - image_path = f"/data/{image_filename}" + outputs = [{ + "simpleText": { + "text": response_text + } + }] - outputs = [{ - "simpleText": { - "text": response_text - } - }] + if os.path.isfile(image_path): + image_url = f"{DOMAIN}/data/{image_filename}" + outputs.append({ + "image": { + "imageUrl": image_url, + "altText": "오늘의 날씨 캡처 이미지" + } + }) - if os.path.isfile(image_path): - image_url = f"{DOMAIN}/data/{image_filename}" - outputs.append({ - "image": { - "imageUrl": image_url, - "altText": "오늘의 날씨 캡처 이미지" + return jsonify({ + "version": "2.0", + "template": { + "outputs": outputs } }) - return jsonify({ - "version": "2.0", - "template": { - "outputs": outputs - } - }) + except Exception as e: + # 에러 발생 시 카카오 챗봇이 받을 수 있는 기본 메시지 반환 + return jsonify({ + "version": "2.0", + "template": { + "outputs": [{ + "simpleText": { + "text": f"서버 오류가 발생했습니다: {str(e)}" + } + }] + } + }) if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=DEBUG_MODE)