{
  "name": "X (Twitter) Sentiment Monitor",
  "nodes": [
    {
      "parameters": {
        "options": {}
      },
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        -60,
        210
      ],
      "id": "f3fbcaa3-da8f-43a9-821c-2940c0e0d923",
      "name": "Loop Over Items"
    },
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "gpt-4o-mini",
          "mode": "list",
          "cachedResultName": "GPT-4O-MINI"
        },
        "messages": {
          "values": [
            {
              "content": "={{ $('Set Field for Loop').item.json.tweet_text }}"
            },
            {
              "content": "=You are a professional Twitter social media user replying to the original poster (admin).\n\nAnalyze the sentiment of the tweet below. If the sentiment is positive, write a short, thoughtful reply **from a user's perspective**, directed to the person who posted it.\n\nThe reply should:\n- Show appreciation, agreement, or excitement\n- Be friendly, intelligent, and relevant to tech or AI fields\n- Use no emojis\n- Be under 160 characters\n- Address the original poster directly as a real person would\n\nTweet:\n\"\"\"\nuse a user prompt\n\"\"\"",
              "role": "system"
            }
          ]
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 1.8,
      "position": [
        1380,
        -220
      ],
      "id": "d4708a65-329e-446d-92a2-e7e96b6e2cff",
      "name": "OpenAI",
      "credentials": {
        "openAiApi": {
          "id": "xYObejx0eIkwMS86",
          "name": "OpenAI account"
        }
      }
    },
    {
      "parameters": {
        "jsonSchemaExample": "{\n\"category\": \"neutral\"\n}"
      },
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1.2,
      "position": [
        1020,
        180
      ],
      "id": "3b42143b-e2f5-40d9-9320-293719ae4a9b",
      "name": "Structured Output Parser"
    },
    {
      "parameters": {
        "documentId": {
          "__rl": true,
          "value": "1sOxYQtX-O6p35FCNSRsovKEHL4w4GsfF-ylMVcwpb_E",
          "mode": "list",
          "cachedResultName": "Google Map Scraper"
        },
        "sheetName": {
          "__rl": true,
          "value": 1040262065,
          "mode": "list",
          "cachedResultName": "Twitter(hashtag)"
        },
        "filtersUI": {
          "values": [
            {
              "lookupColumn": "ID",
              "lookupValue": "={{ $json.id }}"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.6,
      "position": [
        160,
        -40
      ],
      "id": "cfe9c3da-b2f8-44cf-9fb3-54acb7fc181f",
      "name": "Get Post Data",
      "alwaysOutputData": true,
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "Pjxuoti0ywJ2OIzZ",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $('Loop Over Items').item.json.tweet_text }}",
        "hasOutputParser": true,
        "options": {
          "systemMessage": "You are a helpful AI assistant.Perfectly analyze the sentiment of this tweet_text and tell me in one word it is Positive, Neutral, or Negative."
        }
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 2,
      "position": [
        740,
        -80
      ],
      "id": "09218aff-1ed0-41a9-9eec-848ed8d5f1e6",
      "name": "Sentiment Analyst"
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.output.category }}",
                    "rightValue": "Positive",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "id": "394765f1-3f6b-484c-88ac-6eb699acd292"
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Positive"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "d102e916-73dc-4774-81eb-7ca7e6e1033e",
                    "leftValue": "={{ $json.output.category }}",
                    "rightValue": "Negative",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Negative"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "id": "9e383471-7b1b-4311-9fab-92ce4db0301f",
                    "leftValue": "={{ $json.output.category }}",
                    "rightValue": "Neutral",
                    "operator": {
                      "type": "string",
                      "operation": "equals",
                      "name": "filter.operator.equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Neutral"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        1180,
        -40
      ],
      "id": "dba73c62-a11a-4021-90ba-7a5e04221fe8",
      "name": "Switch According Analyst"
    },
    {
      "parameters": {
        "operation": "appendOrUpdate",
        "documentId": {
          "__rl": true,
          "value": "1sOxYQtX-O6p35FCNSRsovKEHL4w4GsfF-ylMVcwpb_E",
          "mode": "list",
          "cachedResultName": "Google Map Scraper"
        },
        "sheetName": {
          "__rl": true,
          "value": 1040262065,
          "mode": "list",
          "cachedResultName": "Twitter(hashtag)"
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Sentiment ": "={{ $('Switch According Analyst').item.json.output.category }}",
            "ID": "={{ $('Set Field for Loop').item.json.id }}",
            "Post Reply": "={{ $json.message.content }}",
            "TweetUrl": "={{ $('Set Field for Loop').item.json.tweet_url }}",
            "TweetText": "={{ $('Set Field for Loop').item.json.tweet_text }}"
          },
          "matchingColumns": [
            "ID"
          ],
          "schema": [
            {
              "id": "ID",
              "displayName": "ID",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "TweetUrl",
              "displayName": "TweetUrl",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "TweetText",
              "displayName": "TweetText",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Sentiment ",
              "displayName": "Sentiment ",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true
            },
            {
              "id": "Post Reply",
              "displayName": "Post Reply",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      },
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.6,
      "position": [
        1700,
        240
      ],
      "id": "eff7e798-b814-4f3f-9550-0ed4d417a5fd",
      "name": "Add Post Data",
      "alwaysOutputData": false,
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "Pjxuoti0ywJ2OIzZ",
          "name": "Google Sheets account"
        }
      }
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 6
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        -940,
        220
      ],
      "id": "a8db2310-960f-41f5-9268-c5405ed0c9e4",
      "name": "Schedule Trigger"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "56262d1d-f624-4e77-a03f-a0f2230070a6",
              "name": "id",
              "value": "={{ $json.postId }}",
              "type": "string"
            },
            {
              "id": "a32fc77b-7378-4ac4-a0af-193ae7092ae3",
              "name": "tweet_url",
              "value": "={{ $json.postUrl }}",
              "type": "string"
            },
            {
              "id": "f744541d-80db-46c6-91ad-3747ebac2c49",
              "name": "tweet_text",
              "value": "={{ $json.postText }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        -280,
        210
      ],
      "id": "87794a1d-336d-4ef7-8014-3f089030df9f",
      "name": "Set Field for Loop"
    },
    {
      "parameters": {
        "authentication": "oAuth2",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "value": "C090F70N52M",
          "mode": "list",
          "cachedResultName": "website-uptime"
        },
        "text": "=Received a Negative retweet on {{ $('Loop Over Items').item.json.tweet_url }}. Consider if a response or clarification is needed.",
        "otherOptions": {
          "includeLinkToWorkflow": false
        }
      },
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [
        1440,
        -60
      ],
      "id": "396aa178-3d97-42dd-b94f-19a9ea57cd22",
      "name": "Send negative post message on slack",
      "webhookId": "dd69ab59-93c0-47ff-ac16-eadbc85f6d79",
      "credentials": {
        "slackOAuth2Api": {
          "id": "0IILIwvVJ5kcTJNv",
          "name": "Slack account"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.apify.com/v2/acts/scraper_one~x-posts-search/run-sync?token=api_key",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n    \"query\": \"LaraconIn\",\n    \"resultsCount\": 5,\n    \"searchType\": \"latest\",\n    \"lang\":\"en\"\n}",
        "options": {}
      },
      "id": "bc936663-4e41-4900-b3f3-a94219afde11",
      "name": "Request for Twitter Post via Apify",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -720,
        220
      ],
      "typeVersion": 4.1,
      "onError": "continueErrorOutput"
    },
    {
      "parameters": {
        "url": "https://api.apify.com/v2/acts/scraper_one~x-posts-search/runs/last/dataset/items?token=api_key",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -500,
        200
      ],
      "id": "6ba08d23-94b8-4c8e-9153-786fe0468469",
      "name": "Get Requested Post from Apify"
    },
    {
      "parameters": {
        "content": "## Data Setup\nCreate your own spreadsheet with these tabs/columns, then select it in the Google Sheets nodes:\n- **Google Map Scraper → Twitter(hashtag)**: `ID`, `TweetUrl`, `TweetText`, `Sentiment`, `Post Reply`\n\n\n## Workflow Summary\nThis n8n workflow is designed to automate the collection, sentiment analysis, and response process for trending content. It uses the Apify API(https://apify.com/) to fetch trending data, performs sentiment analysis, and takes conditional actions based on the results. All outcomes are logged in a Google Sheet for record-keeping and future insights.\n\n## How to Use Apify API in n8n for Trending Data\n ### API Endpoint\n**Use this Apify Actor to fetch trending data via a POST request**\n*https://api.apify.com/v2/acts/scraper_one~x-posts-search/run-sync?token=YOUR_API_TOKEN*\nImportant: Replace YOUR_API_TOKEN with your actual Apify API token.\n\n### Request Body Format\n{\n  \"search\": \"YOUR TOPIC\", \n  \"mode\": \"latest\",\n  \"maxItems\": 10\n}",
        "height": 560,
        "width": 800
      },
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -900,
        -460
      ],
      "typeVersion": 1,
      "id": "721b78e6-cf67-43e5-85dc-7a0f8e4b0a4b",
      "name": "Sticky Note"
    },
    {
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini"
        },
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        760,
        140
      ],
      "id": "8c4e52be-5eca-4aaa-8867-33039ea8a565",
      "name": "OpenAI Chat Model",
      "credentials": {
        "openAiApi": {
          "id": "xYObejx0eIkwMS86",
          "name": "OpenAI account"
        }
      }
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "6f5c821d-8b4b-40ab-a025-1b7fcc42c78d",
              "leftValue": "={{ $json.ID }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        380,
        -40
      ],
      "id": "ddea38a2-a2a8-4917-b730-0185db028a8f",
      "name": "If Duplicate"
    }
  ],
  "pinData": {},
  "connections": {
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Get Post Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Sentiment Analyst",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI": {
      "main": [
        [
          {
            "node": "Add Post Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Post Data": {
      "main": [
        [
          {
            "node": "If Duplicate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sentiment Analyst": {
      "main": [
        [
          {
            "node": "Switch According Analyst",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Switch According Analyst": {
      "main": [
        [
          {
            "node": "OpenAI",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send negative post message on slack",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Add Post Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add Post Data": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Request for Twitter Post via Apify",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Field for Loop": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send negative post message on slack": {
      "main": [
        [
          {
            "node": "Add Post Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Request for Twitter Post via Apify": {
      "main": [
        [
          {
            "node": "Get Requested Post from Apify",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Requested Post from Apify": {
      "main": [
        [
          {
            "node": "Set Field for Loop",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Sentiment Analyst",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "If Duplicate": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Sentiment Analyst",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "830ebe46-55b7-4c1f-a2d0-94df84d44c6c",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "f1c26778ec36097a9722a22c47c5e9c01f00b8862dadd0121ec118026a370fa6"
  },
  "id": "KDjtEAri2OHZzLLa",
  "tags": []
}