8
n8n 中文网amn8n.com

聊天机器人AI

中级

这是一个Other, AI领域的自动化工作流,包含 14 个节点。主要使用 If, Set, Webhook, HttpRequest, Agent 等节点,结合人工智能技术实现智能自动化。 在LINE上构建您自己的心理咨询聊天机器人,支持心理健康对话

前置要求
  • HTTP Webhook 端点(n8n 会自动生成)
  • 可能需要目标 API 的认证凭证
  • OpenAI API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "ghfbOYrOSiQVAbl5",
  "meta": {
    "instanceId": "558d88703fb65b2d0e44613bc35916258b0f0bf983c5d4730c00c424b77ca36a"
  },
  "name": "聊天机器人 AI",
  "tags": [],
  "nodes": [
    {
      "id": "6eec6665-eea6-4aaa-8ae5-2fc7bf0c4746",
      "name": "加载动画",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -520,
        340
      ],
      "parameters": {
        "url": "https://api.line.me/v2/bot/chat/loading/start",
        "body": "={\n    \"chatId\": \"{{ $json.body.events[0].source.userId }}\",\n    \"loadingSeconds\": 60\n}",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "contentType": "raw",
        "authentication": "genericCredentialType",
        "rawContentType": "application/json",
        "genericAuthType": "httpHeaderAuth"
      },
      "credentials": {
        "httpHeaderAuth": {
          "id": "PQI3F0ibV3txKWfv",
          "name": "Talking Therapy Line@"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "72ff06e5-e1d8-47e7-be15-888ec9171c72",
      "name": "回复消息 - 不支持",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        100,
        760
      ],
      "parameters": {
        "url": "https://api.line.me/v2/bot/message/reply",
        "body": "={\n    \"replyToken\":\"{{ $('Line Chatbot').item.json.body.events[0].replyToken }}\",\n    \"messages\":[\n        {\n            \"type\":\"text\",\n            \"text\":\"Currently, the input of image or other type are not supported.\"\n        }\n    ]\n}",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "contentType": "raw",
        "sendHeaders": true,
        "rawContentType": "application/json",
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer /lQWKI4dp71pOMWZu2q18mL8P+zwf9iIOBzUDQOPMqLGMMIg88J6jPcFGfZ2ntsFfaiwCKTEcAsMjliZYXrV5E4lsjioJmv2hS7XYbh8lxmuyz1vXegKwAT66hTIBjQ1zf4l6yKugYNsUmwSYfCSQgdB04t89/1O/w1cDnyilFU="
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "3a4eb71f-033d-4aff-a4fd-2ed14ea80c6c",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        40,
        80
      ],
      "parameters": {
        "text": "={{ $('Line Chatbot').item.json.body.events[0].message.text }}",
        "options": {
          "systemMessage": "You're CBT therapist. You'll help the user find the answer to their problems using CBT. but you will not tell them that you're using CBT\n\nCBT is a talking therapy. Talking therapies are also known as psychotherapies. You can find out more about these on our information page on psychotherapies and psychological treatments.\n\nCBT helps you to learn more helpful ways of thinking and reacting in everyday situations. Changing the way you think, and what you do, can help you to feel better.\n\nUnlike some other talking therapies, CBT focuses on your current challenges rather than on your past experiences. It aims to improve your state of mind by teaching you to spot the links between your thoughts, actions and feelings.\n\nC stands for ‘cognitive’ (what you think) – In CBT, you learn to notice when you are thinking negatively. You work to challenge negative or unhelpful thoughts, for example:\n-‘I’m useless’ or\n-‘It’s all going to go wrong’.\nInstead, you work to develop more useful, realistic thoughts, for example:\n-‘What’s the evidence this is true?’\n-‘What’s another way to think about this?’ or\n-‘What advice would I give a friend in my situation?’\n\nB stands for ‘behaviour’ (what you do) – Your behaviour is what you do and how you act. CBT can help you to deal with things you have been avoiding or have fears around. When doing CBT, you might keep a daily diary of activities, and set goals to try things that you are afraid of doing. Writing down your goals and actions can give you a sense of achievement and help you to mark your progress.\n\nT stands for ‘therapy’ (what you learn) – Through CBT you learn new skills that you can then practise as ‘homework’. After you have finished receiving CBT you can continue to practise these skills, which can give you the tools to stay well in the future.\n\n\nCBT can help you to make sense of overwhelming problems by breaking them down into smaller parts. This makes it easier to see how they are connected and how they affect you. These parts are:\n\nA situation – for example, an activity, or something that happens to you that you find difficult\nFrom this can follow:\n-Thoughts\n-Emotions\n-Physical feelings\n-Actions\n\nTypes of CBT \n- Cognitive therapy : Spot unhelpful thoughts and beliefs. Keep a record and try out more useful and realistic ways of thinking and reacting.\n- Behaviour therapy (e.g., graded exposure) : Change unhelpful behaviours, like avoiding, checking, or getting reassurance. Gradually face situations, thoughts, or memories you’ve been avoiding.\n- Behavioural activation: Get more active and involved in life by doing things that give a sense of pleasure or achievement. Keep a diary and schedule in positive activities.\n- Problem-solving therapy: Identify the problem, come up with ways of solving it, pick one solution, and put it into practice.\n- Motivational interviewing: Look at the pros and cons of a habit. Set goals for change.\n- Mindfulness : Pay attention to your thoughts and surroundings in the here and now without reacting to them.\n- Compassionate mind therapy : Be kinder and less critical of yourself and others, helping you to feel safer and more content.\n- Acceptance and commitment therapy (ACT, pronounced ‘act’) : Accept unpleasant thoughts and feelings rather than fight them or get upset.\n- Dialectical behaviour therapy (DBT) :Manage strong feelings and sudden mood changes to overcome relationship difficulties. Combines one-to-one CBT with group therapy.\n- Cognitive analytic therapy : Understand past causes for current difficulties and find new ways of coping. Combines CBT with analytic therapy.\n\nReference: https://www.rcpsych.ac.uk/mental-health/treatments-and-wellbeing/cognitive-behavioural-therapy-(cbt)?spm=5aebb161.59ab0a80.0.0.3380c921WQnNWN\n\nYou'll keep the character limit under 500"
        },
        "promptType": "define"
      },
      "typeVersion": 1.7
    },
    {
      "id": "7a92aeaf-3496-410f-a6fd-4be5172b650e",
      "name": "Azure OpenAI 聊天模型",
      "type": "@n8n/n8n-nodes-langchain.lmChatAzureOpenAi",
      "position": [
        80,
        200
      ],
      "parameters": {
        "model": "4o",
        "options": {}
      },
      "credentials": {
        "azureOpenAiApi": {
          "id": "5AjoWhww5SQi2VXd",
          "name": "Azure Open AI account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "36df34d5-4232-40c8-b0ca-de7e30807adc",
      "name": "回复消息 - Line",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        920,
        80
      ],
      "parameters": {
        "url": "https://api.line.me/v2/bot/message/reply",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n  \"replyToken\": \"{{ $('Line Chatbot').item.json.body.events[0].replyToken }}\",\n  \"messages\": [\n    {\n      \"type\": \"text\",\n      \"text\": \"{{ $json.output }}\"\n    }\n  ]} ",
        "sendBody": true,
        "specifyBody": "json",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth"
      },
      "credentials": {
        "httpHeaderAuth": {
          "id": "PQI3F0ibV3txKWfv",
          "name": "Talking Therapy Line@"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "c3e227dd-3306-4259-ad7a-c1911c3c5176",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        680
      ],
      "parameters": {
        "color": 4,
        "width": 320,
        "height": 260,
        "content": "对于非文本内容,我们暂不处理,仅告知用户当前不支持"
      },
      "typeVersion": 1
    },
    {
      "id": "fba0b833-896e-4332-97e5-fa09a3838191",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1000,
        280
      ],
      "parameters": {
        "color": 4,
        "width": 340,
        "height": 560,
        "content": "**来自 Line 的 Webhook**"
      },
      "typeVersion": 1
    },
    {
      "id": "f58d7af5-70c4-412a-a8f6-6cfceaf65ade",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -640,
        280
      ],
      "parameters": {
        "color": 4,
        "width": 340,
        "height": 560,
        "content": "**Line 加载动画**"
      },
      "typeVersion": 1
    },
    {
      "id": "7c67d79d-e2b8-453c-8adc-cb66e6ef290c",
      "name": "Line 聊天机器人",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -900,
        340
      ],
      "webhookId": "c69b940a-5a44-45e3-b9b4-04abda6462b2",
      "parameters": {
        "path": "AIChatbot",
        "options": {},
        "httpMethod": "POST"
      },
      "typeVersion": 2
    },
    {
      "id": "a44319cf-d985-4bbf-be99-ac479406c369",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        820,
        0
      ],
      "parameters": {
        "color": 4,
        "width": 320,
        "height": 600,
        "content": "**回复消息**"
      },
      "typeVersion": 1
    },
    {
      "id": "1cfa159b-57c6-424a-a9e2-4b237a0bcbb5",
      "name": "检查消息类型是否为文本?",
      "type": "n8n-nodes-base.if",
      "position": [
        -220,
        340
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "e44288a5-18de-48b3-9bb1-0e18f6491043",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $('Line Chatbot').item.json.body.events[0].message.type }}",
              "rightValue": "text"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "48363222-487e-4d4a-a424-4406aacc7f74",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        400,
        0
      ],
      "parameters": {
        "color": 2,
        "width": 320,
        "height": 320,
        "content": "AI Agent 的输出格式不适合通过 JSON 发送回复,因此需要稍作编辑"
      },
      "typeVersion": 1
    },
    {
      "id": "d7b7d3ca-c685-4a02-8b73-a5b24aa663d4",
      "name": "便签5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        0
      ],
      "parameters": {
        "color": 5,
        "width": 320,
        "height": 620,
        "content": "**聊天模型 (LLM)**"
      },
      "typeVersion": 1
    },
    {
      "id": "8cb1b56a-15dd-4936-b343-c2350b2a6a48",
      "name": "格式化回复",
      "type": "n8n-nodes-base.set",
      "position": [
        500,
        80
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "15bd9ebd-ba6b-4ee5-9f4b-185260e51b0a",
              "name": "output",
              "type": "string",
              "value": "={{ $json.output.replaceAll(\"\\n\",\"\\\\n\").replaceAll(\"\\n\",\"\").removeMarkdown().removeTags().replaceAll('\"',\"\") }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "cca20e40-0b31-4e64-9953-610dc6b569d7",
  "connections": {
    "AI Agent": {
      "main": [
        [
          {
            "node": "Format Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Reply": {
      "main": [
        [
          {
            "node": "ReplyMessage - Line",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Line Chatbot": {
      "main": [
        [
          {
            "node": "Loading Animation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loading Animation": {
      "main": [
        [
          {
            "node": "Check Message Type IsText?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Azure OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Check Message Type IsText?": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "ReplyMessage - Not supported",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

中级 - 其他, 人工智能

需要付费吗?

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

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

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

外部链接
在 n8n.io 查看

分享此工作流