Help With Twitter Automation

Hi guys

I am trying to get only the newest tweets and then publish them on Telegram.
For some reason my code is not working. Can you please help me with this node?
I am attaching the node code below. I have checked the resources that exist currently and copied the code from them but it does not work for me as the same tweet keeps being published over again.

    {
  "nodes": [
    {
      "parameters": {
        "chatId": "-1001541294502",
        "text": "=👇 AMC تغريدة جديدة من \n{{$node[\"Function\"].json[\"tweet_url\"]}}",
        "additionalFields": {}
      },
      "name": "Send Telegram on AMC Group",
      "type": "n8n-nodes-base.telegram",
      "typeVersion": 1,
      "position": [
        1040,
        690
      ],
      "notesInFlow": true,
      "credentials": {
        "telegramApi": "AMC Bot"
      },
      "notes": "Channel: -1001303719248\nAMC Group: -1001196072954"
    },
    {
      "parameters": {
        "operation": "search",
        "searchText": "arabmarketcap",
        "limit": 2,
        "additionalFields": {
          "includeEntities": true,
          "resultType": "recent"
        }
      },
      "name": "Twitter",
      "type": "n8n-nodes-base.twitter",
      "typeVersion": 1,
      "position": [
        340,
        570
      ],
      "credentials": {
        "twitterOAuth1Api": "AMC Twitter"
      }
    },
    {
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "name": "Cron",
      "type": "n8n-nodes-base.cron",
      "typeVersion": 1,
      "position": [
        150,
        570
      ]
    },
    {
      "parameters": {
        "functionCode": "/*\nconst new_items = [];\nconst data = this.getWorkflowStaticData('node');\n\ndata.ids = data.ids || [];\n\nfor (var i=0; i<items.length; i++) {\n  if (data.ids.includes(items[i].json.id)) {\n    break;\n  } else {\n    new_items.push({json: {id: items[i].json.id, url: items[i].json.url, tweet: items[i].json.tweet, username: items[i].json.username, photo: items[i].json.photo, name: items[i].json.name, color: items[i].json.color}});\n  }\n}\n\ndata.ids = items.map(item => item.json.id)\nreturn new_items;\n*/\nconst staticData = getWorkflowStaticData('global');\nconst newTweetIds = items.map((item) => item.json['Tweet ID']);\nconst oldTweetIds = staticData.oldTweetIds;\n\nif (!oldTweetIds) {\n  staticData.oldTweetIds = newTweetIds;\n  return items;\n}\n\nconst actualNewTweetIds = newTweetIds.filter((id) => !oldTweetIds.includes(id));\nconst actualNewTweets = items.filter((data) =>\n  actualNewTweetIds.includes(data.json['Tweet ID'])\n);\nstaticData.oldTweetIds = [...actualNewTweetIds, ...oldTweetIds];\n\nreturn actualNewTweets;\n"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        680,
        570
      ],
      "alwaysOutputData": true,
      "notes": "Returns new tweets"
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "string": [
            {
              "name": "tweet_screename",
              "value": "={{$node[\"Twitter\"].json[\"user\"][\"name\"]}}\n"
            },
            {
              "name": "tweet_text",
              "value": "={{$json[\"text\"]}}\n"
            },
            {
              "name": "tweet_id",
              "value": "={{$json[\"id\"]}}\n"
            },
            {
              "name": "tweet_url",
              "value": "=https://twitter.com/{{$node[\"Twitter\"].json[\"user\"][\"screen_name\"]}}/status/{{$node[\"Twitter\"].json[\"id_str\"]}}"
            }
          ]
        },
        "options": {
          "dotNotation": true
        }
      },
      "name": "Store Tweet Info",
      "type": "n8n-nodes-base.set",
      "typeVersion": 1,
      "position": [
        520,
        570
      ]
    },
    {
      "parameters": {},
      "name": "NoOp",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1050,
        500
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$node[\"Function\"].json[\"tweet_id\"]}}",
              "operation": "isEmpty"
            }
          ]
        }
      },
      "name": "IF new tweets",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        840,
        570
      ]
    }
  ],
  "connections": {
    "Twitter": {
      "main": [
        [
          {
            "node": "Store Tweet Info",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Cron": {
      "main": [
        [
          {
            "node": "Twitter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "IF new tweets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Store Tweet Info": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF new tweets": {
      "main": [
        [
          {
            "node": "NoOp",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Telegram on AMC Group",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Welcome to the community @alijdev

Are you activating the workflow? Because static data only is persisted when the workflow is activated.

1 Like

Thanks for answer and sorry for late reply, We are actually using active mode for the workflow and the same problem

Hey @Essam_Aldeen!

Welcome to the community :sparkling_heart:

I took a look at the workflow shared above, and the variables are different in the Set node and the Function node. You can use this workflow: https://n8n.io/workflows/875

You will have to update the Search Text in the Twitter node and replace the Mattermost node with a Telegram node. You don’t need to use the IF node in the workflow. The Function node will only return new items.

2 Likes