Step-by-Step Tutorial: How to Post to X-Twitter Using Agentic AI and n8n
Based on Alex, PhD AI's video on YouTube. If you like this content, support the original creators by watching, liking and subscribing to their content.
Collect topic, optional reference link, and optional keywords/hashtags via a form to seed content generation.
Briefing
A step-by-step workflow ties together a web form, agentic text generation, email-based human approval, and automated posting to X (Twitter). The core idea is simple: collect a topic (plus optional keywords/hashtags and a reference link), generate platform-specific tweet copy in a structured format, send it to the user via email for approval or rejection, and—only after approval—publish the final post to X using the Twitter API.
The build starts with a form (“my form”) that gathers three inputs: a required “topic” field, an optional “link to relevant materials,” and optional “keywords or hashtags.” Submitting the form triggers the workflow and returns the collected values to downstream nodes. A test submission confirms the form is wired correctly before any automation logic is added.
Next comes the “social media content factory” agent, whose prompt is treated as the critical control layer. It’s instructed to produce concise, crisp, Twitter-optimized content (including SEO/hashtag strategy and engagement prompts) and to keep each tweet to 150 characters or less. The agent must output a specific JSON schema: for Twitter it returns the tweet text (“post”), an array of hashtags, plus suggestions for “video” and “image” (not automated yet). The workflow also includes a structured output parser to enforce that JSON shape, and an OpenAI chat model to generate the content.
A second agent then converts the generated JSON into a clean, modern HTML email. The email is table-based, includes a card per platform (with title, post, video/image suggestions, hashtag list, emoji, and a platform-specific call to action), and strips out code blocks and comments. The workflow uses a Gmail “send and wait” node to email the user an approval request, with a subject and message populated from the generated content.
The email response drives the decision point: the workflow waits for an approval action (approve vs disapprove/decline) with a 45-minute time limit. If approved, an “if” node routes the data to a Twitter “create tweet” action; if declined, nothing gets posted.
Publishing to X requires setting up a Twitter developer app (web app/automated app/bot), enabling read/write permissions and direct messages, and providing OAuth callback and website URL values. The workflow uses the Twitter node to post the approved tweet text to X.
A live test uses a topic based on OpenAI’s Codex release, along with keywords like “Codex,” “open AI,” “chat GPT,” “code generation,” and “agentic AI.” The email arrives with the proposed tweet and approve/decline buttons. After approval, the workflow posts an adapted tweet to X within seconds, including the suggested hashtags. The build ends by pointing to future expansions—threads and additional platforms like Instagram and Facebook—using the same agentic generation and approval pattern.
Cornell Notes
The workflow automates X posting by combining form inputs, agentic content generation, structured JSON output, and a human approval gate. A “social media content factory” agent uses a carefully designed prompt to generate Twitter-ready copy (≤150 characters) plus hashtags and media suggestions, returning results in a strict JSON schema. A second agent turns that JSON into a modern HTML email, which is sent via Gmail with approve/decline buttons. Only when the email response is approved does the workflow publish the tweet to X using a configured Twitter developer app and the Twitter API. This approval step prevents unwanted posts while still speeding up content creation.
How does the workflow ensure the tweet text and hashtags are usable for posting to X?
Why is the email approval step central to the pipeline’s reliability?
What role does the second agent play after the JSON is generated?
How are Twitter API credentials and app settings used in the posting step?
What inputs drive the content generation in the form?
Review Questions
- What specific JSON fields does the structured output parser require for Twitter, and how do those fields map to what gets posted?
- How does the workflow decide whether to post to X after the email is sent, and what is the time limit for that decision?
- Which prompt constraints are used to keep tweets Twitter-appropriate (e.g., length and engagement style), and where do the hashtags come from?
Key Points
- 1
Collect topic, optional reference link, and optional keywords/hashtags via a form to seed content generation.
- 2
Use a “social media content factory” agent with a strict prompt and a structured output parser to produce Twitter-ready JSON (tweet text, hashtag array, media suggestions).
- 3
Generate a clean, table-based HTML approval email from the JSON using a second agent, and send it through Gmail with a wait-for-response step.
- 4
Gate posting with human approval: only an “approve” response triggers the Twitter create-tweet action; declines stop the workflow.
- 5
Configure a Twitter developer app with read/write permissions and correct OAuth callback/website URL values, then connect credentials to the Twitter node.
- 6
Enforce tweet length and engagement requirements in the generation prompt (e.g., ≤150 characters and a call to action).
- 7
Test end-to-end with a real topic and reference link to confirm the email arrives correctly and the approved tweet posts to X.