当前位置: 首页 > news >正文

一条n8n工作流

一条n8n工作流

      • 1 工作流环境信息
      • 2 工作流编辑
      • 3 工作流执行
      • 4 工作流各模块
      • 5 工作流json文件

​ 这里是我的一条工作流,功能是:周期性地获取最新的热点信息,发送到我的钉钉机器人。文末附有完整的工作流json文件,以供有兴趣者参考。

​ 近期使用n8n的主要心得就是:清爽、自由度高。它里面的大模型可以使用ollama、deepseek、openai等,使用开源的话,直接ollama中的7b或者1.5b参数的自己玩也是可以的。如果水平再高些,其实国外有很多免费使用的大模型API,比如groq家的70b参数的,但需要解决网络问题。

1 工作流环境信息

序号序号工具版本大小镜像数据卷
1n8n1.104.21.02GBdocker pull swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/n8nio/n8n:1.104.2-v E:\n8n_data:/home/node/.n8n
2postgres16.4335MBdocker pull swr.cn-north-4.myhuaweicloud.com/ddn-k8s/docker.io/bitnami/postgresql:16-v E:\postgresql16:/bitnami/postgresql

2 工作流编辑

​ 工作流主要涉及AI Agent节点、数据库节点、code自定义节点,是简单工作流的完整呈现。

在这里插入图片描述

3 工作流执行

​ 触发执行这里可以人工点、周期调度两种方式,目前周期调度正常,可以加载文末的json文件,然后修改主要参数就可以执行。

在这里插入图片描述

4 工作流各模块

​ 这里的HTTP-Request节点,这里的热点信息使用的一个免费接口,添加即用。

https://api.1314.cool/getbaiduhot/

在这里插入图片描述

​ 为了保存每天的热点历史信息,我这里使用了postgres数据库,数据库表信息,如下:

CREATE TABLE baidu_hot_words_history (id SERIAL PRIMARY KEY,hot_words VARCHAR(255) NOT NULL,hot_word_url TEXT,record_time TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,record_day DATE DEFAULT CURRENT_DATE
);
-- 添加表注释
COMMENT ON TABLE baidu_hot_words_history IS '百度热搜历史信息存储表';
-- 添加字段注释
COMMENT ON COLUMN baidu_hot_words_history.hot_words IS '热词(热搜关键词)';
COMMENT ON COLUMN baidu_hot_words_history.hot_word_url IS '热词链接(百度搜索URL)';

在这里插入图片描述

​ 通过HTTP节点获取了热搜后,与当天已推送的历史信息比对,保证只推送新增的信息,我这里使用的python代码,当然也可以使用javascript。

if len(_('Select-获取历史数据').all()) == 0:return _('HTTP-百度热点').all()
else:new_hots = {}# http-requestvalue = []for items in _('HTTP-百度热点').all()[0]['json']['data']:# postgresnum = 0for select in _('Select-获取历史数据').all():if items['word'] == select['json']['hot_words']:# 历史信息里存在num += 1if num == 0:value.append(items)new_hots["data"] = valuereturn new_hots

在这里插入图片描述

​ 这里添加了一个过滤判断,有新增才推送下一个节点。

在这里插入图片描述

​ 获取的新增热点,有AI Agent进行整理后,推送给钉钉节点,我这里的prompt如下:

你是一位热搜信息整理专家。
任务:只整理获取的热搜信息{{$node["PyCode-数据比对"].json.toJsonString()}},不产生推荐信息。
请严格按以下格式输出:
热点信息(多少条):
1.热点信息1
2.热点信息2

​ 我这里的chat模型使用的openai的,开源的话可以使用你本地的ollama的,如果你是RMB玩家,可以使用deepseek等,或者国外一些免费api,但是注意数据安全!!!

在这里插入图片描述

​ 最后节点之一是钉钉接口,如何你还不太熟悉,可以参考我的这篇文章:调用钉钉接口实现机器人推送消息_钉钉机器人调用第三方接口-CSDN博客 。

在这里插入图片描述

​ 最后节点之一是及时更新新增热点到历史数据中,也就是postgres中。

INSERT INTO baidu_hot_words_history (hot_words, hot_word_url)
VALUES 
{{ $json.data.map(item => "('" + item.word.replace(/'/g, "''") + "','" + item.url.replace(/'/g, "''") + "')").join(",") 
}};

在这里插入图片描述

5 工作流json文件

​ 保存下面的文本为json文件,在n8n工作流编辑器页面点击从文件导入即可。

在这里插入图片描述

{"name": "Workflow_HotWord_To_Dingding_show","nodes": [{"parameters": {"url": "https://api.1314.cool/getbaiduhot/","options": {}},"type": "n8n-nodes-base.httpRequest","typeVersion": 4.2,"position": [-1088,96],"id": "945ea412-b1d8-4436-84e7-c24434928b68","name": "HTTP-百度热点"},{"parameters": {"operation": "select","schema": "public","table": "baidu_hot_words_history","returnAll": true,"where": {"values": [{"column": "record_day","value": "={{ new Date().toISOString().split('T')[0] }}"}]},"options": {}},"type": "n8n-nodes-base.postgres","typeVersion": 2.6,"position": [-864,96],"id": "83d30cd2-9cf7-4185-aabc-c32ee2110312","name": "Select-获取历史数据","credentials": {"postgres": {"id": "HXP7gxgwbVI8mlKX","name": "Postgres account"}}},{"parameters": {"language": "python","pythonCode": "if len(_('Select-获取历史数据').all()) == 0:\n  return _('HTTP-百度热点').all()\nelse:\n  new_hots = {}\n  # http-request\n  value = []\n  for items in _('HTTP-百度热点').all()[0]['json']['data']:\n    # postgres\n    num = 0\n    for select in _('Select-获取历史数据').all():\n      if items['word'] == select['json']['hot_words']:\n        # 历史信息里存在\n        num += 1\n    if num == 0:\n      value.append(items)\n  new_hots[\"data\"] = value\n  return new_hots"},"type": "n8n-nodes-base.code","typeVersion": 2,"position": [-640,96],"id": "f00feedc-bd3a-4897-85dd-50acdd2e6279","name": "PyCode-数据比对","alwaysOutputData": false},{"parameters": {"conditions": {"options": {"caseSensitive": true,"leftValue": "","typeValidation": "strict","version": 2},"conditions": [{"id": "aaf8b4d1-f776-4626-82ba-b5c12783ba96","leftValue": "={{ $node[\"PyCode-数据比对\"].json.values()[0].length}}","rightValue": 0,"operator": {"type": "number","operation": "gt","name": "filter.operator.gt"}}],"combinator": "and"},"options": {}},"type": "n8n-nodes-base.if","typeVersion": 2.2,"position": [-416,96],"id": "b938433e-bbd5-433d-84cc-cb6be46aa60a","name": "If-新增热词","executeOnce": true},{"parameters": {"promptType": "define","text": "整理热点信息","options": {"systemMessage": "=你是一位热搜信息整理专家。\n任务:只整理获取的热搜信息{{$node[\"PyCode-数据比对\"].json.toJsonString()}},不产生推荐信息。\n请严格按以下格式输出:\n热点信息(多少条):\n1.热点信息1\n2.热点信息2\n\n\n\n\n"}},"type": "@n8n/n8n-nodes-langchain.agent","typeVersion": 2.1,"position": [-192,-160],"id": "036b0f07-0150-4068-96fa-cda634542c0c","name": "AI Agent","retryOnFail": false,"maxTries": 2,"executeOnce": true},{"parameters": {"method": "POST","url": "https://oapi.dingtalk.com/robot/send?access_token=你自己的token","sendHeaders": true,"headerParameters": {"parameters": [{"name": "Content-Type","value": "application/json"}]},"sendBody": true,"specifyBody": "json","jsonBody": "={\n  \"msgtype\": \"text\",\n  \"text\": {\n    \"content\": {{$json.output.toJsonString()}}\n  }\n}","options": {}},"type": "n8n-nodes-base.httpRequest","typeVersion": 4.2,"position": [208,-64],"id": "8102b92e-c490-4e39-bbbc-a262dc7de2a3","name": "钉钉通知"},{"parameters": {"operation": "executeQuery","query": "INSERT INTO baidu_hot_words_history (hot_words, hot_word_url)\nVALUES \n{{ \n  $json.data.map(item => \n    \"('\" + item.word.replace(/'/g, \"''\") + \"','\" + item.url.replace(/'/g, \"''\") + \"')\"\n  ).join(\",\") \n}};","options": {}},"type": "n8n-nodes-base.postgres","typeVersion": 2.6,"position": [-112,240],"id": "c4f56f3a-4ca4-40bd-a32a-ea6b1e6caa53","name": "Insert-更新历史数据","credentials": {"postgres": {"id": "HXP7gxgwbVI8mlKX","name": "Postgres account"}}},{"parameters": {},"type": "n8n-nodes-base.manualTrigger","typeVersion": 1,"position": [-1312,0],"id": "fa3481d8-7f2d-4569-a4ab-d052ec57622b","name": "手动执行"},{"parameters": {"rule": {"interval": [{"field": "minutes"}]}},"type": "n8n-nodes-base.scheduleTrigger","typeVersion": 1.2,"position": [-1312,192],"id": "22a33d38-0fdd-4442-94c6-7340ce144ca6","name": "周期调度"},{"parameters": {"model": {"__rl": true,"mode": "list","value": "gpt-4.1-mini"},"options": {}},"type": "@n8n/n8n-nodes-langchain.lmChatOpenAi","typeVersion": 1.2,"position": [-176,16],"id": "44b4a19d-d9b8-4b04-be56-74c254cdda76","name": "OpenAI Chat Model","credentials": {"openAiApi": {"id": "Hm8gyQkgPUkOVBi6","name": "OpenAi account"}}}],"pinData": {},"connections": {"HTTP-百度热点": {"main": [[{"node": "Select-获取历史数据","type": "main","index": 0}]]},"Select-获取历史数据": {"main": [[{"node": "PyCode-数据比对","type": "main","index": 0}]]},"PyCode-数据比对": {"main": [[{"node": "If-新增热词","type": "main","index": 0}]]},"If-新增热词": {"main": [[{"node": "AI Agent","type": "main","index": 0},{"node": "Insert-更新历史数据","type": "main","index": 0}],[]]},"AI Agent": {"main": [[{"node": "钉钉通知","type": "main","index": 0}]]},"手动执行": {"main": [[{"node": "HTTP-百度热点","type": "main","index": 0}]]},"周期调度": {"main": [[{"node": "HTTP-百度热点","type": "main","index": 0}]]},"钉钉通知": {"main": [[]]},"OpenAI Chat Model": {"ai_languageModel": [[{"node": "AI Agent","type": "ai_languageModel","index": 0}]]}},"active": false,"settings": {"executionOrder": "v1"},"versionId": "55bd8cd7-134f-4cc4-8a93-2cce358d5d94","meta": {"templateCredsSetupCompleted": true,"instanceId": "1f16bc26aa41d68046a6cd33330705e84dc6e662ed18adf783908c69e188965b"},"id": "0OYbYUyfETIADQVv","tags": []
}
http://www.lryc.cn/news/619685.html

相关文章:

  • electron进程间通信- 从渲染进程到主进程
  • Python open 函数详解:参数用法与文件操作实战指南
  • 美团搜索推荐统一Agent之需求分析与架构设计
  • Queue参考代码
  • CompletableFuture介绍及使用方式
  • 闹钟时间到震动与声响提醒的实现-库函数版(STC8)
  • 基于R语言的现代贝叶斯统计学方法(贝叶斯参数估计、贝叶斯回归、贝叶斯计算)实践
  • 计算机网络——协议
  • LangGraph 指南篇-基础控制
  • Linux软件编程3.(文件IO和目录IO)
  • 谷歌、facebook、tiktok广告账户多开怎么安全?亚马逊、ebay、shopee多店铺怎么做好?看看adspower工具,注册免费试用及实用技巧分享
  • 美团搜索推荐统一Agent之交互协议与多Agent协同
  • 在es中安装kibana
  • 动静态库
  • ICCV 2025 | 4相机干掉480机位?CMU MonoFusion高斯泼溅重构4D人体!
  • 内容索引之word转md工具 - markitdown
  • (cvpr2025) IceDiff: 高分辨率北极海冰预报
  • duiLib 利用布局文件显示一个窗口并响应事件
  • 基于UniApp的新大陆物联网平台温湿度检测系统开发方案
  • 在JVM跑JavaScript脚本 | Oracle GraalJS 简介与实践
  • 【AI论文】GLM-4.5:具备智能体特性、推理能力与编码能力的(ARC)基础模型
  • Avalon-MM协议
  • 浅层神经网络
  • SimD小目标样本分配方法
  • 开发避坑指南(24):RocketMQ磁盘空间告急异常处理,CODE 14 “service not available“解决方案
  • 设计原则之【抽象层次一致性(SLAP)】,方法也分三六九等
  • 从零到一:TCP 回声服务器与客户端的完整实现与原理详解
  • Linux LNMP配置全流程
  • 机器学习之词向量转换
  • 第5章 学习的机制