From c58e35c98dcfa085653bcfa19ee5752afa76780c Mon Sep 17 00:00:00 2001 From: henry4682 Date: Tue, 10 Mar 2026 10:25:52 +0800 Subject: [PATCH] feat:linebot 1. change Dockerfile 2. change the way import --- app/DB/Session.py | 2 +- app/Dockerfile | 16 ++++++---------- app/Line/Expense.py | 4 ++-- app/Line/Handlers.py | 4 ++-- app/Line/Router.py | 4 ++-- app/config.py | 21 +++++++++++++++++++++ 6 files changed, 34 insertions(+), 17 deletions(-) create mode 100644 app/config.py diff --git a/app/DB/Session.py b/app/DB/Session.py index 818a4fa..5816427 100644 --- a/app/DB/Session.py +++ b/app/DB/Session.py @@ -1,6 +1,6 @@ from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker -from app.config import DATABASE_URL +from config import DATABASE_URL engine = create_engine(DATABASE_URL) SessionLocal = sessionmaker(bind=engine) diff --git a/app/Dockerfile b/app/Dockerfile index adc6202..7ec5f55 100644 --- a/app/Dockerfile +++ b/app/Dockerfile @@ -1,9 +1,7 @@ FROM python:3.11-slim -# 安裝 uv COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv -# 安裝 Playwright 需要的系統依賴 RUN apt-get update && apt-get install -y \ wget \ gnupg \ @@ -24,17 +22,15 @@ RUN apt-get update && apt-get install -y \ WORKDIR /app -# 複製 uv 設定檔 -COPY pyproject.toml . -COPY uv.lock* . +# pyproject.toml 在 app/ 裡 +COPY app/pyproject.toml . +COPY app/uv.lock* . -# 安裝依賴 RUN uv sync --frozen - -# 安裝 Playwright Chromium RUN uv run playwright install chromium RUN uv run playwright install-deps chromium -COPY . . +# 只複製 app/ 的內容進去 +COPY app/ . -CMD ["uv", "run", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"] \ No newline at end of file +CMD ["uv", "run", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] \ No newline at end of file diff --git a/app/Line/Expense.py b/app/Line/Expense.py index 3ca7585..6b03db8 100644 --- a/app/Line/Expense.py +++ b/app/Line/Expense.py @@ -2,8 +2,8 @@ import re from datetime import datetime, date from sqlalchemy.orm import Session from sqlalchemy import text -from app.db.models import User, LineUser, Category, CategoryRule, Expense -from app.db.session import SessionLocal +from db.models import User, LineUser, Category, CategoryRule, Expense +from db.session import SessionLocal EXPENSE_TEMPLATE = ( "請填寫以下記帳資料後傳回:\n\n" diff --git a/app/Line/Handlers.py b/app/Line/Handlers.py index 75c33d8..8b6ce1d 100644 --- a/app/Line/Handlers.py +++ b/app/Line/Handlers.py @@ -1,5 +1,5 @@ -from app.line import captcha_state -from app.line.expense import ( +from line import captcha_state +from line.expense import ( EXPENSE_TEMPLATE, save_expense, delete_expense, diff --git a/app/Line/Router.py b/app/Line/Router.py index 0e00294..6166e62 100644 --- a/app/Line/Router.py +++ b/app/Line/Router.py @@ -9,8 +9,8 @@ from linebot.v3.messaging import ( ) from linebot.v3.webhooks import MessageEvent, TextMessageContent, FollowEvent from linebot.v3.exceptions import InvalidSignatureError -from app.config import LINE_CHANNEL_ACCESS_TOKEN, LINE_CHANNEL_SECRET -from app.line.handlers import handle_text, handle_captcha +from config import LINE_CHANNEL_ACCESS_TOKEN, LINE_CHANNEL_SECRET +from line.handlers import handle_text, handle_captcha router = APIRouter() configuration = Configuration(access_token=LINE_CHANNEL_ACCESS_TOKEN) diff --git a/app/config.py b/app/config.py new file mode 100644 index 0000000..43762b3 --- /dev/null +++ b/app/config.py @@ -0,0 +1,21 @@ +import os +from dotenv import load_dotenv + +load_dotenv() + +# LINE +LINE_CHANNEL_ACCESS_TOKEN = os.getenv("LINE_CHANNEL_ACCESS_TOKEN") +LINE_CHANNEL_SECRET = os.getenv("LINE_CHANNEL_SECRET") +LINE_USER_ID = os.getenv("LINE_USER_ID") + +# DB +DATABASE_URL = os.getenv("DATABASE_URL") + +# 發票 +EINVOICE_USER = os.getenv("EINVOICE_USER") +EINVOICE_PASS = os.getenv("EINVOICE_PASS") + +# Cloudinary +CLOUDINARY_CLOUD_NAME = os.getenv("CLOUDINARY_CLOUD_NAME") +CLOUDINARY_API_KEY = os.getenv("CLOUDINARY_API_KEY") +CLOUDINARY_API_SECRET = os.getenv("CLOUDINARY_API_SECRET") \ No newline at end of file