SFC O2O System

Published:

๐ŸŽฎClick here to check the Render APP!

๐Ÿ‘พClick here to check the project repository (itโ€™s open source!)

๐Ÿค– LINE Bot Smart Lending System (PoC)

Python FastAPI LINE API Google Sheets License

A Proof of Concept (PoC) system for tracking item borrowing and returning using a LINE Chatbot interface. This project is designed for an empirical experiment to validate O2O (Online-to-Offline) interactions without a complex database setup, utilizing Google Sheets for real-time data visualization and management.

Key Features

  • Scan to Borrow: Users scan a QR code on the item, which triggers a LINE message to the bot. The bot logs the transaction into Google Sheets.
  • Tap to Return: An M5Stack device (NFC/RFID) detects the returned item and calls the backend API to update the status.
  • Instant Feedback: The LINE Bot replies immediately upon borrowing and sends a Push Message upon successful return.
  • Auto-Reminder: A cron-triggered function checks Google Sheets for overdue items (e.g., > 3 days) and automatically sends Push Notifications to remind users.

System Architecture

graph TD
    classDef user fill:#f9f,stroke:#333,stroke-width:2px,color:black;
    classDef logic fill:#bbf,stroke:#333,stroke-width:2px,color:black;
    classDef data fill:#ff9,stroke:#333,stroke-width:2px,color:black;
    classDef ext fill:#ddd,stroke:#333,stroke-width:2px,color:black;
    classDef cron fill:#f66,stroke:#333,stroke-width:2px,color:white;

    subgraph "User Interaction"
        User((User)):::user
        LINE[LINE App]:::ext
        M5["M5Stack/IoT Device"]:::ext
    end

    subgraph "Logic Layer (Render/K-CR)"
        FastAPI[FastAPI Backend]:::logic
        KCR[K-CR Backend]:::logic
    end
    
    subgraph "Data Persistence"
        GSheet[("Google Sheets<br/>(PoC Only)")]:::data
        Database[("PostgreSQL DB<br/>(Production)")]:::data
    end

    subgraph "Automation"
        Cron:::cron
    end

    %% ่ฟž็บฟๅ…ณ็ณป
    User -- 1. Scan QR --> LINE
    User -- 4. Tap NFC --> M5
    
    M5 -- API Request --> KCR
    LINE -- 2. Webhook --> FastAPI
    
    %% Backend Logic
    KCR -. "5. Write Data ".-> GSheet
    FastAPI -- 3. Reply/Push --> LINE
    FastAPI -- 6. Read/Write --> GSheet
    
    %% Production Logic
    FastAPI == 1. API Request ==> Database
    FastAPI == 2. Read/Write ==> Database
    
    %% Reminder Logic
    Cron -- Daily Trigger --> FastAPI
    FastAPI -. Check Overdue .-> GSheet
    FastAPI -. Check Overdue .-> Database
    
    LINE ~~~ FastAPI
    FastAPI ~~~ Database

    linkStyle default interpolate basis
graph TD
subgraph User Interaction
User[User] -- 1. Scan QR --> LINE[LINE App]
User -- 4. Tap NFC/RFID Tag --> M5["M5Stack/Other Device(From K-CR Inc.)"]
end

subgraph "K-CR Inc. Backend"
M5 -- API Request --> K-CR[K-CR Backend]
end

subgraph "Cloud Backend (Render)"
LINE -- 2. Webhook --> FastAPI[FastAPI Backend]
K-CR -- 5. API Request --> GSheet[("Google Sheets (only for PoC)")]
FastAPI -- 3. Reply/Push --> LINE
FastAPI -- 6. Read/Write --> GSheet
end

subgraph "Database (in production environment)"
FastAPI -- 1. API Request --> Database[("PostgreSQL DB")]
FastAPI -- 2. Read/Write --> Database
end

subgraph Reminder System
Cron[UptimeRobot / Cron] -- Daily Trigger --> FastAPI
FastAPI -- Check Overdue --> GSheet
FastAPI -- Send Reminder --> LINE
FastAPI -- Check Overdue --> Database
end

Copyright (c) 2025 Kouzen Jo. Powerer by Mermaid

Tech Stack

  • Backend: Python, FastAPI, Uvicorn

  • Interface: LINE Messaging API (Chatbot)

  • Database: Google Sheets (via gspread & Google Drive API) / PostgreSQL

  • Hardware Endpoint: K-CR RFID Device / M5Stack AtomS3 + RFID Unit (C++) - Current Design

  • Deployment: Render.com / K-CR Inc.

Setup & Installation

Prerequisites

  1. LINE Official Account: Create a channel with Messaging API enabled.

  2. Google Cloud Platform:

    • Enable Google Sheets API and Google Drive API.

    • Create a Service Account and download the service_account.json key.

  3. Google Sheet: Create a sheet and share it (Editor access) with the Service Account email.

    • Header Row (Must be exact): UserIDItemIDDateStatus
  4. Local Development

Clone the repository and set up the environment:

git clone [https://github.com/YOUR_USERNAME/line-bot-lending.git](https://github.com/YOUR_USERNAME/line-bot-lending.git)
cd line-bot-lending

# Create Virtual Environment
python3 -m venv venv
source venv/bin/activate  # Mac/Linux
# .\venv\Scripts\activate  # Windows

# Install Dependencies
pip install -r requirements.txt
  1. Configuration (.env)

Create a .env file in the root directory and add your credentials:

```Ini, TOML LINE_CHANNEL_ACCESS_TOKEN=your_long_token_here LINE_CHANNEL_SECRET=your_secret_here GOOGLE_SHEET_NAME=Name_Of_Your_Google_Sheet OVERDUE_DAYS=3 Note: Place your service_account.json file in the root directory for local testing. DO NOT commit this file to GitHub.


3. Run Locally

``` Bash
uvicorn main:app --reload

Use Ngrok to expose your local server to LINE:

ngrok http 8000
  • Copy the Ngrok HTTPS URL.

  • Set LINE Webhook URL to: https://your-ngrok-url.ngrok-free.app/callback

โ˜๏ธ Deployment (Render)

  1. Create a new Web Service on Render.

  2. Connect this GitHub repository.

  3. Build Command: pip install -r requirements.txt

  4. Start Command: uvicorn main:app โ€“host 0.0.0.0 โ€“port $PORT

  5. Environment Variables:

    • Add all variables from .env.

    • Crucial Step: Create a variable named GOOGLE_JSON. Paste the entire content of your service_account.json file into the value field. The code is designed to load credentials from this variable in production.

API Endpoints

Method	Endpoint	Description
POST	/callback	The Webhook URL for LINE Messaging API.
GET	/	Health check.
GET	/api/cron/check_overdue	Trigger this manually or via a cron job service (e.g., UptimeRobot) to send reminders.

Project Structure

.
โ”œโ”€โ”€ main.py              # Main application logic (FastAPI + Bot)
โ”œโ”€โ”€ gen_qr_bot.py        # Script to generate Deep Link QR codes
โ”œโ”€โ”€ requirements.txt     # Python dependencies
โ”œโ”€โ”€ .env                 # Local secrets (Ignored by Git)
โ”œโ”€โ”€ service_account.json # Google Cloud Key (Ignored by Git)
โ””โ”€โ”€ .gitignore           # Git ignore rules

License

This project is licensed under the MIT License. Copyright (c) 2025 Kouzen Jo