Automated Twitter Trend Analysis and Posting with GPT and Database Logging - n8n Workflow

Use this powerful n8n workflow to automatically identify trending topics using AI, generate summaries via OpenAI, post to Twitter, log activities in MySQL, and notify Slack. Boost your content strategy with these n8n templates.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Content Strategists needing timely social media posts.
Marketing Teams looking for efficient Twitter automation.
Developers seeking advanced n8n templates integrating Langchain AI Agents, databases, and social media APIs.
Users familiar with n8n nodes and MySQL for robust workflow tracking.

Overview

Maintaining a relevant and active social media presence requires constant monitoring of trending topics. This comprehensive n8n workflow solves the problem of manual trend identification and content creation by fully automating the process every two hours. It begins with an n8n trigger (Schedule Trigger) that initiates a highly intelligent AI Agent to select the most relevant, brand-safe U.S. trend while suppressing recently published topics via a MySQL database look-up. A second AI Agent then generates a compelling summary using OpenAI's capabilities. The final steps ensure the content is posted to Twitter, logged accurately in the database for compliance and history, and reported instantly via Slack. This advanced n8n template demonstrates powerful orchestration between custom code, advanced AI agents, and critical business systems, providing a fully integrated solution.

How it Works

This automation is initiated by the Schedule Trigger, a core n8n trigger set to run every two hours.


  1. Exclusion Check: The first n8n node executes a MySQL query to retrieve a list of recently published keywords (3-day window) to prevent redundant posting. This exclusion list is passed to the AI Preprocessor.

  2. Trend Identification: The AI Agent Preprocess n8n node, leveraging an OpenAI Chat Model and a custom tool (MCP Client), analyzes US trends, scores them based on engagement potential, filters against the exclusion list, and selects the single best trend name.

  3. Data Preparation: A Function n8n node (Format Results1) standardizes the AI output for downstream processing, ensuring clean data structure.

  4. Summary Generation: The primary AI Agent n8n node takes the trend name, searches recent tweets via the MCP Client, and uses the OpenAI model to synthesize a concise, 30–60 word summary explaining why the topic is currently trending. It outputs a structured JSON object containing the trend and summary.

  5. Metadata and Canonization: The keywordformatter Code n8n node processes the AI output, calculates a standardized, canonical form of the trend name, and prepares the full metadata payload.

  6. Database Logging: A MySQL n8n node executes a SQL query to insert or update the trend details (including the canonical form, summary, and publish timestamp) into the keywordregistry table. It crucially sets a 3-day lockout period for future runs of this n8n workflow.

  7. Notification: A Slack n8n node sends the final summary to a specific channel, providing immediate visibility of the published content.

  8. Publication: Finally, the Twitter n8n node (Create Tweet) uses the generated summary and trend name to post the update to the designated X (Twitter) account, completing the cycle of this robust n8n workflow.

Installation Guide

To deploy this n8n workflow, follow these steps:


  1. Import: Copy the entire JSON code and import it into your n8n instance using the 'New' menu -> 'Import from JSON'.

  2. Credentials Setup: You must configure the following credentials for the respective n8n node types:

MySQL: Configure credentials for Execute a SQL query and Execute a SQL query1 to connect to your database for logging and exclusion checks.
OpenAI API: Set up the credentials for the two OpenAI Chat Model n8n nodes (named 'Cooper Openai' in the JSON). Ensure your key supports the models specified (e.g., gpt-5-mini).
Header Auth: Configure the HTTP Header Auth credential for the two MCP Client n8n nodes, which are essential for accessing the custom Twitter search tools.
Slack: Configure OAuth2 credentials for the Send a message n8n node and verify the #mcp-hub-test channel ID is correct.
* Twitter/X: Configure OAuth2 credentials for the Create Tweet n8n node to allow posting.

  1. Activation: Once all credentials are linked correctly, activate the n8n workflow via the toggle switch to enable the two-hourly schedule trigger.

Node Details

Schedule Trigger: This is the initial n8n trigger, configured to run every 2 hours at minute 12.
Execute a SQL query1 (MySQL n8n node): Function is to retrieve recently published trends (within 3 days) to create an exclusion list, preventing content repetition.
AI Agent Preprocess (n8n node): Key Configuration uses WOEID 23424977 (United States) to find trends. It applies filtering rules based on the MySQL exclusion list and scores items for engagement potential.
AI Agent (n8n node): The core content generator. It is configured to call the aigeon-ai-twitter154.searchtweets tool and synthesize a 30-60 word explanation of the trend, outputting a strict JSON payload.
keywordformatter (Code n8n node): A critical n8n node for data sanitization. It calculates the canon (canonical) form of the trend name by normalizing and cleaning the string, ensuring consistent database records.
Execute a SQL query (MySQL n8n node): Logs the published content, status, and sets the nexteligibleat timestamp (3 days from now) using prepared statements to prevent SQL injection.
Send a message (Slack n8n node): Provides immediate operational feedback by sending the generated summary to the designated Slack channel.
Create Tweet (Twitter n8n node): The final action, posting the combination of the trend name and the AI-generated summary.

Related n8n Workflows

Free

Nodes: 11 Nodes
Updated: December 26 2025
View all
Created by

Developer of mcphub.com. Use mcp to automate LLM tool call!

Featured*