no message

This commit is contained in:
2025-06-30 15:42:36 +09:00
parent 72b5d14d4f
commit c67d1d7f16

View File

@ -8,7 +8,6 @@ app = Flask(__name__)
DB_PATH = '/data/weather.sqlite' DB_PATH = '/data/weather.sqlite'
DOMAIN = os.getenv('DOMAIN', 'http://localhost:5000') DOMAIN = os.getenv('DOMAIN', 'http://localhost:5000')
# 환경변수 FLASK_DEBUG를 받아서 '1'일 때만 True, 아니면 False로 처리 (기본 False)
debug_env = os.getenv('FLASK_DEBUG', '0') debug_env = os.getenv('FLASK_DEBUG', '0')
DEBUG_MODE = debug_env == '1' DEBUG_MODE = debug_env == '1'
@ -28,10 +27,11 @@ def get_rain_data(date):
@app.route('/webhook', methods=['POST']) @app.route('/webhook', methods=['POST'])
def webhook(): def webhook():
today = datetime.today().strftime('%Y%m%d')
response_text = ""
try: 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) time_rain_list, total_rainfall = get_rain_data(today)
if not time_rain_list: if not time_rain_list:
@ -45,33 +45,43 @@ def webhook():
lines.append(f"\n영업시간 내 총 강수량은 {total_rainfall:.1f}mm 입니다.") lines.append(f"\n영업시간 내 총 강수량은 {total_rainfall:.1f}mm 입니다.")
response_text = '\n'.join(lines) response_text = '\n'.join(lines)
except Exception as e: image_filename = f"weather_capture_{today}.png"
response_text = f"데이터 조회 중 오류가 발생했습니다: {e}" image_path = f"/data/{image_filename}"
image_filename = f"weather_capture_{today}.png" outputs = [{
image_path = f"/data/{image_filename}" "simpleText": {
"text": response_text
}
}]
outputs = [{ if os.path.isfile(image_path):
"simpleText": { image_url = f"{DOMAIN}/data/{image_filename}"
"text": response_text outputs.append({
} "image": {
}] "imageUrl": image_url,
"altText": "오늘의 날씨 캡처 이미지"
}
})
if os.path.isfile(image_path): return jsonify({
image_url = f"{DOMAIN}/data/{image_filename}" "version": "2.0",
outputs.append({ "template": {
"image": { "outputs": outputs
"imageUrl": image_url,
"altText": "오늘의 날씨 캡처 이미지"
} }
}) })
return jsonify({ except Exception as e:
"version": "2.0", # 에러 발생 시 카카오 챗봇이 받을 수 있는 기본 메시지 반환
"template": { return jsonify({
"outputs": outputs "version": "2.0",
} "template": {
}) "outputs": [{
"simpleText": {
"text": f"서버 오류가 발생했습니다: {str(e)}"
}
}]
}
})
if __name__ == '__main__': if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=DEBUG_MODE) app.run(host='0.0.0.0', port=5000, debug=DEBUG_MODE)