1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| ---
## ⚙️ Part 2:Lenovo 一鍵建立腳本(PowerShell 版)
你可以把以下內容存成 `setup_line_order.ps1`(放在任何目錄), 然後右鍵 → **以系統管理員身分執行**。
> 💡 假設你的 MySQL root 密碼是 `rootpass`(請自行改),GitHub 已登入過。
```powershell # setup_line_order.ps1 Write-Host "=== 建立 LINE Order System Backend 專案 ===" -ForegroundColor Cyan
# 1️⃣ 建立資料夾結構 $projectDir = "D:\Projects\line-order-system" $backendDir = "$projectDir\backend" Write-Host "建立資料夾..." New-Item -ItemType Directory -Force -Path $backendDir | Out-Null New-Item -ItemType Directory -Force -Path "$backendDir\routes" | Out-Null
# 2️⃣ 初始化 npm 專案 Set-Location $backendDir npm init -y | Out-Null
# 3️⃣ 安裝套件 Write-Host "安裝 Node.js 依賴..." npm install express cors dotenv mysql2 @line/bot-sdk | Out-Null npm install -D nodemon | Out-Null
# 4️⃣ 建立主要檔案 Write-Host "建立程式檔案..."
# index.js @" import express from "express"; import cors from "cors"; import dotenv from "dotenv"; import lineRouter from "./routes/line.js";
dotenv.config();
const app = express(); app.use(cors()); app.use(express.json({ limit: "1mb" }));
app.get("/", (req, res) => res.send("Line Order Backend - OK")); app.use("/line", lineRouter);
const PORT = process.env.PORT || 3000; app.listen(PORT, () => console.log(`🚀 Server running on port ${PORT}`)); "@ | Set-Content "$backendDir\index.js"
# db.js @" import mysql from "mysql2/promise"; import dotenv from "dotenv"; dotenv.config();
export const pool = mysql.createPool({ host: process.env.DB_HOST || "localhost", user: process.env.DB_USER || "root", password: process.env.DB_PASS || "", database: process.env.DB_NAME || "line_order_db", waitForConnections: true, connectionLimit: 10, queueLimit: 0, }); "@ | Set-Content "$backendDir\db.js"
# routes/line.js @" import express from "express"; import { middleware, Client } from "@line/bot-sdk"; import dotenv from "dotenv"; import { pool } from "../db.js"; dotenv.config();
const router = express.Router();
const config = { channelAccessToken: process.env.LINE_CHANNEL_ACCESS_TOKEN, channelSecret: process.env.LINE_CHANNEL_SECRET, };
const client = new Client(config);
router.post("/webhook", middleware(config), async (req, res) => { try { const events = req.body.events || []; for (const event of events) { if (event.type === "message" && event.message.type === "text") { const userId = event.source?.userId || null; const text = event.message.text; await pool.query( "INSERT INTO messages_log (line_user_id, event_type, message_type, message_text, raw_event) VALUES (?, ?, ?, ?, ?)", [userId, event.type, event.message.type, text, JSON.stringify(event)] ); await client.replyMessage(event.replyToken, { type: "text", text: `收到:「${text}」` }); } } res.status(200).send("OK"); } catch (err) { console.error("Webhook error:", err); res.status(500).end(); } });
export default router; "@ | Set-Content "$backendDir\routes\line.js"
# db_init.sql @" CREATE DATABASE IF NOT EXISTS line_order_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci; USE line_order_db; CREATE TABLE IF NOT EXISTS users ( id INT AUTO_INCREMENT PRIMARY KEY, line_user_id VARCHAR(50) UNIQUE, display_name VARCHAR(100), phone VARCHAR(50), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); CREATE TABLE IF NOT EXISTS messages_log ( id INT AUTO_INCREMENT PRIMARY KEY, line_user_id VARCHAR(50), event_type VARCHAR(50), message_type VARCHAR(50), message_text TEXT, raw_event JSON, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); "@ | Set-Content "$backendDir\db_init.sql"
# .env.example @" LINE_CHANNEL_SECRET=your_channel_secret_here LINE_CHANNEL_ACCESS_TOKEN=your_channel_access_token_here DB_HOST=localhost DB_USER=root DB_PASS=rootpass DB_NAME=line_order_db PORT=3000 "@ | Set-Content "$backendDir\.env.example"
# package.json 追加 scripts/type (Get-Content "$backendDir\package.json") -replace '"test": "echo.*"', '"start": "node index.js", "dev": "nodemon index.js"' | Set-Content "$backendDir\package.json" Add-Content "$backendDir\package.json" "`n`"'"type": "module",'
# .gitignore @" node_modules/ .env .DS_Store "@ | Set-Content "$backendDir\.gitignore"
# 5️⃣ 建 DB Write-Host "初始化 MySQL..." & mysql -u root -prootpass -e "SOURCE $backendDir\db_init.sql"
# 6️⃣ 啟動測試 Write-Host "啟動伺服器測試..." Start-Process powershell -ArgumentList "cd $backendDir; npm run dev" Write-Host "伺服器啟動後請執行: ngrok http 3000" -ForegroundColor Yellow Write-Host "再到 LINE Developers 設定 Webhook URL -> https://xxxxx.ngrok.io/line/webhook"
|