webhook 응답테스트용 파일
This commit is contained in:
@ -4,12 +4,10 @@ import sqlite3
|
|||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
|
|
||||||
DB_PATH = '/data/weather.sqlite'
|
DB_PATH = '/data/weather.sqlite'
|
||||||
|
|
||||||
# 환경변수에서 DOMAIN 읽기, 없으면 기본값 지정 (로컬 테스트용)
|
|
||||||
DOMAIN = os.getenv('DOMAIN', 'http://localhost:5000')
|
DOMAIN = os.getenv('DOMAIN', 'http://localhost:5000')
|
||||||
|
|
||||||
|
|
||||||
def get_rain_data(date):
|
def get_rain_data(date):
|
||||||
conn = sqlite3.connect(DB_PATH)
|
conn = sqlite3.connect(DB_PATH)
|
||||||
curs = conn.cursor()
|
curs = conn.cursor()
|
||||||
@ -24,39 +22,41 @@ def get_rain_data(date):
|
|||||||
conn.close()
|
conn.close()
|
||||||
return time_rain_list, total_rainfall
|
return time_rain_list, total_rainfall
|
||||||
|
|
||||||
|
|
||||||
@app.route('/webhook', methods=['POST'])
|
@app.route('/webhook', methods=['POST'])
|
||||||
def webhook():
|
def webhook():
|
||||||
|
data = request.json
|
||||||
|
utterance = data.get("userRequest", {}).get("utterance", "").strip()
|
||||||
today = datetime.today().strftime('%Y%m%d')
|
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_filename = f"weather_capture_{today}.png"
|
||||||
image_path = f"/data/{image_filename}"
|
image_path = f"/data/{image_filename}"
|
||||||
|
image_url = f"{DOMAIN}/data/{image_filename}"
|
||||||
|
|
||||||
outputs = [{
|
response_text = ""
|
||||||
"simpleText": {
|
|
||||||
"text": response_text
|
|
||||||
}
|
|
||||||
}]
|
|
||||||
|
|
||||||
if os.path.isfile(image_path):
|
if "날씨" in utterance or "강수" in utterance or "비" in utterance:
|
||||||
image_url = f"{DOMAIN}/data/{image_filename}"
|
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({
|
outputs.append({
|
||||||
"image": {
|
"image": {
|
||||||
"imageUrl": image_url,
|
"imageUrl": image_url,
|
||||||
@ -71,5 +71,6 @@ def webhook():
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(host='0.0.0.0', port=5000)
|
app.run(host='0.0.0.0', port=5000)
|
||||||
|
|||||||
Reference in New Issue
Block a user