# n8n 整合範例 ## 基本設定 ### API 端點 - **Base URL:** `http://localhost:3002/api/v1` - **Method:** `POST` - **Content-Type:** `application/json` --- ## Workflow 1: 基礎搜尋 ### Trigger: Manual / Webhook ``` [Manual Trigger] ↓ [HTTP Request] → POST http://localhost:3002/api/v1/search ↓ [Set] → 設定搜尋詞 "charade" ↓ [Code] → 處理回傳結果 ↓ [Respond] ``` ### HTTP Request 設定 ```json { "url": "http://localhost:3002/api/v1/search", "method": "POST", "body": { "query": "={{ $json.searchTerm }}", "limit": 5 }, "options": { "headers": { "Content-Type": "application/json" } } } ``` ### Code (處理結果) ```javascript const results = $input.first().json.results; const videoUrl = "https://wp.momentry.ddns.net/Old_Time_Movie_Show_-_Charade_1963.HD.mov"; return results.map(r => ({ chunk_id: r.chunk_id, text: r.text, start: r.start_time, end: r.end_time, score: r.score, video_url: `${videoUrl}#t=${r.start_time},${r.end_time}` })); ``` --- ## Workflow 2: n8n 專用格式 使用 `/n8n/search` 端點(已包含 media_url) ### HTTP Request ```json { "url": "http://localhost:3002/api/v1/n8n/search", "method": "POST", "body": { "query": "={{ $json.searchTerm }}", "limit": 5 } } ``` ### 回傳格式 ```json { "query": "charade", "count": 5, "hits": [ { "id": "sentence_0006", "vid": "a1b10138a6bbb0cd", "start": 48.8, "end": 55.44, "title": "Chunk sentence_0006", "text": "fun plot twists...", "score": 0.526, "media_url": "https://wp.momentry.ddns.net/Old_Time_Movie_Show_-_Charade_1963.HD.mov" } ] } ``` --- ## Workflow 3: 訊息機器人整合 ### Telegram Bot 範例 ``` [Webhook: Telegram] ↓ [Extract: /search charade] ↓ [HTTP Request] → POST /api/v1/search ↓ [Format Response] ↓ [Telegram: Send Message] ``` ### 回傳格式 ``` 🎬 搜尋結果: "charade" 1. "fun plot twists, Woody Dialog and charming performances..." ⏱ 48.8s - 55.4s 📊 分數: 0.526 2. "Don't you like me to say that a pretty girl..." ⏱ 4745.6s - 4748.6s 📊 分數: 0.525 ``` --- ## Workflow 4: 多影片搜尋 ### 取得所有影片 ``` [HTTP Request] GET http://localhost:3002/api/v1/videos ``` ### 依 UUID 篩選 ```json { "query": "charade", "limit": 5, "uuid": "a1b10138a6bbb0cd" } ``` --- ## Workflow 5: 定時更新 ``` [Cron: 每小時] ↓ [HTTP Request] → GET /api/v1/videos ↓ [Loop Over Items] ↓ [Check: 新影片?] ↓ [Process: 執行 vectorize] ``` --- ## 實用場景 ### 1. 客服機器人 用戶問「這部片在哪一段有談到 charade?」 → 搜尋 API → 回傳時戳 → 直接播放該片段 ### 2. 內容推薦 根據用戶輸入的關鍵字,找到相關影片片段 ### 3. 自動化剪輯 搜尋多個片段 → 組合成精華影片 --- ## 錯誤處理 ```javascript const response = $input.first(); if (!response.json.results || response.json.results.length === 0) { return { success: false, message: "找不到相關結果" }; } return { success: true, count: response.json.results.length, data: response.json.results }; ``` --- ## 測試用 cURL ```bash # 基本搜尋 curl -X POST http://localhost:3002/api/v1/search \ -H "Content-Type: application/json" \ -d '{"query": "charade", "limit": 3}' # n8n 格式 curl -X POST http://localhost:3002/api/v1/n8n/search \ -H "Content-Type: application/json" \ -d '{"query": "charade", "limit": 3}' # 取得影片列表 curl http://localhost:3002/api/v1/videos # 取得特定影片的區塊 curl http://localhost:3002/api/v1/videos/a1b10138a6bbb0cd/chunks ```