AI-driven social media content automation system built with LangGraph.
- AI Post Generation: Generate engaging social media posts using LLM with human-in-the-loop review
- Multi-Platform Publishing: Publish to Twitter and LinkedIn simultaneously
- URL Deduplication: Prevent duplicate posts using LangGraph Store
- Content Extraction: Automatically extract page content, relevant links, and images from URLs
- Flexible Scheduling: Schedule posts with natural language date parsing (e.g., "tomorrow at 2pm")
- Content Quality Control: Automatic post condensation to meet platform requirements
- Full Observability: LangSmith integration for tracing and debugging
The generate_post graph is the core pipeline that transforms input links into social media posts:
START
│
▼
┌─────────────┐
│ checkUrls │ ─────────► END (all duplicates)
│ (去重检查) │
└─────────────┘
│
▼
┌─────────────┐
│ verifyLinks │
│ (内容提取) │
└─────────────┘
│
▼
┌──────────────┐
│generateReport│
│ (生成报告) │
└──────────────┘
│
▼
┌─────────────┐
│ generatePost│
│ (生成帖子) │
└─────────────┘
│
┌─────────┴─────────┐
▼ ▼
condensePost ──────► humanReview ◄───────┐
(压缩帖子) (人工审核) │
│ │
┌────────┼────────┐ │
▼ ▼ ▼ │
schedulePost rewritePost update│
(发布/排期) (重写帖子) (更新时间)
│ │ │ │
▼ └────────┴─────┘
END
Phase 0: URL Deduplication
checkUrls: Filters out URLs that have already been used for post generation (via LangGraph Store). If all URLs are duplicates, ends the graph early.
Phase 1: Content Extraction & Generation
verifyLinks: Invokes verify-links sub-graph to extract page contents, relevant links, and imagesgenerateReport: Analyzes content and generates a marketing report as contextgeneratePost: Creates engaging social media post based on the reportcondensePost: Shortens posts if needed (max 3 attempts)
Phase 2: Human Review (Interrupt)
humanReview: Human-in-the-loop interrupt for post reviewrewritePost: Rewrites post based on user feedbackupdateScheduleDate: Parses and validates new schedule dateschedulePost: Publishes to Twitter/LinkedIn or schedules for later
| Graph | Description |
|---|---|
generate_post |
Core pipeline: URL deduplication → content extraction → post generation → human review → publishing |
verify_links |
Sub-graph: Extracts page contents, relevant links, and images from URLs (invoked by verifyLinks node) |
upload_post |
Sub-graph: Validates and publishes posts to Twitter/LinkedIn with authentication handling |
- Node.js >= 20.0.0
- npm or yarn
# Clone the repository
git clone https://github.com/maisieyang/social-media-agent-from-scratch.git
cd social-media-agent-from-scratch
# Install dependencies
npm install
# Copy environment template
cp .env.example .envEdit .env with your credentials:
# Required: LLM Configuration
DASHSCOPE_API_KEY=your_api_key
DEFAULT_LLM_MODEL=qwen3-max
# Optional: LangSmith Observability
LANGSMITH_API_KEY=your_langsmith_key
LANGSMITH_TRACING=true
# Social Media Platforms (at least one)
TWITTER_APP_KEY=...
TWITTER_APP_SECRET=...
TWITTER_ACCESS_TOKEN=...
TWITTER_ACCESS_SECRET=...
# Or LinkedIn
LINKEDIN_ACCESS_TOKEN=...
LINKEDIN_PERSON_URN=...# Start LangGraph development server
npm run langgraph:dev
# Or run locally without server
npm run run:local -- --graph generate_post --links "https://github.com/user/repo"Start the development server and open LangGraph Studio:
npm run langgraph:dev# Generate post from URL
npm run run:generate -- --links "https://github.com/example/repo"
# Run with dry-run mode (no actual posting)
npm run run:local -- --graph generate_post --links "https://..." --dry-run# Run via LangGraph API (requires running langgraph:dev first)
npm run cron -- --links "https://github.com/user/repo"
# Crontab example: Run daily at 8 AM
0 8 * * * cd /path/to/project && npm run cron -- --links "https://example.com"# Run all tests
npm test
# Run with coverage
npm test -- --coverage
# Run specific test file
npm test -- src/tests/unit/date-parser.test.ts
# Watch mode
npm test -- --watchsrc/tests/
├── setup.ts # Test configuration
├── unit/
│ ├── date-parser.test.ts # Date parsing tests (29)
│ └── url-utils.test.ts # URL utility tests (40)
├── integration/
│ ├── nodes.test.ts # Node tests (13)
│ └── graph.test.ts # Graph tests (12)
└── evaluation/
└── content-quality.test.ts # Quality metrics (21)
# Build and run
docker-compose up langgraph-api
# Development mode
docker-compose --profile dev up langgraph-dev
# With full stack (Redis + PostgreSQL)
docker-compose --profile full up- Push to GitHub
- Connect repository in LangGraph Cloud
- Deploy with
langgraph.jsonconfiguration
.
├── src/
│ ├── agents/
│ │ ├── generate-post/ # Core: Post generation graph
│ │ │ ├── graph.ts # Graph definition
│ │ │ ├── state.ts # State annotations
│ │ │ ├── constants.ts # Status constants
│ │ │ ├── nodes/ # Graph nodes
│ │ │ │ ├── check-urls.ts # URL deduplication
│ │ │ │ ├── verify-links.ts # Content extraction
│ │ │ │ ├── generate-report.ts # Report generation
│ │ │ │ ├── generate-post.ts # Post generation
│ │ │ │ ├── condense-post.ts # Post condensation
│ │ │ │ ├── human-node.ts # Human review interrupt
│ │ │ │ ├── rewrite-post.ts # Post rewriting
│ │ │ │ ├── schedule-post.ts # Publishing/scheduling
│ │ │ │ └── routing.ts # Conditional routing
│ │ │ └── prompts/ # LLM prompts
│ │ ├── verify-links/ # Sub-graph: URL verification & content extraction
│ │ ├── upload-post/ # Sub-graph: Post validation & publishing
│ │ └── shared/ # Shared utilities
│ ├── clients/ # Platform clients
│ │ ├── twitter/
│ │ └── linkedin/
│ ├── utils/ # Utilities
│ └── tests/ # Test suites
├── scripts/
│ ├── cron-runner.ts # Scheduled task runner
│ └── run-local.ts # Local execution
├── langgraph.json # LangGraph configuration
├── Dockerfile # Container definition
└── docker-compose.yml # Service orchestration
The generate_post graph supports human review before publishing:
简化字符串输入:
accept/approve/ok/yes:接受当前内容并继续发布流程ignore/discard/skip/no:放弃该帖子并结束- 任意其他字符串:作为反馈进入重写流程
JSON 格式输入:
{"type":"accept"}:接受当前内容并继续发布流程{"type":"ignore"}:放弃该帖子并结束{"type":"edit","args":{"feedback":"把语气更正式,压缩到 240 字"}}:进入重写流程,feedback作为改写指令{"type":"respond","args":{"scheduleDate":"tomorrow 9am"}}:进入排期更新流程(也支持date字段){"type":"respond","args":{"post":"改成这段文案..."}}:进入重写流程,使用你给的文案作为输入(也支持content/text字段)
| Variable | Required | Description |
|---|---|---|
DASHSCOPE_API_KEY |
Yes | LLM API key |
DEFAULT_LLM_MODEL |
No | Model name (default: qwen3-max) |
LANGSMITH_API_KEY |
No | LangSmith tracing |
TWITTER_* |
No | Twitter OAuth credentials |
LINKEDIN_* |
No | LinkedIn OAuth credentials |
FIRECRAWL_API_KEY |
No | Web scraping service |
GITHUB_TOKEN |
No | GitHub API access |
MIT