使用 Telegram 机器人和 AI 日志分析监控管理 Docker 容器

高级

这是一个DevOps、AI Chatbot领域的自动化工作流,包含 31 个节点。主要使用 If、Ssh、Code、Merge、Switch 等节点。 使用 Telegram 机器人和 AI 日志分析监控管理 Docker 容器

前置要求
  • HTTP Webhook 端点(n8n 会自动生成)
  • Telegram Bot Token
  • OpenAI API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "7d85123b3a1d956ac2c27d1a99710fba4936b72780f7dd11e84a21b5ef2fadcb",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "b18c85dc-9e56-42ad-b775-ef8da3b93f0a",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -336,
        -848
      ],
      "parameters": {
        "path": "c851e840-457c-4050-8803-4e6d258580e1",
        "options": {},
        "httpMethod": "POST"
      },
      "typeVersion": 2.1
    },
    {
      "id": "487f642b-5e4f-4499-b3da-b5d50c4e1494",
      "name": "向模型发送消息",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        1312,
        -368
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini",
          "cachedResultName": "GPT-4.1-MINI"
        },
        "options": {},
        "responses": {
          "values": [
            {
              "role": "system",
              "content": "You are a senior IT specialist experienced in debugging distributed systems, containers, Linux services, and application logs. When the user provides log output or an error message, you must:\n\nAnalyze the logs carefully and respond in a structured format:\n\nSummary\nShort, concise explanation of what's happening.\n\nMost Likely Root Cause\nOne clear sentence. If unknown, say so and state what’s missing.\n\nImpact\nExplain if functionality is broken, partially limited, or unaffected.\n\nKey Evidence From Logs\nList only the relevant log lines and briefly explain each.\n\nSeverity Level\nRate 1–5 (1 negligible, 5 critical). Include a one-line justification.\n\nRecommended Next Steps\nBullet actionable steps. Include different paths if needed (example: “If you use USB devices…”).\n\nFollow-up / What to Monitor\nOne or two items the user should watch for or collect next.\n\nGuidelines:\n\nBe concise and structured. No long essays.\n\nNever just quote logs back. Interpret them.\n\nIf assumptions are required, state them explicitly.\n\nIf the logs do not contain enough info, say so and request specifics.\n\nTone should be calm, confident, and clear like a senior engineer helping a teammate.\n\nAvoid unnecessary technical jargon unless required.\n\nFocus on actionable clarity."
            },
            {
              "content": "={{ $('get logs').item.json.stdout }}"
            }
          ]
        },
        "builtInTools": {}
      },
      "typeVersion": 2
    },
    {
      "id": "9bf441ad-c65f-496f-b5cb-8f75f35a6169",
      "name": "Telegram 触发器",
      "type": "n8n-nodes-base.telegramTrigger",
      "position": [
        -304,
        -336
      ],
      "parameters": {
        "updates": [
          "message"
        ],
        "additionalFields": {}
      },
      "typeVersion": 1.2
    },
    {
      "id": "45602d61-92d9-4df1-95e5-4482b218eb56",
      "name": "Switch",
      "type": "n8n-nodes-base.switch",
      "position": [
        16,
        80
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "a4f85833-5791-4513-bef5-f31c582bf26a",
                    "operator": {
                      "type": "string",
                      "operation": "contains"
                    },
                    "leftValue": "={{ $json.message.text.toLowerCase() }}",
                    "rightValue": "logs"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "52a99c27-c2bb-49b5-9ca9-27c3de1aa9ac",
                    "operator": {
                      "type": "string",
                      "operation": "contains"
                    },
                    "leftValue": "={{ $json.message.text.toLowerCase() }}",
                    "rightValue": "restart"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "f12f78c4-e959-4e50-9b1d-2a605169387d",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.message.text.toLowerCase() }}",
                    "rightValue": "status"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "3e13d044-3bfe-4669-a107-ace1b573240e",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.message.text.toLowerCase() }}",
                    "rightValue": "update"
                  }
                ]
              }
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.3
    },
    {
      "id": "c407c82e-763f-4007-9ebf-a42831e550b1",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "position": [
        640,
        -368
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "combineBy": "combineByPosition"
      },
      "typeVersion": 3.2
    },
    {
      "id": "da335286-a8fc-4ede-9ab6-f2760e74bb9a",
      "name": "Merge1",
      "type": "n8n-nodes-base.merge",
      "position": [
        640,
        0
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "combineBy": "combineByPosition"
      },
      "typeVersion": 3.2
    },
    {
      "id": "2a535b9d-11d6-439c-a440-f0f90bc2fc86",
      "name": "Switch1",
      "type": "n8n-nodes-base.switch",
      "position": [
        -112,
        -848
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "5f956f73-4789-4fae-bedc-7c1f0edfc2d2",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.body.heartbeat.msg }}",
                    "rightValue": "running"
                  }
                ]
              }
            },
            {
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "d1ec668d-53e8-44b2-9cb6-6a4de3bcec07",
                    "operator": {
                      "type": "string",
                      "operation": "notEquals"
                    },
                    "leftValue": "={{ $json.body.heartbeat.msg }}",
                    "rightValue": "running"
                  }
                ]
              }
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.3
    },
    {
      "id": "32eac2eb-7cf9-4d90-a719-7bafa7a4537a",
      "name": "If",
      "type": "n8n-nodes-base.if",
      "position": [
        1376,
        0
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "30a53424-65b9-4a9b-a4cd-e78933c8441b",
              "operator": {
                "type": "string",
                "operation": "empty",
                "singleValue": true
              },
              "leftValue": "={{ $json.stderr }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "13342b76-df0b-4810-bec7-e8ac0926d038",
      "name": "正常消息",
      "type": "n8n-nodes-base.telegram",
      "position": [
        112,
        -944
      ],
      "parameters": {
        "text": "={{ $json.body.msg }}",
        "chatId": "8209283663",
        "replyMarkup": "replyKeyboard",
        "additionalFields": {
          "appendAttribution": false,
          "reply_to_message_id": 0
        },
        "replyKeyboardOptions": {}
      },
      "typeVersion": 1.2
    },
    {
      "id": "08e46910-e286-424c-9877-c50d2b966044",
      "name": "错误消息",
      "type": "n8n-nodes-base.telegram",
      "position": [
        112,
        -752
      ],
      "parameters": {
        "text": "={{ $json.body.monitor.docker_container }} reported an issue : {{ $json.body.heartbeat.msg }} at {{ $json.body.heartbeat.time }}",
        "chatId": "8209283663",
        "replyMarkup": "replyKeyboard",
        "replyKeyboard": {
          "rows": [
            {
              "row": {
                "buttons": [
                  {
                    "text": "={{ $json.body.monitor.docker_container }} Logs",
                    "additionalFields": {}
                  },
                  {
                    "text": "={{ $json.body.monitor.docker_container }} Restart",
                    "additionalFields": {}
                  }
                ]
              }
            }
          ]
        },
        "additionalFields": {
          "appendAttribution": false,
          "reply_to_message_id": 0
        },
        "replyKeyboardOptions": {}
      },
      "typeVersion": 1.2
    },
    {
      "id": "b66aa8c3-71e0-4c1d-973a-c3aeee7c9b04",
      "name": "状态更新",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1088,
        -368
      ],
      "parameters": {
        "text": "=Analyzing Log File...",
        "chatId": "8209283663",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "c23aa34e-a6be-47f8-a9c0-f42c40cb5b29",
      "name": "日志分析",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1664,
        -368
      ],
      "parameters": {
        "text": "={{ $json.output[0].content[0].text }}",
        "chatId": "8209283663",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "28dc5172-8521-4ebe-932b-ba7a27d594fe",
      "name": "重启消息",
      "type": "n8n-nodes-base.telegram",
      "position": [
        864,
        0
      ],
      "parameters": {
        "text": "=Attempting to restart...",
        "chatId": "8209283663",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "ccacd643-662d-42b4-a298-50670aed45dd",
      "name": "重启成功",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1664,
        -96
      ],
      "parameters": {
        "text": "=Successfull restarting {{ $json.stdout }}",
        "chatId": "8209283663",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "07aa5b92-a6f3-4d3f-8805-0ef9bee295cc",
      "name": "重启失败",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1664,
        96
      ],
      "parameters": {
        "text": "=Restart failed\n{{ $('restart container').item.json.stderr }}",
        "chatId": "8209283663",
        "additionalFields": {
          "appendAttribution": false
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "88514140-3fbf-4644-a0f1-baf092ebe300",
      "name": "Docker 状态",
      "type": "n8n-nodes-base.telegram",
      "position": [
        672,
        448
      ],
      "parameters": {
        "text": "={{ $json.stdout }}",
        "chatId": "8209283663",
        "additionalFields": {
          "parse_mode": "HTML",
          "appendAttribution": false
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "3da786e4-4591-44f9-8389-82da4ac53932",
      "name": "获取日志",
      "type": "n8n-nodes-base.ssh",
      "position": [
        864,
        -368
      ],
      "parameters": {
        "cwd": "",
        "command": "=docker logs --tail 100 {{ $json.service_name }}"
      },
      "typeVersion": 1
    },
    {
      "id": "b91ccb85-58cc-457e-8cbe-b2a2bf2856ec",
      "name": "重启容器",
      "type": "n8n-nodes-base.ssh",
      "position": [
        1088,
        0
      ],
      "parameters": {
        "cwd": "",
        "command": "=docker restart {{ $('Merge1').item.json.service_name }}"
      },
      "typeVersion": 1
    },
    {
      "id": "ca38de10-1c2b-4e0c-ac7d-5c0fcf9ba534",
      "name": "docker ps",
      "type": "n8n-nodes-base.ssh",
      "position": [
        448,
        448
      ],
      "parameters": {
        "cwd": "",
        "command": "docker ps --format \"{{.Names}}\\t{{.Status}}\""
      },
      "typeVersion": 1
    },
    {
      "id": "7c674372-8f06-4fc5-98f4-1131a56d55ad",
      "name": "Python 代码(测试版)",
      "type": "n8n-nodes-base.code",
      "position": [
        848,
        848
      ],
      "parameters": {
        "language": "python",
        "pythonCode": "import re\nimport json\n\ndef extract_update_summary(stdout):\n    match = re.search(r'Update Summary:\\n({.*?\\})', stdout, re.DOTALL)\n    if not match:\n        return {}\n    update_json = match.group(1)\n    update_json_clean = re.sub(r',\\s*([\\]\\}])', r'\\1', update_json)\n    return json.loads(update_json_clean)\n\ndef format_update_message(update_summary):\n    status_parts = []\n    for compose_file, updates in update_summary.items():\n        service = compose_file.replace('-compose.yaml', '')\n        if updates == [\"none\"]:\n            status_parts.append(f\"{service}: No updates\")\n        else:\n            status_parts.append(f\"{service}: Updated ({', '.join(updates)})\")\n    return \"; \".join(status_parts)\n\n# n8n passes items as a list of dicts; get the \"stdout\" field from the input\nstdout = _input.first().json.stdout\nupdate_summary = extract_update_summary(stdout)\nmessage = format_update_message(update_summary)\nreturn [{\"message\": message}]\n"
      },
      "typeVersion": 2
    },
    {
      "id": "18e00083-2c70-48ca-adec-d6883456cb3f",
      "name": "更新 Docker",
      "type": "n8n-nodes-base.ssh",
      "position": [
        624,
        848
      ],
      "parameters": {
        "cwd": "",
        "command": "./update-all-docker-compose.sh"
      },
      "notesInFlow": true,
      "typeVersion": 1,
      "alwaysOutputData": true
    },
    {
      "id": "28fa6554-6062-4369-b08c-313da9c5da5d",
      "name": "更新消息",
      "type": "n8n-nodes-base.telegram",
      "position": [
        400,
        848
      ],
      "parameters": {
        "text": "=Running Update...",
        "chatId": "8209283663",
        "additionalFields": {
          "parse_mode": "HTML",
          "appendAttribution": false
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "887c4a0e-37f5-4a35-8a33-bf7c202b01c1",
      "name": "更新消息1",
      "type": "n8n-nodes-base.telegram",
      "position": [
        1136,
        848
      ],
      "parameters": {
        "text": "={{ $json.message }}",
        "chatId": "8209283663",
        "additionalFields": {
          "parse_mode": "HTML",
          "appendAttribution": false
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "a0357748-6516-47e0-a626-7fab48afdf4e",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -400,
        -1008
      ],
      "parameters": {
        "height": 384,
        "content": "## 传入 Webhook"
      },
      "typeVersion": 1
    },
    {
      "id": "56fb8580-a18d-4660-9b57-eb9f54857562",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -400,
        -448
      ],
      "parameters": {
        "width": 352,
        "height": 304,
        "content": "## 用户交互"
      },
      "typeVersion": 1
    },
    {
      "id": "aadc3881-5a4c-415c-a567-39350f07520e",
      "name": "提取服务名称",
      "type": "n8n-nodes-base.code",
      "position": [
        400,
        -368
      ],
      "parameters": {
        "language": "python",
        "pythonCode": "def extract_service_name(message):\n    # Split by spaces\n    parts = message.split()\n    # Take the first part (service name)\n    if parts:\n        return parts[0]\n    else:\n        return ''\n\n# Example usage:\nincoming_message = _input.first().json.message.text\nservice_name = extract_service_name(incoming_message)\nreturn {\n    \"service_name\": service_name\n}"
      },
      "typeVersion": 2
    },
    {
      "id": "177a270a-8b0c-4dbe-9e01-9edfafaf4668",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        384,
        -496
      ],
      "parameters": {
        "width": 1488,
        "height": 320,
        "content": "## 问题分析器"
      },
      "typeVersion": 1
    },
    {
      "id": "adbdf635-c739-491b-8374-6c29ac00bc8a",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        368,
        -112
      ],
      "parameters": {
        "width": 1488,
        "height": 352,
        "content": "## Docker 重启服务"
      },
      "typeVersion": 1
    },
    {
      "id": "49230022-6b62-4c3f-b498-6e90860f26da",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        368,
        320
      ],
      "parameters": {
        "width": 752,
        "height": 352,
        "content": "## 允许用户获取所有当前部署的 Docker 容器"
      },
      "typeVersion": 1
    },
    {
      "id": "3ad0679c-b841-42c5-b85c-7f736b6de37c",
      "name": "便签5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        368,
        720
      ],
      "parameters": {
        "width": 1232,
        "height": 352,
        "content": "## 自动更新服务器上的所有 Docker 镜像"
      },
      "typeVersion": 1
    },
    {
      "id": "79dcda25-eac3-4c2a-88b6-be597637ef5d",
      "name": "便签6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        368,
        1104
      ],
      "parameters": {
        "width": 464,
        "height": 944,
        "content": "## 用于 Docker 服务器的自定义脚本"
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "If": {
      "main": [
        [
          {
            "node": "Success restart",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Restart Failed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "get logs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge1": {
      "main": [
        [
          {
            "node": "Restart Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Merge1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "docker ps",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Update Msg",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch1": {
      "main": [
        [
          {
            "node": "OK Message",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "ERROR Message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "Switch1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "get logs": {
      "main": [
        [
          {
            "node": "Status Update",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "docker ps": {
      "main": [
        [
          {
            "node": "Docker Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OK Message": {
      "main": [
        []
      ]
    },
    "Update Msg": {
      "main": [
        [
          {
            "node": "Update Docker",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Status Update": {
      "main": [
        [
          {
            "node": "Message a model",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Update Docker": {
      "main": [
        [
          {
            "node": "Code in Python (Beta)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Message a model": {
      "main": [
        [
          {
            "node": "Log Analysis",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Restart Message": {
      "main": [
        [
          {
            "node": "restart container",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Telegram Trigger": {
      "main": [
        [
          {
            "node": "Switch",
            "type": "main",
            "index": 0
          },
          {
            "node": "Extract the Service Name",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "restart container": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code in Python (Beta)": {
      "main": [
        [
          {
            "node": "Update Msg1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract the Service Name": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          },
          {
            "node": "Merge1",
            "type": "main",
            "index": 1
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

这是一个高级难度的工作流,适用于DevOps、AI Chatbot等场景。适合高级用户,包含 16+ 个节点的复杂工作流

需要付费吗?

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

工作流信息
难度等级
高级
节点数量31
分类2
节点类型10
难度说明

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

外部链接
在 n8n.io 上查看 →

分享此工作流