{
  "id": "GHDkm5yryfu6FoXN",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Twitter Monitoring Workflow",
  "tags": [],
  "nodes": [
    {
      "id": "8fa04bf9-68e5-43b7-81f2-ee5f2355abd9",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -60,
        210
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "dec03e00-a5fa-44eb-b269-b486259ac1aa",
      "name": "OpenAI",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        1380,
        -220
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini",
          "cachedResultName": "GPT-4O-MINI"
        },
        "options": {},
        "messages": {
          "values": [
            {
              "content": "={{ $('Set Field for Loop').item.json.tweet_text }}"
            },
            {
              "role": "system",
              "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\"\"\""
            }
          ]
        }
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.8
    },
    {
      "id": "a6ebd6b7-29a4-4d02-ab90-5e74235485ea",
      "name": "Structured Output Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        1020,
        180
      ],
      "parameters": {
        "jsonSchemaExample": "{\n\"category\": \"neutral\"\n}"
      },
      "typeVersion": 1.2
    },
    {
      "id": "c725e544-b9c4-4b5f-9e48-3e4ca1e8aec1",
      "name": "Get Post Data",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        160,
        -40
      ],
      "parameters": {
        "options": {},
        "filtersUI": {
          "values": [
            {
              "lookupValue": "={{ $json.id }}",
              "lookupColumn": "ID"
            }
          ]
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 1040262065,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1sOxYQtX-O6p35FCNSRsovKEHL4w4GsfF-ylMVcwpb_E/edit#gid=1040262065",
          "cachedResultName": "Twitter(hashtag)"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1sOxYQtX-O6p35FCNSRsovKEHL4w4GsfF-ylMVcwpb_E",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1sOxYQtX-O6p35FCNSRsovKEHL4w4GsfF-ylMVcwpb_E/edit?usp=drivesdk",
          "cachedResultName": "Google Map Scraper"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.6,
      "alwaysOutputData": true
    },
    {
      "id": "1def5fb1-e928-4c9e-bb7a-4f1ea5658d46",
      "name": "Sentiment Analyst",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        740,
        -80
      ],
      "parameters": {
        "text": "={{ $('Loop Over Items').item.json.tweet_text }}",
        "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."
        },
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 2
    },
    {
      "id": "bc3fdb44-6588-4607-8e22-27a9bdf9ce9c",
      "name": "Switch According Analyst",
      "type": "n8n-nodes-base.switch",
      "position": [
        1180,
        -40
      ],
      "parameters": {
        "rules": {
          "values": [
            {
              "outputKey": "Positive",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "394765f1-3f6b-484c-88ac-6eb699acd292",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.category }}",
                    "rightValue": "Positive"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Nagative",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "d102e916-73dc-4774-81eb-7ca7e6e1033e",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.category }}",
                    "rightValue": "Negative"
                  }
                ]
              },
              "renameOutput": true
            },
            {
              "outputKey": "Neutral",
              "conditions": {
                "options": {
                  "version": 2,
                  "leftValue": "",
                  "caseSensitive": true,
                  "typeValidation": "strict"
                },
                "combinator": "and",
                "conditions": [
                  {
                    "id": "9e383471-7b1b-4311-9fab-92ce4db0301f",
                    "operator": {
                      "name": "filter.operator.equals",
                      "type": "string",
                      "operation": "equals"
                    },
                    "leftValue": "={{ $json.output.category }}",
                    "rightValue": "Neutral"
                  }
                ]
              },
              "renameOutput": true
            }
          ]
        },
        "options": {}
      },
      "typeVersion": 3.2
    },
    {
      "id": "ce1e35b7-df4a-4969-81ed-21d8967cc834",
      "name": "Add Post Data",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1700,
        240
      ],
      "parameters": {
        "columns": {
          "value": {
            "ID": "={{ $('Set Field for Loop').item.json.id }}",
            "TweetUrl": "={{ $('Set Field for Loop').item.json.tweet_url }}",
            "TweetText": "={{ $('Set Field for Loop').item.json.tweet_text }}",
            "Post Reply": "={{ $json.message.content }}",
            "Sentiment ": "={{ $('Switch According Analyst').item.json.output.category }}"
          },
          "schema": [
            {
              "id": "ID",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "ID",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "TweetUrl",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "TweetUrl",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "TweetText",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "TweetText",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Sentiment ",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Sentiment ",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Post Reply",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Post Reply",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "ID"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "appendOrUpdate",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 1040262065,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1sOxYQtX-O6p35FCNSRsovKEHL4w4GsfF-ylMVcwpb_E/edit#gid=1040262065",
          "cachedResultName": "Twitter(hashtag)"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1sOxYQtX-O6p35FCNSRsovKEHL4w4GsfF-ylMVcwpb_E",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1sOxYQtX-O6p35FCNSRsovKEHL4w4GsfF-ylMVcwpb_E/edit?usp=drivesdk",
          "cachedResultName": "Google Map Scraper"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.6,
      "alwaysOutputData": false
    },
    {
      "id": "0ac588c7-2612-482e-91c2-e2622840ec5e",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        -940,
        220
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours",
              "hoursInterval": 6
            }
          ]
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "b84b9a8a-e13e-41bf-96d0-aeb36c13c4be",
      "name": "Set Field for Loop",
      "type": "n8n-nodes-base.set",
      "position": [
        -280,
        210
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "56262d1d-f624-4e77-a03f-a0f2230070a6",
              "name": "id",
              "type": "string",
              "value": "={{ $json.postId }}"
            },
            {
              "id": "a32fc77b-7378-4ac4-a0af-193ae7092ae3",
              "name": "tweet_url",
              "type": "string",
              "value": "={{ $json.postUrl }}"
            },
            {
              "id": "f744541d-80db-46c6-91ad-3747ebac2c49",
              "name": "tweet_text",
              "type": "string",
              "value": "={{ $json.postText }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "3e8a8b36-69c8-42dd-86c2-fdda0259184c",
      "name": "Send negative post message on slack",
      "type": "n8n-nodes-base.slack",
      "position": [
        1440,
        -60
      ],
      "parameters": {
        "text": "=Received a Negative retweet on {{ $('Check Duplicate').item.json.TweetUrl }}. Consider if a response or clarification is needed.",
        "select": "channel",
        "channelId": {
          "__rl": true,
          "mode": "list",
          "value": "C090F70N52M",
          "cachedResultName": "website-uptime"
        },
        "otherOptions": {
          "includeLinkToWorkflow": false
        },
        "authentication": "oAuth2"
      },
      "credentials": {
        "slackOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "31469921-4053-435f-9909-ab5a7eb85204",
      "name": "Request for Twitter Post via Apify",
      "type": "n8n-nodes-base.httpRequest",
      "onError": "continueErrorOutput",
      "position": [
        -720,
        220
      ],
      "parameters": {
        "url": "https://api.apify.com/v2/acts/scraper_one~x-posts-search/run-sync?token=YOUR_TOKEN_HERE",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n    \"query\": \"LaraconIn\",\n    \"resultsCount\": 5,\n    \"searchType\": \"latest\",\n    \"lang\":\"en\"\n}",
        "sendBody": true,
        "sendHeaders": true,
        "specifyBody": "json",
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        }
      },
      "typeVersion": 4.1
    },
    {
      "id": "10774b92-a390-43de-a7a5-62ce25587e3e",
      "name": "Get Requested Post from Apify",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -500,
        200
      ],
      "parameters": {
        "url": "https://api.apify.com/v2/acts/scraper_one~x-posts-search/runs/last/dataset/items?token=YOUR_TOKEN_HERE",
        "options": {}
      },
      "typeVersion": 4.2
    },
    {
      "id": "31525bf2-922d-413e-bad7-9ea8b9099880",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -900,
        -460
      ],
      "parameters": {
        "width": 800,
        "height": 560,
        "content": "## Sample Output Sheet\n- https://docs.google.com/spreadsheets/d/1sOxYQtX-O6p35FCNSRsovKEHL4w4GsfF-ylMVcwpb_E/edit?usp=sharing\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}"
      },
      "typeVersion": 1
    },
    {
      "id": "29960d67-1378-49b3-9174-ac26aef71ad9",
      "name": "OpenAI Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        760,
        140
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "8bd4d3f7-9020-43c6-bf0e-788075c5ec6f",
      "name": "If Duplicate",
      "type": "n8n-nodes-base.if",
      "position": [
        380,
        -40
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "6f5c821d-8b4b-40ab-a025-1b7fcc42c78d",
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              },
              "leftValue": "={{ $json.ID }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "09a99040-3fec-411f-a270-2f73092e0c8d",
  "connections": {
    "OpenAI": {
      "main": [
        [
          {
            "node": "Add Post Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Duplicate": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Sentiment Analyst",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add Post Data": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Post Data": {
      "main": [
        [
          {
            "node": "If Duplicate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Get Post Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Request for Twitter Post via Apify",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Sentiment Analyst",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Sentiment Analyst": {
      "main": [
        [
          {
            "node": "Switch According Analyst",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Field for Loop": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Sentiment Analyst",
            "type": "ai_outputParser",
            "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
          }
        ]
      ]
    },
    "Get Requested Post from Apify": {
      "main": [
        [
          {
            "node": "Set Field for Loop",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Request for Twitter Post via Apify": {
      "main": [
        [
          {
            "node": "Get Requested Post from Apify",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send negative post message on slack": {
      "main": [
        [
          {
            "node": "Add Post Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}