Retweet Tweets with a special Hashtag

Hey there,
I´m getting more and more into the n8n world and I guess I´m a little bit addicted to this. But as often, I come across some questions I can´t solve myself. Maybe someone can help me.

What´s my goal?
I want to retweet tweets from other Users with a special hashtag #stuttgarttype

What have I done so far?

  1. Cronjob, to execute the workflow every hour
  2. Twitter node: Search for tweets with the hashtag #stuttgarttype (limit: 50)
  3. Twitter node: Retweet the result

At point 3 I don´t know what to do. I have the hashtag results from node 2. Ok. But how can I retweet the newest tweet(s) that haven´t been tweeted already? Sorry, I hope I didn´t explain it too complicated.

Here is my workflow so far:

{
  "nodes": [
    {
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "mode": "everyHour"
            }
          ]
        }
      },
      "name": "Cron",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        460,
        300
      ]
    },
    {
      "parameters": {
        "operation": "search",
        "searchText": "#stuttgarttype",
        "additionalFields": {}
      },
      "name": "Twitter_Search",
      "type": "n8n-nodes-base.twitter",
      "typeVersion": 1,
      "position": [
        680,
        300
      ],
      "credentials": {
        "twitterOAuth1Api": {
          "id": "5",
          "name": "Stuttgart_Type"
        }
      }
    },
    {
      "parameters": {
        "operation": "retweet",
        "tweetId": "={{$node[\"Twitter_Search\"].json[\"id\"]}}",
        "additionalFields": {
          "trimUser": false
        }
      },
      "name": "Twitter_Retweet",
      "type": "n8n-nodes-base.twitter",
      "typeVersion": 1,
      "position": [
        900,
        300
      ],
      "credentials": {
        "twitterOAuth1Api": {
          "id": "5",
          "name": "Stuttgart_Type"
        }
      }
    }
  ],
  "connections": {
    "Cron": {
      "main": [
        [
          {
            "node": "Twitter_Search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Twitter_Search": {
      "main": [
        [
          {
            "node": "Twitter_Retweet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Thanks for your help and have a nice evening.

Hi @Floyd, I hope you’re well? It seems on your second Twitter node you are referencing the id value of the tweets you have previously searched. Twitter would, however, want the id_str value. So if you simply update your expression from {{$node["Twitter_Search1"].json["id"]}} to something like {{$json["id_str"]}}, Twitter should find the Tweet you’re looking for and let you retweet it.

Edit: Keep in mind that Twitter will only let you retweet each Tweet once though. So if you try to retweet Retweets of a Tweet you have already retweeted you might get another error.

So you might want to either ignore that or add a bit of filtering for that, e.g. like so (I’m not 100% sure about this one as I am not super familiar with Twitter’s behaviour, but it seems to have worked for me):

Example Workflow
{
  "nodes": [
    {
      "parameters": {
        "operation": "search",
        "searchText": "#stuttgarttype",
        "additionalFields": {}
      },
      "name": "Twitter_Search",
      "type": "n8n-nodes-base.twitter",
      "typeVersion": 1,
      "position": [
        480,
        140
      ],
      "credentials": {
        "twitterOAuth1Api": {
          "id": "9",
          "name": "@MutedJam"
        }
      }
    },
    {
      "parameters": {
        "operation": "retweet",
        "tweetId": "={{$json[\"id_str\"]}}",
        "additionalFields": {
          "trimUser": false
        }
      },
      "name": "Twitter_Retweet",
      "type": "n8n-nodes-base.twitter",
      "typeVersion": 1,
      "position": [
        920,
        140
      ],
      "credentials": {
        "twitterOAuth1Api": {
          "id": "9",
          "name": "@MutedJam"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"retweeted_status\"]}}",
              "operation": "isEmpty"
            }
          ]
        }
      },
      "name": "Original Tweet?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        700,
        140
      ]
    },
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "operation": "search",
        "searchText": "#stuttgarttype",
        "additionalFields": {}
      },
      "name": "Twitter_Search",
      "type": "n8n-nodes-base.twitter",
      "typeVersion": 1,
      "position": [
        480,
        140
      ],
      "credentials": {
        "twitterOAuth1Api": {
          "id": "9",
          "name": "@MutedJam"
        }
      }
    },
    {
      "parameters": {
        "operation": "retweet",
        "tweetId": "={{$json[\"id_str\"]}}",
        "additionalFields": {
          "trimUser": false
        }
      },
      "name": "Twitter_Retweet",
      "type": "n8n-nodes-base.twitter",
      "typeVersion": 1,
      "position": [
        920,
        140
      ],
      "credentials": {
        "twitterOAuth1Api": {
          "id": "9",
          "name": "@MutedJam"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"retweeted_status\"]}}",
              "operation": "isEmpty"
            }
          ]
        }
      },
      "name": "Original Tweet?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        700,
        140
      ]
    }
  ],
  "connections": {
    "Twitter_Search": {
      "main": [
        [
          {
            "node": "Original Tweet?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Original Tweet?": {
      "main": [
        [
          {
            "node": "Twitter_Retweet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Start": {
      "main": [
        [
          {
            "node": "Twitter_Search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
1 Like

Hey @MutedJam, thanks for clearing things up. I guess I understand it a little bit better now :wink: The workflow works, but there is one more thing I try to solve. How can I prevent, that my own tweets with the hashtag #stuttgarttype will be retweetet.

One simple workaround, that comes to my mind: the usage of two hashtags. One for my own tweets, e.g. #stuttgart_type and one for the retweets, e.g. #stuttgarttype.

But maybe there is a solution that it is possible to prevent my own tweets from being retweetet. Do you see a solution? Can you give me a little help, please?

Thank you so much for your help so far.

So the IF node in my example workflow above will already check whether the retweeted_status field provided by Twitter is empty (to make sure a Tweet is an original Tweet and not a Retweet).

You could add a separate condition to this node checking the user’s name doesn’t match your name:

image

The expression used in the Value 1 field above is {{$json["user"]["screen_name"]}}.

The below example workflow searches for Tweets with n8n and then filters out those that are Retweets and those that are from @n8n_io:

Example Workflow
{
  "nodes": [
    {
      "parameters": {
        "operation": "retweet",
        "tweetId": "={{$json[\"id_str\"]}}",
        "additionalFields": {
          "trimUser": false
        }
      },
      "name": "Twitter_Retweet",
      "type": "n8n-nodes-base.twitter",
      "typeVersion": 1,
      "position": [
        880,
        440
      ],
      "credentials": {
        "twitterOAuth1Api": {
          "id": "9",
          "name": "@MutedJam"
        }
      }
    },
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        240,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"retweeted_status\"]}}",
              "operation": "isEmpty"
            },
            {
              "value1": "={{$json[\"user\"][\"screen_name\"]}}",
              "operation": "notEqual",
              "value2": "n8n_io"
            }
          ]
        }
      },
      "name": "Original Tweet?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        660,
        440
      ]
    },
    {
      "parameters": {
        "operation": "search",
        "searchText": "n8n",
        "additionalFields": {}
      },
      "name": "Twitter_Search",
      "type": "n8n-nodes-base.twitter",
      "typeVersion": 1,
      "position": [
        440,
        440
      ],
      "credentials": {
        "twitterOAuth1Api": {
          "id": "9",
          "name": "@MutedJam"
        }
      }
    },
    {
      "parameters": {
        "operation": "retweet",
        "tweetId": "={{$json[\"id_str\"]}}",
        "additionalFields": {
          "trimUser": false
        }
      },
      "name": "Twitter_Retweet",
      "type": "n8n-nodes-base.twitter",
      "typeVersion": 1,
      "position": [
        880,
        440
      ],
      "credentials": {
        "twitterOAuth1Api": {
          "id": "9",
          "name": "@MutedJam"
        }
      }
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Twitter_Search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Original Tweet?": {
      "main": [
        [
          {
            "node": "Twitter_Retweet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Twitter_Search": {
      "main": [
        [
          {
            "node": "Original Tweet?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Hope this helps :slight_smile:

1 Like

Wow @MutedJam, thanks a lot for your help. I have to play a little bit around with the IF node. Seems to be a node I would need often :wink:

I really appreciate your help and will no spend the afternoon with playing around with different nodes. I have to learn a lot :slight_smile:

Thank you!

1 Like

You are most welcome! Let me know if you get stuck somewhere.

2 Likes