Skip to content

Instantly share code, notes, and snippets.

@nczz
Last active November 15, 2025 05:31
Show Gist options
  • Select an option

  • Save nczz/803d2b0e9f265481b487684d181909f3 to your computer and use it in GitHub Desktop.

Select an option

Save nczz/803d2b0e9f265481b487684d181909f3 to your computer and use it in GitHub Desktop.
n8n 透過 Telegram 對話 OpenAI (包含外部連結取回內容分析)
{
"name": "Telegram AI Chat Bot",
"nodes": [
{
"parameters": {
"promptType": "define",
"text": "=幫我根據下方使用者提供的內容,拆分成一個純 JSON 輸出的格式,包含兩個欄位: 1. prompt: 表示一個整理過指示\n2. link: 如果該內容有附帶一個網址,將此網址整理至這欄位存放,如果沒有就空值。\n\n要注意,除了 JSON 格式的內容外,其他輸出完全不必要。\n\n然後幫我根據 prompt 的指示,選定一個 tool 工具執行。\n====== USER CONTENT START =====\n{{ $json.message.text }}\n===== USER CONTENT END =====",
"hasOutputParser": true,
"options": {}
},
"type": "@n8n/n8n-nodes-langchain.agent",
"typeVersion": 1.9,
"position": [
160,
-160
],
"id": "fbabea7d-643c-4a85-8dc4-022590c608af",
"name": "AI Agent",
"alwaysOutputData": true
},
{
"parameters": {
"model": {
"__rl": true,
"value": "gpt-4o-mini",
"mode": "list",
"cachedResultName": "gpt-4o-mini"
},
"options": {}
},
"type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
"typeVersion": 1.2,
"position": [
160,
32
],
"id": "367dffbd-9fe7-433b-a408-e38edcb938c0",
"name": "OpenAI Chat Model",
"credentials": {
"openAiApi": {
"id": "tTbbgpWR46tkgV97",
"name": "OpenAi account"
}
}
},
{
"parameters": {
"updates": [
"message"
],
"additionalFields": {
"userIds": "222745835"
}
},
"type": "n8n-nodes-base.telegramTrigger",
"typeVersion": 1.2,
"position": [
-64,
-160
],
"id": "bc3786b2-8c70-4624-b442-534710d5b7d3",
"name": "Telegram Trigger",
"webhookId": "ebefa421-bd82-4c41-b144-c6fa50f53f74",
"credentials": {
"telegramApi": {
"id": "djCdAGCSoT0YPBfC",
"name": "Telegram account"
}
}
},
{
"parameters": {
"jsonSchemaExample": "{\n\t\"prompt\": \"\",\n\t\"link\": \"\"\n}"
},
"type": "@n8n/n8n-nodes-langchain.outputParserStructured",
"typeVersion": 1.2,
"position": [
304,
32
],
"id": "97cbdbcc-ac3f-4566-b9dc-166dec270903",
"name": "Structured Output Parser"
},
{
"parameters": {
"chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
"text": "={{$json[\"message\"]}}",
"additionalFields": {
"appendAttribution": false,
"reply_to_message_id": "={{ $('Telegram Trigger').item.json.message.message_id }}"
}
},
"type": "n8n-nodes-base.telegram",
"typeVersion": 1.2,
"position": [
1824,
-48
],
"id": "b7f0dc49-a1a5-4c3a-97fa-b7cb02555215",
"name": "Telegram",
"webhookId": "26578807-805e-4ba4-bcad-5e55252fbe1e",
"credentials": {
"telegramApi": {
"id": "djCdAGCSoT0YPBfC",
"name": "Telegram account"
}
}
},
{
"parameters": {
"html": "=html {{ $json.data }}"
},
"type": "n8n-nodes-webpage-content-extractor.webpageContentExtractor",
"typeVersion": 1,
"position": [
992,
-384
],
"id": "b62b3277-b911-41f8-994e-2f107d279784",
"name": "WebpageContentExtractor"
},
{
"parameters": {
"url": "={{ $json.output.link }}",
"sendHeaders": true,
"headerParameters": {
"parameters": [
{
"name": "user-agent",
"value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36"
}
]
},
"options": {
"allowUnauthorizedCerts": true,
"timeout": 10000
}
},
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 4.2,
"position": [
352,
-576
],
"id": "db25a07f-f65b-4f85-a209-2f6e3137ff79",
"name": "HTTP Request",
"alwaysOutputData": true,
"retryOnFail": true,
"onError": "continueErrorOutput"
},
{
"parameters": {
"promptType": "define",
"text": "=1. 請根據 USER_PROMPT 的指令執行要求。\n2. 並使用 USER_CONTENT 當作前後文來搭配 USER_PROMPT 指令執行。\n3. 除了指令要求的內容外,請勿輸出其他不相關的資訊,包含以上列出來的重點前置指令。\n==== USER_PROMPT ====\n{{ $('AI Agent').item.json.output.prompt }}\n==== USER_PROMPT ====\n==== USER_CONTENT ====\n{{ $json.textContent }}\n==== USER_CONTENT ===="
},
"type": "@n8n/n8n-nodes-langchain.chainLlm",
"typeVersion": 1.6,
"position": [
1232,
-304
],
"id": "0d709f03-1b52-454b-9ea2-85f4641eb05d",
"name": "Basic LLM Chain",
"alwaysOutputData": true,
"retryOnFail": true
},
{
"parameters": {
"model": {
"__rl": true,
"value": "chatgpt-4o-latest",
"mode": "list",
"cachedResultName": "chatgpt-4o-latest"
},
"options": {}
},
"type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
"typeVersion": 1.2,
"position": [
1232,
-144
],
"id": "c941ad8a-5aae-4fa0-a432-79d9d047a835",
"name": "OpenAI Chat Model1",
"credentials": {
"openAiApi": {
"id": "tTbbgpWR46tkgV97",
"name": "OpenAi account"
}
}
},
{
"parameters": {
"promptType": "define",
"text": "=1. 請根據 USER_PROMPT 的指令執行要求。\n2. 除了指令要求的內容外,請勿輸出其他不相關的資訊,包含以上列出來的重點前置指令。\n==== USER_PROMPT ====\n{{ $('AI Agent').item.json.output.prompt }}\n==== USER_PROMPT ====\n"
},
"type": "@n8n/n8n-nodes-langchain.chainLlm",
"typeVersion": 1.6,
"position": [
784,
-48
],
"id": "886ccee6-c1af-4e24-9058-689636831b4a",
"name": "Basic LLM Chain1",
"alwaysOutputData": true,
"retryOnFail": true
},
{
"parameters": {
"model": {
"__rl": true,
"value": "chatgpt-4o-latest",
"mode": "list",
"cachedResultName": "chatgpt-4o-latest"
},
"options": {}
},
"type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
"typeVersion": 1.2,
"position": [
784,
160
],
"id": "67888c87-6653-4a4b-a531-4264e3433e9f",
"name": "OpenAI Chat Model2",
"credentials": {
"openAiApi": {
"id": "tTbbgpWR46tkgV97",
"name": "OpenAi account"
}
}
},
{
"parameters": {
"conditions": {
"options": {
"caseSensitive": true,
"leftValue": "",
"typeValidation": "strict",
"version": 2
},
"conditions": [
{
"id": "e6f9bd63-43e2-4ce5-a75f-4f5ca8752d30",
"leftValue": "={{ $json.output.link }}",
"rightValue": "",
"operator": {
"type": "string",
"operation": "notEmpty",
"singleValue": true
}
}
],
"combinator": "and"
},
"options": {}
},
"type": "n8n-nodes-base.if",
"typeVersion": 2.2,
"position": [
528,
-160
],
"id": "cf84f4c0-9290-4d4f-9acf-4a10b6658d65",
"name": "If"
},
{
"parameters": {
"sessionIdType": "customKey",
"sessionKey": "=chat_with_{{ $('Telegram Trigger').first().json.message.chat.id }}",
"contextWindowLength": 15
},
"type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
"typeVersion": 1.3,
"position": [
192,
-560
],
"id": "e2d55adf-234a-47d0-bd8e-68e9f660df9f",
"name": "Simple Memory"
},
{
"parameters": {
"chatId": "{{ $('Telegram Trigger').item.json.message.chat.id }}",
"text": "網路相關問題,無法爬取內容。",
"additionalFields": {}
},
"type": "n8n-nodes-base.telegram",
"typeVersion": 1.2,
"position": [
992,
-192
],
"id": "a328f8ad-64d1-4cd3-9f2e-307e2c97b061",
"name": "Send a text message",
"webhookId": "5f69d462-777a-423c-823f-ca1ecc6d8d4c",
"credentials": {
"telegramApi": {
"id": "djCdAGCSoT0YPBfC",
"name": "Telegram account"
}
}
},
{
"parameters": {
"requestMethod": "POST",
"url": "http://browserless:3000/chromium/content?token=6R0W53R131229",
"allowUnauthorizedCerts": true,
"responseFormat": "string",
"jsonParameters": true,
"options": {},
"bodyParametersJson": "={\n \"url\": \"{{ $json.output.link }}\",\n \"viewport\":{\n \"width\":2560,\n \"height\":1600\n },\n \"userAgent\": {\n \t\"userAgent\": \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36\"\n },\n \"gotoOptions\":\n {\n \"waitUntil\":\n [\n \"networkidle2\"\n ]\n }\n}"
},
"id": "ba449288-157c-48f8-87e1-4f55c3710d7d",
"name": "Call Browserless",
"type": "n8n-nodes-base.httpRequest",
"typeVersion": 1,
"position": [
752,
-304
],
"alwaysOutputData": true,
"retryOnFail": true,
"waitBetweenTries": 5000,
"onError": "continueErrorOutput"
},
{
"parameters": {
"jsCode": "const input = $json.message.text || \"\";\n\n// 儲存所有連結\nlet links = [];\n\n// 1️⃣ 先處理 Markdown 連結 [文字](url)\nconst mdRegex = /\\[([^\\]]+)\\]\\((https?:\\/\\/[^\\)]+)\\)/g;\nlet prompt = input.replace(mdRegex, (match, text, url) => {\n const index = links.length;\n links.push(url);\n return `${text}[${index}]`;\n});\n\n// 2️⃣ 再處理純網址\nconst urlRegex = /(https?:\\/\\/[^\\s]+)/g;\nprompt = prompt.replace(urlRegex, (match) => {\n const index = links.length;\n links.push(match);\n return `[link][${index}]`;\n});\n\n// 清理多餘空白\nprompt = prompt.replace(/\\s+/g, ' ').trim();\n\nreturn [\n {\n json: {\n prompt,\n link: links\n }\n }\n];\n"
},
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
208,
-304
],
"id": "20af1ec9-d977-4bc9-aad0-e08cd26bb5f2",
"name": "Code in JavaScript"
},
{
"parameters": {
"jsCode": "function safeSplit(text, limit = 3500) {\n const parts = [];\n let start = 0;\n\n while (start < text.length) {\n let end = start + limit;\n if (end >= text.length) {\n parts.push(text.substring(start));\n break;\n }\n\n let safeEnd = text.lastIndexOf('\\n', end);\n if (safeEnd < start) safeEnd = text.lastIndexOf(' ', end);\n if (safeEnd < start) safeEnd = end;\n\n let chunk = text.substring(start, safeEnd);\n\n // 避免落在 markdown 斷點(例如 * 沒關閉)\n const openAsterisk = (chunk.match(/\\*/g) || []).length % 2 !== 0;\n if (openAsterisk) chunk += '*';\n\n parts.push(chunk);\n start = safeEnd;\n }\n\n return parts.map(x => ({ json: { message: x } }));\n}\n\nreturn safeSplit($json.text, 3500);\n"
},
"type": "n8n-nodes-base.code",
"typeVersion": 2,
"position": [
1600,
-48
],
"id": "c0a48d67-7547-495b-bd4c-1fffcbb9f072",
"name": "split_msg"
}
],
"pinData": {},
"connections": {
"OpenAI Chat Model": {
"ai_languageModel": [
[
{
"node": "AI Agent",
"type": "ai_languageModel",
"index": 0
}
]
]
},
"Telegram Trigger": {
"main": [
[
{
"node": "AI Agent",
"type": "main",
"index": 0
},
{
"node": "Code in JavaScript",
"type": "main",
"index": 0
}
]
]
},
"AI Agent": {
"main": [
[
{
"node": "If",
"type": "main",
"index": 0
}
]
]
},
"Structured Output Parser": {
"ai_outputParser": [
[
{
"node": "AI Agent",
"type": "ai_outputParser",
"index": 0
}
]
]
},
"WebpageContentExtractor": {
"main": [
[
{
"node": "Basic LLM Chain",
"type": "main",
"index": 0
}
]
]
},
"HTTP Request": {
"main": [
[],
[]
]
},
"Basic LLM Chain": {
"main": [
[
{
"node": "split_msg",
"type": "main",
"index": 0
}
]
]
},
"OpenAI Chat Model1": {
"ai_languageModel": [
[
{
"node": "Basic LLM Chain",
"type": "ai_languageModel",
"index": 0
}
]
]
},
"OpenAI Chat Model2": {
"ai_languageModel": [
[
{
"node": "Basic LLM Chain1",
"type": "ai_languageModel",
"index": 0
}
]
]
},
"Basic LLM Chain1": {
"main": [
[
{
"node": "split_msg",
"type": "main",
"index": 0
}
]
]
},
"If": {
"main": [
[
{
"node": "Call Browserless",
"type": "main",
"index": 0
}
],
[
{
"node": "Basic LLM Chain1",
"type": "main",
"index": 0
}
]
]
},
"Simple Memory": {
"ai_memory": [
[]
]
},
"Call Browserless": {
"main": [
[
{
"node": "WebpageContentExtractor",
"type": "main",
"index": 0
}
],
[
{
"node": "Send a text message",
"type": "main",
"index": 0
}
]
]
},
"split_msg": {
"main": [
[
{
"node": "Telegram",
"type": "main",
"index": 0
}
]
]
}
},
"active": true,
"settings": {
"executionOrder": "v1"
},
"versionId": "9ff32e19-db76-4444-ab82-61bcf934b6fa",
"meta": {
"templateCredsSetupCompleted": true,
"instanceId": "8d94026d93095b7a2931bd60c3058ac77711e243683316712f67bc36a16352c1"
},
"id": "wetsRE1PeiqYj8TH",
"tags": []
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment