Files
fg-auto/webhook/webhook.py
2025-06-30 15:40:11 +09:00

78 lines
2.2 KiB
Python

import os
from flask import Flask, request, jsonify
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)
curs = conn.cursor()
curs.execute('SELECT time, rainfall FROM precipitation WHERE date = ? ORDER BY time', (date,))
time_rain_list = curs.fetchall()
curs.execute('SELECT total_rainfall FROM precipitation_summary WHERE date = ?', (date,))
row = curs.fetchone()
total_rainfall = row[0] if row else 0.0
conn.close()
return time_rain_list, total_rainfall
@app.route('/webhook', methods=['POST'])
def webhook():
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}"
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": "오늘의 날씨 캡처 이미지"
}
})
return jsonify({
"version": "2.0",
"template": {
"outputs": outputs
}
})
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=DEBUG_MODE)