8
n8n 中文网amn8n.com

使用Telegram、Whisper和Gemini实现Home Assistant语音与文本控制

高级

这是一个Miscellaneous, AI Chatbot, Multimodal AI领域的自动化工作流,包含 30 个节点。主要使用 If, Set, Code, Telegram, Agent 等节点。 使用Telegram、Whisper和Gemini实现Home Assistant语音与文本控制

前置要求
  • Telegram Bot Token
  • OpenAI API Key
  • HTTP Webhook 端点(n8n 会自动生成)
  • Google Gemini API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "51c6a64ef13fda2e6aa96576c25d72519ca4bc44cea9da20af60a642b594d835",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "7e0b01ef-5cf7-4e9f-9aed-8870278982de",
      "name": "Telegram 触发器",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -1392,
        -176
      ],
      "webhookId": "ef275b2b-bfed-4650-99d2-d02b3510558d",
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "id": "0sqJKu6xk6XEHAcX",
          "name": "HomeWhisper"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "827393b8-ce03-4c88-8844-ac02930ef543",
      "name": "Telegram 发送",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1328,
        -32
      ],
      "webhookId": "2923625f-1ad1-43d3-9155-ad52a144c940",
      "parameters": {
        "text": "={{ $json.text }}",
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "additionalFields": {
          "parse_mode": "HTML"
        }
      },
      "credentials": {
        "telegramApi": {
          "id": "0sqJKu6xk6XEHAcX",
          "name": "HomeWhisper"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "15b61fb1-26d0-40e1-8760-d1a458600d37",
      "name": "回复路由器",
      "type": "n8n-nodes-base.if",
      "position": [
        768,
        32
      ],
      "parameters": {
        "options": {
          "ignoreCase": false
        },
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "8e3b6656-fab8-49d5-9ed0-54bd8b6a8729",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $('Process messages').item.json.source }}",
              "rightValue": "telegram"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "cef7c350-5225-434e-b81d-27d9ede69b8e",
      "name": "Telegram 消息美化器",
      "type": "n8n-nodes-base.code",
      "position": [
        1088,
        -32
      ],
      "parameters": {
        "jsCode": "let text = $json.output;\n\n// 1. Zamień **coś** na <b>coś</b>\ntext = text.replace(/\\*\\*(.*?)\\*\\*/g, '<b>$1</b>');\n\n// 2. Zamień *   <b>Coś:</b> ... na bullet z boldem\ntext = text.replace(/^\\*\\s+<b>([^<]+):<\\/b>/gm, '• <b>$1:</b>');\n\n// 3. Zamień *   Coś: ... na bullet z boldem (gdy nie ma bolda)\ntext = text.replace(/^\\*\\s+([^:]+):/gm, '• <b>$1:</b>');\n\n// 4. Zamień `code` na <code>\ntext = text.replace(/`([^`]+)`/g, '<code>$1</code>');\n\n// 5. Zamień podwójne nowe linie na pojedyncze (żeby nie było za dużych przerw)\ntext = text.replace(/\\n{3,}/g, '\\n\\n');\n\n// 6. (Opcjonalnie) Zamień linki [tekst](url) na <a href=\"url\">tekst</a>\ntext = text.replace(/\\[([^\\]]+)\\]\\(([^)]+)\\)/g, '<a href=\"$2\">$1</a>');\n\n// 7. Chunking\nconst chunkSize = 4096;\nconst chunks = [];\nfor (let i = 0; i < text.length; i += chunkSize) {\n  chunks.push({ text: text.substring(i, i + chunkSize) });\n}\n\nreturn chunks;"
      },
      "typeVersion": 2
    },
    {
      "id": "46595cbb-4be2-4561-90b5-d1d3b5602a25",
      "name": "响应Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        1088,
        144
      ],
      "parameters": {
        "options": {},
        "respondWith": "text",
        "responseBody": "={{ $json.output }}"
      },
      "typeVersion": 1.4
    },
    {
      "id": "b729b4fc-c986-4773-bef2-f49ab4a1b895",
      "name": "转录转聊天输入",
      "type": "n8n-nodes-base.set",
      "position": [
        -256,
        -176
      ],
      "parameters": {
        "fields": {
          "values": [
            {
              "name": "chatInput",
              "stringValue": "={{ $json.text }}"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "154315db-df92-449d-9930-94139e8012e9",
      "name": "获取语音文件",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -544,
        -176
      ],
      "webhookId": "04226bd9-ad35-4792-993d-5ec2eecb519e",
      "parameters": {
        "fileId": "={{ $json.message.voice.file_id }}",
        "resource": "file",
        "additionalFields": {}
      },
      "credentials": {
        "telegramApi": {
          "id": "0sqJKu6xk6XEHAcX",
          "name": "HomeWhisper"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "0748b3dd-b6fc-4a12-b433-a7d09c65cc7f",
      "name": "语音或文本",
      "type": "n8n-nodes-base.set",
      "position": [
        -992,
        -64
      ],
      "parameters": {
        "fields": {
          "values": [
            {
              "name": "text",
              "stringValue": "={{ $json?.message?.text || \"\" }}"
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "d99898a9-6619-422d-928c-d371812c1f5e",
      "name": "语音转文本",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        -400,
        -176
      ],
      "parameters": {
        "options": {},
        "resource": "audio",
        "operation": "transcribe"
      },
      "credentials": {
        "openAiApi": {
          "id": "7CgAvVK5FzY7W0vB",
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "acfc8962-6ae2-4ddb-9429-2bfc50e04eb2",
      "name": "如果",
      "type": "n8n-nodes-base.if",
      "position": [
        -832,
        -64
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "a0bf9719-4272-46f6-ab3b-eda6f7b44fd8",
              "operator": {
                "type": "string",
                "operation": "empty",
                "singleValue": true
              },
              "leftValue": "={{ $json.message.text }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "fafce53a-2570-4ae9-b92c-029485cf3890",
      "name": "处理消息",
      "type": "n8n-nodes-base.code",
      "position": [
        16,
        32
      ],
      "parameters": {
        "jsCode": "// Helper: bezpieczne czytanie z innych nodów\nfunction safe(getter, fallback = undefined) {\n  try {\n    const v = getter();\n    return v === undefined || v === null ? fallback : v;\n  } catch (e) {\n    return fallback;\n  }\n}\n\n// Spróbuj pobrać oba źródła, ale bez crasha:\nconst tgMsg   = safe(() => $('Telegram Trigger').item.json.message, null);\nconst chatEvt = safe(() => $('When chat message received').item.json, null);\n\n// Pick-first\nconst pick = (...arr) => arr.find(v => typeof v === 'string' && v.trim()) || '';\n\n// Tekst z różnych miejsc (STT -> chatInput, tekst, itd.)\nconst inputText = pick(\n  $json.inputText,\n  $json.chatInput,\n  $json.text,\n  $json.message?.text,\n  tgMsg?.text,\n  chatEvt?.message,\n  chatEvt?.input,\n  chatEvt?.text\n);\n\n// Źródło\nlet source = 'chat';\nif (tgMsg) source = 'telegram';\nelse if (chatEvt) source = 'n8n-chat';\n\n// SessionId\nlet sessionId = 'unknown';\nif (tgMsg) {\n  sessionId = String(tgMsg.chat?.id ?? tgMsg.from?.id ?? 'telegram-unknown');\n} else if (chatEvt) {\n  sessionId = String(\n    chatEvt.sessionId ?? chatEvt.chatId ?? chatEvt.connectionId ?? chatEvt.userId ?? 'n8n-chat-unknown'\n  );\n} else if ($json.chatId) {\n  sessionId = String($json.chatId);\n}\n\n// Voice flag (na przyszłość)\nconst isVoice = Boolean(safe(() => $('Telegram Trigger').item.json.message.voice.file_id, null));\n\nreturn [{\n  json: {\n    inputText: inputText.trim(),\n    source,\n    sessionId,\n    isVoice\n  }\n}];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "55de43ac-f154-4025-9f31-78b8466a497f",
      "name": "机器人正在输入",
      "type": "n8n-nodes-base.telegram",
      "position": [
        -992,
        -256
      ],
      "webhookId": "6b14a2b6-91ff-45a5-af70-bd9696961512",
      "parameters": {
        "chatId": "={{ $('Telegram Trigger').item.json.message.chat.id }}",
        "operation": "sendChatAction"
      },
      "credentials": {
        "telegramApi": {
          "id": "0sqJKu6xk6XEHAcX",
          "name": "HomeWhisper"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "58b26df3-00e6-48c6-bd1a-209dd6c63ec3",
      "name": "简单记忆1",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        -1312,
        480
      ],
      "parameters": {},
      "typeVersion": 1.3
    },
    {
      "id": "fa7b3918-2f19-4b32-896b-83fb8ea2b400",
      "name": "Home Assistant 连接器",
      "type": "@n8n/n8n-nodes-langchain.mcpClientTool",
      "position": [
        512,
        272
      ],
      "parameters": {
        "include": "selected",
        "options": {},
        "endpointUrl": "http://192.168.1.69:8123/mcp_server/sse",
        "includeTools": [
          "GetLiveContext",
          "HassTurnOn",
          "HassTurnOff",
          "HassLightSet",
          "HassBroadcast"
        ],
        "authentication": "bearerAuth"
      },
      "credentials": {
        "httpBearerAuth": {
          "id": "jU7PgSSiWQjSlEp7",
          "name": "HA MCP"
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "b7a623d6-f689-4687-bc66-275b1f73a63a",
      "name": "家庭助手",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        304,
        32
      ],
      "parameters": {
        "text": "={{ $json.inputText }}",
        "options": {},
        "promptType": "define"
      },
      "typeVersion": 2
    },
    {
      "id": "60021ab2-33cb-46e1-87e0-3d7f9d1e56fe",
      "name": "简单记忆",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        368,
        272
      ],
      "parameters": {},
      "typeVersion": 1.3
    },
    {
      "id": "c51dc09b-ad53-4a1d-8b38-59a950d9d31c",
      "name": "Google Gemini 聊天模型",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        224,
        272
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "googlePalmApi": {
          "id": "1BSwX5Mk7OVEOxL1",
          "name": "Google Gemini(PaLM) Api account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "8c61f905-4040-44ff-aed9-3b36ecbc6daf",
      "name": "当收到聊天消息时",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -1312,
        272
      ],
      "webhookId": "9521c056-a097-4f48-a0eb-7caccf132a4c",
      "parameters": {
        "public": true,
        "options": {
          "loadPreviousSession": "memory"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "4117eb44-33aa-4d3c-b032-4de7a50f5b15",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1632,
        208
      ],
      "parameters": {
        "width": 256,
        "height": 224,
        "content": "## 当聊天消息收到时:"
      },
      "typeVersion": 1
    },
    {
      "id": "d1904d79-7004-445e-bc8e-ef056163a464",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1696,
        -208
      ],
      "parameters": {
        "content": "## Telegram 触发器:"
      },
      "typeVersion": 1
    },
    {
      "id": "fb31fb1c-c1ae-49fa-abb4-f239ed349706",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1056,
        -432
      ],
      "parameters": {
        "content": "## 机器人正在输入:"
      },
      "typeVersion": 1
    },
    {
      "id": "90ddab99-91db-419f-8190-7d5bfd74fdb9",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1040,
        80
      ],
      "parameters": {
        "color": 4,
        "width": 400,
        "content": "## 语音或文本:"
      },
      "typeVersion": 1
    },
    {
      "id": "b5f91665-8eac-4ded-80cc-3bad4444d972",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -576,
        -448
      ],
      "parameters": {
        "color": 4,
        "width": 448,
        "height": 240,
        "content": "### 获取语音文件:"
      },
      "typeVersion": 1
    },
    {
      "id": "d93fb3d9-06d2-43eb-8c46-0c133ab636c3",
      "name": "便签5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -32,
        -160
      ],
      "parameters": {
        "color": 4,
        "content": "### 处理消息:"
      },
      "typeVersion": 1
    },
    {
      "id": "d326f85d-61a1-4f29-8f52-207313b79a3a",
      "name": "### 需要帮助?",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1040,
        -208
      ],
      "parameters": {
        "content": "### Telegram 消息美化器:"
      },
      "typeVersion": 1
    },
    {
      "id": "dfb1166c-9e3f-4b58-8cbf-021ab731c0c0",
      "name": "## 试试看!",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        288,
        -160
      ],
      "parameters": {
        "content": "## 家庭助手:"
      },
      "typeVersion": 1
    },
    {
      "id": "c9f34a9e-b13b-43da-a45d-472b4ffb8a7a",
      "name": "GET 模型",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        464,
        416
      ],
      "parameters": {
        "width": 208,
        "height": 192,
        "content": "## Home Assistant 连接器:"
      },
      "typeVersion": 1
    },
    {
      "id": "6f6e3028-9886-40dd-a17a-e82aa94f9747",
      "name": "## 1. 创建新的自定义 OpenAI 凭据",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        688,
        -160
      ],
      "parameters": {
        "content": "## 回复路由器:"
      },
      "typeVersion": 1
    },
    {
      "id": "8e5c9b6c-8ad1-494e-a754-115f830d27bd",
      "name": "便签10",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1312,
        160
      ],
      "parameters": {
        "width": 272,
        "height": 240,
        "content": "### Telegram 发送:"
      },
      "typeVersion": 1
    },
    {
      "id": "5f8b5ae2-d736-40c0-9db6-daef3256f894",
      "name": "便签 11",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1792,
        -752
      ],
      "parameters": {
        "color": 3,
        "width": 592,
        "height": 448,
        "content": "**工作流名称:** Home Assistant Whisper"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "If": {
      "main": [
        [
          {
            "node": "Get Voice File",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Process messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Home Agent": {
      "main": [
        [
          {
            "node": "Reply Router",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Reply Router": {
      "main": [
        [
          {
            "node": "Telegram Message Beautifier",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory": {
      "ai_memory": [
        [
          {
            "node": "Home Agent",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Voice or Text": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Voice File": {
      "main": [
        [
          {
            "node": "Speech to Text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory1": {
      "ai_memory": [
        [
          {
            "node": "When chat message received",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Speech to Text": {
      "main": [
        [
          {
            "node": "Transcription to ChatInput",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process messages": {
      "main": [
        [
          {
            "node": "Home Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Voice or Text",
            "type": "main",
            "index": 0
          },
          {
            "node": "Bot Is typing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Home Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Home assistant Connector": {
      "ai_tool": [
        [
          {
            "node": "Home Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Transcription to ChatInput": {
      "main": [
        [
          {
            "node": "Process messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "Process messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Message Beautifier": {
      "main": [
        [
          {
            "node": "Telegram Send",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

高级 - 杂项, AI 聊天机器人, 多模态 AI

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
高级
节点数量30
分类3
节点类型13
难度说明

适合高级用户,包含 16+ 个节点的复杂工作流

外部链接
在 n8n.io 查看

分享此工作流