iT邦幫忙

2026 iThome 鐵人賽

DAY 18
0
ChatGPT & Codex

窮鬼冰箱升級計劃系列 第 18 篇

【Day 18】從即期規則到 Notification Service

  • 分享至 

  • xImage
  •  

前言

昨天提到關於不同種類型的食材,會設定合理的即期通知門檻,下列是我們列出的狀態。

NORMAL
SOON
TODAY
EXPIRED
UNKNOWN

Design API

GET /api/alerts/expiration

Data Flow

SQLite
  ↓
ExpirationService
  ↓
找到需要提醒的 InventoryItem
  ↓
NotificationService
  ↓
Email / Telegram / Discord / LINE / Meshtastic
  ↓
使用者

觸發條件

NORMAL
→ 不通知

SOON
→ 通知

TODAY
→ 通知

EXPIRED
→ 通知

UNKNOWN
→ 不通知

通知訊息建模

輸出訊息之前,會需要湊出內容,先思考哪些欄位是必須用來判斷和顯示的。

Notification
├── id
├── inventory_item_id
├── channel
├── type
├── message
├── remaining_days
├── status
├── created_at
└── sent_at

Implement

避免所有內容都塞在 routers,先產生下列檔案

app/
├── routers/
├── services/
│   ├── expiration_service.py
│   └── notification_service.py
│
└── repositories/
    └── notification_repository.py

notification_service.py

class NotificationService:

    def send_expiration_alert(
        self,
        item_name: str,
        remaining_days: int,
    ):
        message = self.build_expiration_message(
            item_name,
            remaining_days,
        )

        self.send(message)


    def build_expiration_message(
        self,
        item_name: str,
        remaining_days: int,
    ) -> str:

        if remaining_days < 0:
            return f"{item_name} 已過期"

        if remaining_days == 0:
            return f"{item_name} 今天到期"

        return (
            f"{item_name} 距到期還有 "
            f"{remaining_days} 天"
        )


    def send(self, message: str):
        raise NotImplementedError

上一篇
【Day 17】保存期限與即期提醒規則
下一篇
【Day 19】分類食材歸類
系列文
窮鬼冰箱升級計劃 共 22 篇
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言