n8n自动化工作流
设计方案

通过Telegram Bot控制的智能自动化解决方案

自动打卡

定时打卡、查询状态、灵活配置

自动抢购

实时监控、精准抢购、即时通知

方案概述

方案 A:自动打卡工作流

适用场景:公司考勤、学习打卡、健身打卡等

  • 支持定时自动打卡
  • 实时查询打卡状态
  • 灵活配置打卡时间
  • Telegram即时通知

方案 B:自动抢购工作流

适用场景:限时商品抢购、秒杀活动等

  • 实时监控商品状态
  • 精准时间点抢购
  • 自动下单支付
  • 成功失败即时通知

基础架构设计

1

部署 n8n

使用 Docker 快速部署 n8n 自动化平台

bash
# 使用 Docker 部署 n8n(推荐版本 1.78.1)
docker run -d \
  --name n8n \
  -p 5678:5678 \
  -e GENERIC_TIMEZONE="Asia/Shanghai" \
  -e TZ="Asia/Shanghai" \
  -v n8n_data:/home/node/.n8n \
  n8nio/n8n:1.78.1
2

创建 Telegram Bot

通过 BotFather 创建控制机器人

  • 在 Telegram 搜索 @BotFather
  • 发送 /newbot 创建新 bot
  • 获取 Bot Token(格式:123456:ABC-DEF...
  • 发送 /setcommands 设置命令菜单

方案 A:自动打卡工作流

工作流架构图

Telegram Bot
命令判断
/checkin
/status
/schedule
HTTP Request
发送结果通知

详细节点配置

json
{
  "node": "Telegram Trigger",
  "type": "telegram-trigger",
  "config": {
    "authentication": "credentials",
    "updates": ["message"],
    "additionalFields": {
      "download_attachments": false
    }
  }
}
json
{
  "node": "Switch",
  "type": "switch",
  "rules": [
    {
      "name": "打卡",
      "condition": "{{ $json.message.text === '/checkin' }}"
    },
    {
      "name": "查询",
      "condition": "{{ $json.message.text === '/status' }}"
    },
    {
      "name": "定时设置",
      "condition": "{{ $json.message.text.startsWith('/schedule') }}"
    }
  ]
}
json
{
  "node": "HTTP Request",
  "type": "http-request",
  "method": "POST",
  "url": "https://your-checkin-api.com/api/checkin",
  "authentication": "headerAuth",
  "headers": {
    "Content-Type": "application/json",
    "Authorization": "Bearer YOUR_TOKEN"
  },
  "body": {
    "user_id": "{{ $json.message.from.id }}",
    "timestamp": "{{ $now }}",
    "location": "office"
  }
}
json
{
  "node": "Telegram",
  "type": "telegram",
  "operation": "sendMessage",
  "chatId": "{{ $json.message.chat.id }}",
  "text": "✅ 打卡成功!\n时间:{{ $now.format('YYYY-MM-DD HH:mm:ss') }}\n地点:办公室"
}
json
{
  "node": "Cron",
  "type": "cron",
  "triggerTimes": {
    "mode": "custom",
    "cronExpression": "0 9 * * 1-5"
  },
  "comment": "工作日早上9点自动打卡"
}

方案 B:自动抢购工作流

工作流架构图

配置抢购任务
存储任务信息
定时轮询检查
检查商品状态
判断可购买
YES → 执行抢购
NO → 继续监控

详细节点配置

json
{
  "command": "/snipe",
  "format": "/snipe <商品URL> <抢购时间>",
  "example": "/snipe https://shop.com/item/123 2026-01-22 10:00:00"
}
javascript
// Code Node - 解析抢购参数
const message = $input.first().json.message.text;
const parts = message.split(' ');

return {
  json: {
    productUrl: parts[1],
    targetTime: new Date(parts[2] + ' ' + parts[3]),
    userId: $input.first().json.message.from.id,
    chatId: $input.first().json.message.chat.id
  }
};
json
{
  "node": "HTTP Request",
  "method": "GET",
  "url": "{{ $json.productUrl }}",
  "options": {
    "timeout": 5000,
    "followRedirect": true
  }
}
javascript
// 解析页面HTML,检查库存状态
const html = $input.first().json.body;
const inStock = html.includes('立即购买') || html.includes('Add to Cart');
const price = html.match(/¥(\d+\.?\d*)/)?.[1];

return {
  json: {
    inStock: inStock,
    price: price,
    timestamp: new Date().toISOString()
  }
};
json
{
  "conditions": {
    "boolean": [
      {
        "value1": "={{ $json.inStock }}",
        "value2": true
      }
    ],
    "dateTime": [
      {
        "value1": "={{ $now }}",
        "operation": "after",
        "value2": "={{ $json.targetTime }}"
      }
    ]
  }
}
json
{
  "method": "POST",
  "url": "https://shop.com/api/order",
  "headers": {
    "Cookie": "YOUR_SESSION_COOKIE",
    "User-Agent": "Mozilla/5.0..."
  },
  "body": {
    "product_id": "{{ $json.productId }}",
    "quantity": 1,
    "payment_method": "alipay"
  }
}
javascript
// 成功通知
"🎉 抢购成功!\n" +
"商品:{{ $json.productName }}\n" +
"价格:¥{{ $json.price }}\n" +
"时间:{{ $now }}\n" +
"订单号:{{ $json.orderId }}"

// 失败通知
"❌ 抢购失败\n" +
"原因:{{ $json.error }}\n" +
"将继续监控..."

Telegram Bot 命令配置

在 BotFather 中设置以下命令菜单:

/checkin 立即打卡
/status 查询打卡状态
/schedule 设置定时打卡时间
/snipe 配置抢购任务 (格式: /snipe <URL> <时间>)
/list 查看所有抢购任务
/cancel 取消抢购任务
/help 显示帮助信息

安全配置建议

1. 用户认证

限制授权用户访问工作流

javascript
// 在工作流开始添加认证节点
const allowedUsers = [123456789, 987654321]; // 你的 Telegram ID
const userId = $json.message.from.id;

if (!allowedUsers.includes(userId)) {
  return {
    json: {
      authorized: false,
      message: "⛔️ 未授权用户"
    }
  };
}

2. 敏感信息存储

  • 使用 n8n 的 Credentials 功能存储 API Token
  • Cookie 和 Session 使用环境变量
  • 不要在工作流中硬编码密码

3. 频率限制

防止频繁调用API

javascript
// 防止频繁调用
const lastCallTime = $getWorkflowStaticData('lastCall');
const now = Date.now();

if (now - lastCallTime < 5000) { // 5秒间隔
  throw new Error('请求过于频繁,请稍后再试');
}

$setWorkflowStaticData('lastCall', now);

部署步骤

1

启动 n8n

使用前面提供的 Docker 命令启动 n8n 服务

2

访问 n8n 控制台

在浏览器中打开:http://your-server:5678

3

导入工作流 JSON

创建新工作流并粘贴配置代码

4

配置 Credentials

  • Telegram Bot Token
  • API 认证信息
  • Cookie 和 Session 数据
5

激活工作流

点击工作流右上角的激活开关

6

测试 Telegram 命令

在 Telegram 中向 Bot 发送测试命令验证功能

完整工作流包含

Telegram Bot 触发器
命令解析和路由
HTTP API 调用
错误处理和重试
定时任务
结果通知