8
n8n 中文网amn8n.com

Twitter 自动化

中级

这是一个自动化工作流,包含 14 个节点。主要使用 Set, Code, Twitter, RedditTool, GoogleSheets 等节点。 使用 Gemini 和 Google Sheets 将 Reddit 帖子重新利用为 AI 推文

前置要求
  • Twitter API 凭证
  • Google Sheets API 凭证
  • Google Gemini API Key

分类

-
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "TdUwxmTIJnTwQhJe",
  "meta": {
    "instanceId": "52254486b159b349334953c1738da94e90477c7604aa8db2062d11afc0120739",
    "templateCredsSetupCompleted": true
  },
  "name": "Twitter 自动化",
  "tags": [
    {
      "id": "GcbJtfnHd72wKEMY",
      "name": "admin",
      "createdAt": "2025-05-06T11:04:59.376Z",
      "updatedAt": "2025-05-06T11:04:59.376Z"
    }
  ],
  "nodes": [
    {
      "id": "1cb94f67-d47c-4cdc-a231-d87adc9f8f42",
      "name": "代码1",
      "type": "n8n-nodes-base.code",
      "position": [
        128,
        2272
      ],
      "parameters": {
        "jsCode": "// Templates for normal tweets\nconst subreddits = [\n  \"n8n\",\n  \"microsaas\",\n  \"SaaS\",\n  \"automation\",\n  \"n8n_ai_agents\"\n];\n\n// Track last output to avoid duplicates\nif (!global.lastTweet) {\n  global.lastTweet = null;\n}\n\nfunction getRandom(arr) {\n  return arr[Math.floor(Math.random() * arr.length)];\n}\n\nlet tweet;\nlet ads = false\n\ndo {\n  if (Math.random() < 0.2) {\n    // 20% chance → promo\n    tweet = \"advertise\";\n    ads = true;\n  } else {\n    // 80% chance → template\n    tweet = getRandom(subreddits);\n  }\n} while (tweet === global.lastTweet); // prevent repeats\n\n// Save for next run\nglobal.lastTweet = tweet;\n\nreturn [{ json: { tweet, ads} }];\n\n"
      },
      "typeVersion": 2
    },
    {
      "id": "01a9b39a-784a-409d-b4f1-3a1a345e3dca",
      "name": "推文制作器1",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        528,
        2272
      ],
      "parameters": {
        "text": "={{ $json.tweet }}",
        "options": {
          "systemMessage": "=You are a ghostwriter who creates short, raw, non-repetitive tweets. \nYour job: generate a tweet refereingcing a post you saw on reddit\n\nYou have access to 3 tools to \n1. get trending posts from a subreddit\n2. get a list of recently posted tweets in the users account\n3. log a tweet based on a subredit post_id so that we dont mrecreate a similar tweet based on the post_id\n\nRules:  \n- Get the subreddit from the user\n- Fetch trending subreddit posts from the database reddit tool.\n- Fetch the past tweets that has been posted and logged to the database\n- Choose a subreddit post_id to write about and make sure that post_id in that subreddit has never been used before to write a tweet\n- Generate a unique tweet. Write from the first person point of view. something like i discovered this cool workflow on reddit or this tool. Limit to 200 characters\n- Tweets must be punchy, edgy, and written in modern Twitter style.  Have a strong opinion whether u think its cool or not. you are very edgy programmer turned enterpreneur but avoid all words of profanity, vulgar and sexual words\n- Keep it concise (2–4 lines). No hashtags. Minimal emojis (only if it fits). Ask for opinions. Always state what u saw\n- Use a separate line for each short phrase or thought"
        },
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "183cfd15-198f-4f7d-824d-e11bdadb962c",
      "name": "读取数据库2",
      "type": "n8n-nodes-base.googleSheetsTool",
      "position": [
        672,
        2464
      ],
      "parameters": {
        "options": {},
        "filtersUI": {
          "values": [
            {
              "lookupValue": "={{ $fromAI('subreddit', `subreddit`, 'string') }}",
              "lookupColumn": "subreddit"
            },
            {
              "lookupValue": "={{ $fromAI('id', `id of the post`, 'string') }}",
              "lookupColumn": "post_id"
            }
          ]
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1DbPO7U68-YlOHhb8fIlGu2ImD_N-cm0E-8pwc7qnAo4/edit#gid=0",
          "cachedResultName": "posts"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1DbPO7U68-YlOHhb8fIlGu2ImD_N-cm0E-8pwc7qnAo4",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1DbPO7U68-YlOHhb8fIlGu2ImD_N-cm0E-8pwc7qnAo4/edit?usp=drivesdk",
          "cachedResultName": "Twitter Automation"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "dNmdYyKDCj9rTeSw",
          "name": "Gsheet"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "beed3c7c-7b8f-4158-a9d4-64e66288659d",
      "name": "Google Gemini 聊天模型1",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        384,
        2320
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "googlePalmApi": {
          "id": "VogGayxALH0ssmBl",
          "name": "Gemini"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "995f4e4e-dd8c-474d-b22d-f4bd4697d043",
      "name": "计划触发器1",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -96,
        2272
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 2
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "f19be085-9dce-4751-b51b-a5a05eda309c",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        80,
        2160
      ],
      "parameters": {
        "width": 256,
        "height": 608,
        "content": "## 随机选择子版块"
      },
      "typeVersion": 1
    },
    {
      "id": "8afc5b1f-1367-4846-8154-7d2357b8c9d6",
      "name": "创建推文1",
      "type": "n8n-nodes-base.twitter",
      "position": [
        1312,
        2272
      ],
      "parameters": {
        "text": "={{ $json.tweet }}",
        "additionalFields": {
          "attachments": "={{ $json.image_id || null }}"
        }
      },
      "credentials": {
        "twitterOAuth2Api": {
          "id": "Ig60ncJDzlhLU0Ap",
          "name": "X"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "1fbf01eb-2e78-48d1-86b0-aba52156f958",
      "name": "结构化输出解析器2",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        832,
        2336
      ],
      "parameters": {
        "schemaType": "manual",
        "inputSchema": "{\n\t\"type\": \"object\",\n\t\"properties\": {\n\t\t\"tweet\": {\n\t\t\t\"type\": \"string\"\n\t\t},\n      \"subreddit\": {\n\t\t\t\"type\": \"string\"\n\t\t},\n\t\t\"id\": {\n\t\t\t\"type\": \"string\", \n          \"description\": \"id of the post on reddit\"\n\t\t}\n\t},\n  \"required\": [\"tweet\"]\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "9943e3e6-ad59-4e34-acb0-af2be3f0a062",
      "name": "在 Reddit1 中获取多个帖子",
      "type": "n8n-nodes-base.redditTool",
      "position": [
        496,
        2448
      ],
      "parameters": {
        "limit": 10,
        "filters": {
          "category": "rising"
        },
        "operation": "getAll",
        "subreddit": "={{$fromAI('subreddit','name of the subreddit','string')}}"
      },
      "credentials": {
        "redditOAuth2Api": {
          "id": "rIyaOqpm6SelzXHv",
          "name": "Reddit account"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "3de9105e-f1c2-4ad4-8617-48a1ca259069",
      "name": "在表格1 中添加行",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1664,
        2272
      ],
      "parameters": {
        "columns": {
          "value": {
            "Date": "={{$now.format('dd/MM/yyyy')}}",
            "post_id": "={{ $('Edit Fields1').item.json.post_id }}",
            "subreddit": "={{ $('Edit Fields1').item.json.subreddit }}",
            "PAST TWEETS": "={{ $('Edit Fields1').item.json.tweet }}"
          },
          "schema": [
            {
              "id": "PAST TWEETS",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "PAST TWEETS",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Date",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Date",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "subreddit",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "subreddit",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "post_id",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "post_id",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1DbPO7U68-YlOHhb8fIlGu2ImD_N-cm0E-8pwc7qnAo4/edit#gid=0",
          "cachedResultName": "posts"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1DbPO7U68-YlOHhb8fIlGu2ImD_N-cm0E-8pwc7qnAo4",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1DbPO7U68-YlOHhb8fIlGu2ImD_N-cm0E-8pwc7qnAo4/edit?usp=drivesdk",
          "cachedResultName": "Twitter Automation"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "dNmdYyKDCj9rTeSw",
          "name": "Gsheet"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "e100fb38-9f6a-42a3-bd97-a08a0077bfc0",
      "name": "编辑字段1",
      "type": "n8n-nodes-base.set",
      "position": [
        1136,
        2272
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "316d8d54-ce09-409b-ae4e-a2dffe41d011",
              "name": "tweet",
              "type": "string",
              "value": "={{$json.output.tweet}}"
            },
            {
              "id": "69c01373-920f-4b2a-92bf-9a3f2fad16b2",
              "name": "subreddit",
              "type": "string",
              "value": "={{$json.output.subreddit || null}}"
            },
            {
              "id": "a8b984bf-a55d-4ac3-9afc-d2fd95d1336c",
              "name": "post_id",
              "type": "string",
              "value": "={{ $json.output.id || null}}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "79f6984b-1080-4477-8c4a-0f77d0459755",
      "name": "便签 10",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        960,
        2160
      ],
      "parameters": {
        "color": 5,
        "width": 624,
        "height": 608,
        "content": "## 发布到 Twitter"
      },
      "typeVersion": 1
    },
    {
      "id": "b5381fec-824a-44c2-9cde-cdabbbe4c542",
      "name": "便签11",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1600,
        2160
      ],
      "parameters": {
        "color": 4,
        "width": 512,
        "height": 608,
        "content": "## 更新 Google 表格"
      },
      "typeVersion": 1
    },
    {
      "id": "bb800730-5a63-411e-bdbb-63b28fb683ae",
      "name": "便签9",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        352,
        2160
      ],
      "parameters": {
        "color": 2,
        "width": 592,
        "height": 608,
        "content": "## 从 reddit 重新利用内容"
      },
      "typeVersion": 1
    }
  ],
  "active": true,
  "pinData": {},
  "settings": {
    "callerPolicy": "workflowsFromSameOwner",
    "errorWorkflow": "ernonQOxi07n6WGi",
    "executionOrder": "v1",
    "timeSavedPerExecution": 5
  },
  "versionId": "2483f036-f1af-4907-9a3a-26dd84a14018",
  "connections": {
    "Code1": {
      "main": [
        [
          {
            "node": "Tweet maker1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields1": {
      "main": [
        [
          {
            "node": "Creates the tweet1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tweet maker1": {
      "main": [
        [
          {
            "node": "Edit Fields1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "read database2": {
      "ai_tool": [
        [
          {
            "node": "Tweet maker1",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger1": {
      "main": [
        [
          {
            "node": "Code1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Creates the tweet1": {
      "main": [
        [
          {
            "node": "Append row in sheet1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Append row in sheet1": {
      "main": [
        []
      ]
    },
    "Get many posts in Reddit1": {
      "ai_tool": [
        [
          {
            "node": "Tweet maker1",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "Tweet maker1",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser2": {
      "ai_outputParser": [
        [
          {
            "node": "Tweet maker1",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

中级

需要付费吗?

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

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

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

作者
Jay Emp0

Jay Emp0

@jay-emp0

AI and Automation developer. Im implementing n8n and ai tools to automate marketing and sales in companies

外部链接
在 n8n.io 查看

分享此工作流