8
n8n 中文网amn8n.com

Slack 自动翻译器(/trans)— 日语 ⇄ 英语

中级

这是一个Internal Wiki, AI Chatbot领域的自动化工作流,包含 13 个节点。主要使用 Code, Merge, Webhook, HttpRequest, OpenAi 等节点。 使用 GPT-4o-mini 实现日语与英语间的 Slack 自动翻译

前置要求
  • HTTP Webhook 端点(n8n 会自动生成)
  • 可能需要目标 API 的认证凭证
  • OpenAI API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "jcqcULGGIS3CRxFD",
  "meta": {
    "instanceId": "f7a1112b5f8bd062516a6917a99cf95547386f2263722b462c2c44478d765429",
    "templateCredsSetupCompleted": true
  },
  "name": "Slack自动翻译器(/trans)— 日语 ⇄ 英语",
  "tags": [],
  "nodes": [
    {
      "id": "27a5c8da-5f3a-42c4-a5b3-661ba6273ae2",
      "name": "Webhook(斜杠命令)",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -48,
        -224
      ],
      "webhookId": "auto",
      "parameters": {
        "path": "slack/trans",
        "options": {},
        "httpMethod": "POST",
        "responseMode": "responseNode"
      },
      "typeVersion": 2
    },
    {
      "id": "b1f97605-e600-4cb0-bfda-fa2d46f97b04",
      "name": "响应Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "position": [
        400,
        -224
      ],
      "parameters": {
        "options": {
          "responseCode": 200,
          "responseHeaders": {
            "entries": [
              {
                "name": "Content-Type",
                "value": "text/plain; charset=utf-8"
              }
            ]
          }
        },
        "respondWith": "text",
        "responseBody": "={{$json[\"ack\"]}}"
      },
      "typeVersion": 1
    },
    {
      "id": "6a1b64d8-e2f2-480c-8587-85ae58be1fc6",
      "name": "检测语言(代码)",
      "type": "n8n-nodes-base.code",
      "position": [
        160,
        16
      ],
      "parameters": {
        "jsCode": "// --- Parse Slack payload & trim ---\n// --- Slackのペイロードを取得して整形 ---\nconst body = $json.body || $json;\nconst raw = (body.text || '').trim();\n\n// --- Early return if empty ---\n// --- 空入力はエラーメッセージを返して打ち切り ---\nif (!raw) {\n  return [{ json: {\n    text: '',\n    target: '',\n    response_url: body.response_url,\n    user_id: body.user_id,\n    channel_id: body.channel_id,\n    error: 'Please provide text after /trans.' // 入力してください\n  }}];\n}\n\n// --- Optional override: \"en: こんにちは\" / \"ja: hello\" ---\n// --- 明示ターゲット指定に対応(例: en:, ja:)---\nconst m = raw.match(/^(\\w{2}):\\s*(.*)$/);\nlet text = raw;\nlet override = null;\nif (m && (m[1] === 'en' || m[1] === 'ja')) {\n  override = m[1];\n  text = m[2];\n}\n\n// --- Heuristic JA detection ---\n// --- 日本語スクリプト判定 ---\nconst hasJa = /[\\p{Script=Hiragana}\\p{Script=Katakana}\\p{Script=Han}]/u.test(text);\n\n// --- Decide target language (override wins) ---\n// --- ターゲット言語決定(明示指定を優先)---\nconst target = override ?? (hasJa ? 'en' : 'ja');\n\nreturn [{\n  json: {\n    text,\n    target,\n    response_url: body.response_url,\n    user_id: body.user_id,\n    channel_id: body.channel_id\n  }\n}];"
      },
      "typeVersion": 2
    },
    {
      "id": "48be3dda-347d-45b0-a663-2b9c5945e433",
      "name": "OpenAI(聊天)- 翻译",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        512,
        16
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "id",
          "value": "gpt-4o-mini"
        },
        "options": {
          "temperature": 0.2
        },
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "=You are a precise translator. Translate the input ONLY into the target language.\n- Preserve punctuation and line breaks.\n- Do NOT add explanations.\n- Keep style natural and concise.\n\nTarget language: {{$json[\"target\"]}}\n\nInput:\n{{$json[\"text\"]}}"
            },
            {
              "content": "={{$json[\"text\"]}}"
            }
          ]
        }
      },
      "credentials": {
        "openAiApi": {
          "name": "OpenAi account"
        }
      },
      "typeVersion": 1.8
    },
    {
      "id": "2f4bb84e-3109-4f91-ae22-124b83e73d0c",
      "name": "准备响应",
      "type": "n8n-nodes-base.code",
      "position": [
        688,
        240
      ],
      "parameters": {
        "jsCode": "const j = $json;\n\n// --- Retrieve translated text (fallback-safe) ---\n// --- 翻訳結果を安全に取得 ---\nconst translated =\n  j.message?.content ??\n  j.choices?.[0]?.message?.content ??\n  j.data?.[0]?.content?.[0]?.text?.value ??\n  j.response ??\n  'No content';\n\n// --- Read original & user id from Detect node ---\n// --- Detectノードから原文とユーザーIDを取得 ---\nconst detect = $node[\"Detect Language (Code)\"].json;\nconst original = detect.text;\nconst userId = detect.user_id;\n\n// --- Public message format ---\n// --- 公開メッセージの整形 ---\nconst reply = `<@${userId}>\\n> ${original}\\n${translated}`;\n// const reply = `<@${userId}> ${translated}`; // ← 翻訳だけ派にしたい時はこの行に切替\n\nreturn [{ json: { reply, response_url: j.response_url } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "cdeb82fe-2702-4fbf-a2c4-3f6263c289aa",
      "name": "代码(确认)",
      "type": "n8n-nodes-base.code",
      "position": [
        160,
        -224
      ],
      "parameters": {
        "jsCode": "return [{ json: { ack: \"Translating...\" } }];"
      },
      "typeVersion": 2
    },
    {
      "id": "24c37c6e-7d41-4828-b284-aafe250b16d8",
      "name": "HTTP 请求",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        864,
        240
      ],
      "parameters": {
        "url": "={{$json[\"response_url\"]}}",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "response_type",
              "value": "in_channel"
            },
            {
              "name": "text",
              "value": "={{$json[\"reply\"]}}"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "ca69bd68-8977-4a28-8a72-c889af35b1d2",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        864,
        144
      ],
      "parameters": {
        "color": 2,
        "height": 96,
        "content": "将最终翻译发布回Slack(默认为in_channel)"
      },
      "typeVersion": 1
    },
    {
      "id": "68b6b3b8-7883-4e9b-93f8-83bfc8a7e85b",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        160,
        -80
      ],
      "parameters": {
        "color": 4,
        "height": 96,
        "content": "自动检测日语/英语。支持覆盖指令如"
      },
      "typeVersion": 1
    },
    {
      "id": "9d050866-b804-44ae-a81a-91b925ebc9fd",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        512,
        -64
      ],
      "parameters": {
        "color": 6,
        "width": 288,
        "height": 80,
        "content": "使用gpt-4o-mini模型,温度设置为0.2以获得稳定翻译"
      },
      "typeVersion": 1
    },
    {
      "id": "ec9fb181-2acd-4d17-a367-b61b75c7e180",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        160,
        -304
      ],
      "parameters": {
        "color": 5,
        "width": 208,
        "height": 80,
        "content": "发送即时\"翻译中...\"响应以避免Slack的3秒超时"
      },
      "typeVersion": 1
    },
    {
      "id": "5a2c7e05-221c-433e-bdd8-8e9fcb9504ad",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -96,
        -304
      ],
      "parameters": {
        "width": 192,
        "height": 80,
        "content": "接收来自Slack的/trans请求。在Slack中使用生产环境URL"
      },
      "typeVersion": 1
    },
    {
      "id": "553ffc33-92aa-4274-aa93-555e25ba42da",
      "name": "合并(组合响应)",
      "type": "n8n-nodes-base.merge",
      "position": [
        512,
        240
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "combineBy": "combineByPosition"
      },
      "typeVersion": 3.2
    }
  ],
  "active": true,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "203ba874-0839-4b4b-bbab-61b1409ad662",
  "connections": {
    "Code (Ack)": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Response": {
      "main": [
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Respond to Webhook": {
      "main": [
        []
      ]
    },
    "Detect Language (Code)": {
      "main": [
        [
          {
            "node": "OpenAI (Chat) - Translate",
            "type": "main",
            "index": 0
          },
          {
            "node": "Merge (Combine Response)",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Webhook (Slash Command)": {
      "main": [
        [
          {
            "node": "Detect Language (Code)",
            "type": "main",
            "index": 0
          },
          {
            "node": "Code (Ack)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge (Combine Response)": {
      "main": [
        [
          {
            "node": "Prepare Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI (Chat) - Translate": {
      "main": [
        [
          {
            "node": "Merge (Combine Response)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

中级 - 内部知识库, AI 聊天机器人

需要付费吗?

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

工作流信息
难度等级
中级
节点数量13
分类2
节点类型7
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

作者
Tomohiro Goto

Tomohiro Goto

@taoo

Creative Technologist blending design and automation to make work a little more fun. Exploring how AI and workflow tools like n8n can bring creativity into everyday work.

外部链接
在 n8n.io 查看

分享此工作流