Get New Tweets from Twitter + Add to CRM Automation Issue

Hi guys

I am trying to get only the newest tweets based on a query from twitter, check the username in my crm nimber and if the username and other conditions are false, it will check tweet text for text classification in Monkeylearn and if it passes, it will the add the Tweet and the user to my CRM as a contact.

I found another post that referenced this workflow “https://n8n.io/workflows/875” but I need to iterate each individual tweet through the workflow. If I send all the newest tweets through at once the Nimble crm lookup returns multiple results instead of one and then breaks the rest of the flow. If I return one tweet (limit = 1) in the twitter node instead of multiple then (>50) then it works fine.

{
  "name": "Tweets \"hiring\" \"remote\"",
  "nodes": [
    {
      "parameters": {},
      "name": "Start",
      "type": "n8n-nodes-base.start",
      "typeVersion": 1,
      "position": [
        0,
        70
      ]
    },
    {
      "parameters": {
        "resource": "lookup",
        "query": "={\"twitter\":{\"contain\":\"{{$node[\"Twitter\"].json[\"user\"][\"screen_name\"]}}\"}}",
        "per_page": 20
      },
      "name": "Nimble",
      "type": "n8n-nodes-base.nimble",
      "typeVersion": 1,
      "position": [
        480,
        360
      ],
      "alwaysOutputData": true,
      "credentials": {
        "nimbleApi": "Jibril"
      }
    },
    {
      "parameters": {
        "first_name": "={{$node[\"Twitter\"].json[\"user\"][\"name\"]}}",
        "last_name": "1",
        "phone": "[{\"modifier\": \"work\",\"value\": \"123123123\"}]",
        "twitter": "=[{\"avatar_url\": \"\",\"group\": \"Contact Info\",\"user_id\": \"\", \"user_name\": \"\", \"modifier\": \"\", \"field_id\":\"\", \"value\": \"{{$node[\"Twitter\"].json[\"user\"][\"screen_name\"]}}\", \"label\": \"twitter\"}]",
        "url": "=[{\"modifier\": \"work\",\"value\": \"{{$node[\"Twitter\"].json[\"user\"][\"url\"]}}\", \"label\": \"URL\"}]",
        "tags": "twitterjobn8n"
      },
      "name": "Nimble1",
      "type": "n8n-nodes-base.nimble",
      "typeVersion": 1,
      "position": [
        910,
        200
      ],
      "credentials": {
        "nimbleApi": "Jibril"
      }
    },
    {
      "parameters": {
        "operation": "search",
        "searchText": "\"hiring\" \"remotejobs\" ",
        "additionalFields": {
          "includeEntities": true,
          "lang": "en",
          "locationFieldsUi": {
            "locationFieldsValues": {
              "latitude": "43.010493",
              "longitude": "-92.101723",
              "radius": "km",
              "distance": 2770
            }
          },
          "resultType": "recent"
        }
      },
      "name": "Twitter",
      "type": "n8n-nodes-base.twitter",
      "typeVersion": 1,
      "position": [
        -110,
        330
      ],
      "alwaysOutputData": true,
      "credentials": {
        "twitterOAuth1Api": "Twitter OAuth account"
      }
    },
    {
      "parameters": {
        "model_id": "cl_uYLPM7di",
        "data": "=[\"{{$node[\"Twitter\"].json[\"text\"]}}\"]"
      },
      "name": "MonkeyLearn",
      "type": "n8n-nodes-base.monkeyLearn",
      "typeVersion": 1,
      "position": [
        790,
        350
      ],
      "alwaysOutputData": false,
      "credentials": {
        "monkeyLearnApi": "Jibril"
      }
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [],
          "string": [
            {
              "value1": "={{$node[\"Twitter\"].json[\"user\"][\"screen_name\"]}}",
              "operation": "contains",
              "value2": "tmj_"
            },
            {
              "value1": "={{$node[\"Twitter\"].json[\"user\"][\"screen_name\"]}}",
              "operation": "contains",
              "value2": "remot"
            },
            {
              "value1": "={{$node[\"Twitter\"].json[\"user\"][\"screen_name\"]}}",
              "operation": "contains",
              "value2": "job"
            },
            {
              "value1": "={{$node[\"Twitter\"].json[\"user\"][\"name\"]}}",
              "operation": "contains",
              "value2": "job"
            },
            {
              "value1": "={{$node[\"Twitter\"].json[\"user\"][\"name\"]}}",
              "operation": "contains",
              "value2": "Incluzion"
            }
          ]
        },
        "combineOperation": "any"
      },
      "name": "IF",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        360,
        160
      ],
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "conditions": {
          "boolean": [],
          "string": [
            {
              "value1": "={{$node[\"Nimble\"].json[\"resources\"][1][\"fields\"][\"twitter\"][0][\"value\"]}}",
              "operation": "contains",
              "value2": "={{$node[\"Twitter\"].json[\"user\"][\"screen_name\"].toLowerCase()}}"
            }
          ]
        },
        "combineOperation": "any"
      },
      "name": "IF1",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        630,
        170
      ],
      "alwaysOutputData": true
    },
    {
      "parameters": {
        "functionCode": "const 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"
      },
      "name": "Function",
      "type": "n8n-nodes-base.function",
      "position": [
        220,
        330
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "keepOnlySet": true,
        "values": {
          "number": [
            {
              "name": "id",
              "value": "={{$node[\"Twitter\"].json[\"id\"]}}"
            }
          ],
          "string": [
            {
              "name": "url",
              "value": "=https://twitter.com/{{$node[\"Twitter\"].json[\"user\"][\"screen_name\"]}}/status/{{$node[\"Twitter\"].json[\"id_str\"]}}"
            },
            {
              "name": "tweet",
              "value": "={{$node[\"Twitter\"].json[\"text\"]}}"
            },
            {
              "name": "username",
              "value": "={{$node[\"Twitter\"].json[\"user\"][\"screen_name\"]}}"
            },
            {
              "name": "photo",
              "value": "={{$node[\"Twitter\"].json[\"user\"][\"profile_image_url_https\"]}}"
            },
            {
              "name": "name",
              "value": "={{$node[\"Twitter\"].json[\"user\"][\"name\"]}}"
            },
            {
              "name": "color",
              "value": "={{$node[\"Twitter\"].json[\"user\"][\"profile_link_color\"]}}"
            }
          ]
        },
        "options": {
          "dotNotation": true
        }
      },
      "name": "Set",
      "type": "n8n-nodes-base.set",
      "position": [
        100,
        200
      ],
      "typeVersion": 1
    },
    {
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "name": "Cron1",
      "type": "n8n-nodes-base.cron",
      "position": [
        -260,
        140
      ],
      "typeVersion": 1
    }
  ],
  "connections": {
    "Start": {
      "main": [
        []
      ]
    },
    "Twitter": {
      "main": [
        [
          {
            "node": "Set",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Nimble": {
      "main": [
        [
          {
            "node": "IF1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "MonkeyLearn": {
      "main": [
        [
          {
            "node": "Nimble1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF": {
      "main": [
        [],
        [
          {
            "node": "Nimble",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "IF1": {
      "main": [
        [],
        [
          {
            "node": "MonkeyLearn",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set": {
      "main": [
        [
          {
            "node": "Function",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Function": {
      "main": [
        [
          {
            "node": "IF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Cron1": {
      "main": [
        [
          {
            "node": "Twitter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {},
  "id": 1
}

Noticed that Nimble and MonkeyLearn are custom nodes. It’s possible that the execute method it’s not iterating per each item in the input? If that is the case, you can either update the nodes to iterate or use a splitbatches node after the Twitter node.

1 Like