n8n Hands-On Guide 16: How to Send Long Thread X Tweets

10 months ago

In the previous two tutorials, we covered X configuration and sending regular tweets:

n8n Configuration Guide 14: How to Configure X (formerly Twitter) Node n8n Practical Guide 15: How to Automate X Operations

https://appstore.lazycat.cloud/#/shop/detail/cloud.lazycat.app.n8n

Some friends asked if it's possible to send "long thread" tweets. This tutorial will demonstrate exactly how to do that.

First, let me explain what a "long thread" tweet is.

It's a unique type of post on Twitter where you publish a tweet and then continuously add replies to your own post, creating a long connected thread. Here's an example of how it looks:

image.png

First, let's look at the workflow diagram:

image.png

Workflow Setup

Since this workflow is similar to the one in the previous tutorial, we can simply copy the previous workflow, paste it here, and modify it accordingly.

image.png

I've prepared a Google Sheet in advance, formatted as shown:

image.png

First, retrieve data where IsSend=0 and execute:

image.png

Next, add an Edit Fields node to filter out the tweet fields:

image.png

Exclude these 3 fields, the output should look like this on the right:

image.png

Add a Code node to process the data into JSON format. Reference code:

// Get JSON data from the first input item
const obj = $input.first().json;

// Get all values from the obj object and convert to array
const valuesArray = Object.values(obj);

// Map array to object array containing index and value
return valuesArray.map((v, i) => ({
  json: {
    index: i,
    value: v
  }
}));

The effect is as shown:

image.png

Add a loop processing node:

image.png

Prepare to reply to the tweet:

image.png

Here we enter the loop logic. After getting the ID of the previous tweet, directly reply to that tweet ID:

image.png

To prevent API rate limiting, I added a 15-second wait node in the middle:

image.png

Finally, after sending is complete, mark the corresponding row in Google Sheet as sent:

image.png

The core challenge of this workflow is to correctly implement the tweet reply chain:

  1. Each tweet must correctly reference the previous tweet's ID The Twitter API's inReplyToStatusId parameter has strict format requirements Need to dynamically pass and update tweet IDs within the loop

  2. The SplitInBatches node's looping mechanism limits data access Cannot directly reference data from nodes executed outside the loop Need to use static storage ($getWorkflowStaticData) to maintain state across loops

The final result is as shown:

image.png

This completes the workflow for automatically sending long thread tweets.

Author
天天