From a0bb137d5226bcb831af74078c7e23c57e653cef Mon Sep 17 00:00:00 2001 From: KWON Date: Mon, 30 Jun 2025 16:05:55 +0900 Subject: [PATCH] no message --- webhook/webhook.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/webhook/webhook.py b/webhook/webhook.py index b42c688..216e0c2 100644 --- a/webhook/webhook.py +++ b/webhook/webhook.py @@ -11,7 +11,6 @@ DOMAIN = os.getenv('DOMAIN', 'http://localhost:5000') debug_env = os.getenv('FLASK_DEBUG', '0') DEBUG_MODE = debug_env == '1' - def get_rain_data(date): conn = sqlite3.connect(DB_PATH) curs = conn.cursor() @@ -26,21 +25,19 @@ def get_rain_data(date): conn.close() return time_rain_list, total_rainfall - # 정적 파일 서빙: /data/ 경로로 이미지 접근 가능하게 함 @app.route('/data/') def serve_data_file(filename): return send_from_directory('/data', filename) - @app.route('/webhook', methods=['POST']) def webhook(): try: - data = request.get_json(silent=True) # 사용자 메시지 참고 가능 (예: data['userRequest']['utterance']) + data = request.get_json(silent=True) # 사용자 발화가 필요한 경우: data['userRequest']['utterance'] today = datetime.today().strftime('%Y%m%d') time_rain_list, total_rainfall = get_rain_data(today) - # 메시지 생성 + # 메시지 구성 if not time_rain_list: response_text = f"{today} 날짜의 강수량 데이터가 없습니다." else: @@ -51,16 +48,14 @@ def webhook(): lines.append(f"\n영업시간 내 총 강수량은 {total_rainfall:.1f}mm 입니다.") response_text = '\n'.join(lines) - # 이미지 파일 존재 확인 + # 이미지 포함 여부 확인 image_filename = f"weather_capture_{today}.png" image_path = f"/data/{image_filename}" - outputs = [{ "simpleText": { "text": response_text } }] - if os.path.isfile(image_path): image_url = f"{DOMAIN}/data/{image_filename}" outputs.append({ @@ -70,7 +65,7 @@ def webhook(): } }) - # 카카오 스킬 응답 JSON 생성 + # 응답 본문 구성 (version을 최상단에) response_body = { "version": "2.0", "template": { @@ -78,13 +73,12 @@ def webhook(): } } - # 응답 헤더 명시: 카카오가 요구하는 정확한 Content-Type + # 응답 헤더 설정 resp = make_response(jsonify(response_body)) resp.headers['Content-Type'] = 'application/json; charset=utf-8' return resp except Exception as e: - # 오류 발생 시 기본 메시지 반환 error_body = { "version": "2.0", "template": { @@ -99,6 +93,5 @@ def webhook(): resp.headers['Content-Type'] = 'application/json; charset=utf-8' return resp - if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=DEBUG_MODE)