Up to 3 attempts with exponential backoff for 429, 502-504, and common network errors before surfacing publish failure. Made-with: Cursor
AI News Writer
Autonomous news writing system for BitReport. Researches news from trusted sources, detects topics, and writes complete articles like a professional journalist — then exports to WordPress.
Paperclip issue: BIT-353
Gitea: https://git.bitreport.pt/BitReport.pt/ai-news-writer
Dev container: ai-news-writer-dev (planned CTID 223, VLAN 1050 — created only after you run tools/proxmox/create_ai_news_writer_lxc.sh on a Proxmox node)
Domain: ainews.bitreport.pt (NPM configured manually)
Quick start
cp .env.example .env
# fill OPENAI_API_KEY in .env
npm install
node src/index.js write "FC Porto match result"
Commands
| Command | Description |
|---|---|
npm run write |
Write one article (manual mode); merges RSS + X when social.json + bearer are set |
npm run write -- "topic" |
Write article about specific topic |
npm run listen |
Start auto RSS listener (cron) |
npm run pm2:start |
PM2 via ecosystem.config.cjs (requires global pm2) |
npm run verify:deploy -- <url> |
Smoke-test GET /health (expects listen mode); needs curl + node |
npm run wp:test |
GET /wp-json/wp/v2/users/me — validates every config/sites/*.json (excl. example), or WP_* env if no site files |
npm run rss:test |
Probe each RSS URL in config/sources.json (3 attempts, backoff); stdout { "ok", "feeds" }; exit 1 if any feed fails |
npm run x:test |
X API v2 recent search for each x.queries[] in config/social.json; needs TWITTER_BEARER_TOKEN |
npm run ops:check |
rss:test always; wp:test only if config/sites/*.json (≠ example) or WP_URL+WP_USER+WP_PASSWORD |
npm start |
Same as write |
npm run dev |
Auto-restart with nodemon |
Architecture
RSS Feeds / News Sites
↓
src/crawler/rss.js — fetch + normalize RSS articles
src/crawler/merge-sources.js — RSS + optional X, sorted
src/crawler/scraper.js — scrape full article content
↓
src/writer/ai.js — OpenAI journalist AI
↓
src/publisher/wordpress.js — WP REST API export (retries on 429 / 5xx / transient network)
↓
WordPress (draft/publish)
Listener / auto mode:
src/listener/rss-listener.js — cron-based RSS polling (+ optional X recent search)
↓
detectTopics() → generateArticle() → publishToWordPress()
X (Twitter) — optional: copy config/social.example.json → config/social.json, set TWITTER_BEARER_TOKEN in .env. In listen mode, tweets are merged with RSS before keyword filtering. Uses X API v2 recent search (plan limits apply).
Configuration
Sources (config/sources.json)
Edit to add/remove RSS feeds, keywords, and trusted domains.
npm run rss:test walks every rss[] entry with the same retry logic as the crawler; stdout is { "ok": boolean, "feeds": [ { "ok", "name", "url", "item_count", "error" } ] }.
Default URLs are chosen for stability; publishers change endpoints often — if probes fail, swap the entry for a working RSS URL and re-run rss:test.
Social — X (config/social.json, gitignored)
Copy config/social.example.json → config/social.json. Define x.queries (recent-search strings, max 512 chars each per X rules) and optional max_results_per_query (10–100). Requires TWITTER_BEARER_TOKEN. Run npm run x:test to validate credentials and queries.
Sites (config/sites/*.json)
One JSON file per WordPress target site. Copy config/sites/example.json.
Credentials come from your vault — never commit real passwords.
npm run wp:test checks each site file (or env fallback if config/sites/ has no JSON except example); stdout is one JSON object { "ok": boolean, "sites": [ ... ] } and the process exits non-zero if any check fails.
{
"name": "sports news",
"wp_url": "https://wp.newsproject.bitreport.pt",
"user": "wp-api-user",
"password": "application-password-from-vault",
"category_default": 5,
"status_default": "draft",
"lang": "pt"
}
Environment variables (.env)
| Variable | Purpose |
|---|---|
OPENAI_API_KEY |
OpenAI API key |
AI_MODEL |
Model (default: gpt-4o) |
HOST |
HTTP bind address in listen mode (default: 0.0.0.0) |
PORT |
HTTP port in listen mode (default: 3200) |
WP_URL |
WordPress base URL (fallback) |
WP_USER |
WP application username |
WP_PASSWORD |
WP application password |
MODE |
auto or manual |
AUTO_CRON |
Cron expression for auto mode |
LOG_LEVEL |
debug, info, warn, error |
HTTP_USER_AGENT |
Sent to RSS hosts and article scraper (identify your deployment) |
RSS_TIMEOUT_MS |
Per-feed HTTP timeout for rss-parser (default 10000, max 120000) |
RSS_FETCH_CONCURRENCY |
Parallel RSS fetches in fetchAllFeeds (default 3, max 20) |
SCRAPER_TIMEOUT_MS |
Article scrape timeout (default 12000, max 120000) |
TWITTER_BEARER_TOKEN |
X API v2 bearer (optional; required when using social.json + listen mode) |
Article output format
{
"title": "...",
"subtitle": "...",
"slug": "url-safe-slug",
"body": "<p>...</p>",
"summary": "...",
"tags": ["...", "..."],
"category": "sports",
"seo_title": "...",
"seo_description": "..."
}
Deploy (LXC dev container)
The public URL (ainews.bitreport.pt) expects something listening on PORT (default 3200) on the LXC. In listen mode the app starts a small HTTP server on HOST:PORT (HOST defaults to 0.0.0.0) so NPM can proxy to it. npm start / write does not start HTTP — use listen for long-running + NPM.
Was the LXC actually created?
Nothing in this repository creates Proxmox containers by itself. The bash script must be executed as root on a Proxmox node. If CT 223 (or ai-news-writer-dev) does not appear in pct list, the container was never provisioned.
(Nota PT: o domínio só responde se o CT existir, o Node estiver em modo listen na porta 3200, e o NPM apontar para o IP real desse CT — não assumir 10.10.50.23 sem confirmar no Proxmox.)
# --- On the Proxmox host (SSH as root) — create CT if missing ---
pct list | grep -E '223|ai-news-writer'
# If no row, from a clone of this repo:
bash tools/proxmox/create_ai_news_writer_lxc.sh
# Confirm guest IP for NPM (example VMID 223)
pct exec 223 -- ip -4 addr show eth0
# --- Inside the CT ---
git clone https://git.bitreport.pt/BitReport.pt/ai-news-writer.git /srv/ai-news-writer
cd /srv/ai-news-writer
npm install
cp .env.example .env
# edit .env: OPENAI_API_KEY, HOST=0.0.0.0, PORT=3200
# pm2 delete ai-news-writer # only if replacing an old one-shot / wrong-args process
npm run ops:check
pm2 start ecosystem.config.cjs
pm2 save && pm2 startup
(Alternative, easier to mistype: pm2 start src/index.js --name ai-news-writer --cwd /srv/ai-news-writer --time -- listen — the -- before listen is required.)
NPM (CT 102): Proxy Host ainews.bitreport.pt → http://<lxc-ip>:3200 (forward scheme http unless you terminate TLS only at NPM). Quick check from another machine: curl -sS http://<lxc-ip>:3200/health — JSON includes version, mode: "listen", and non-secret flags: rss_feed_count, x_listening (bearer + social.json queries), wp_credentials_configured.
Structured check (exit code 0/1): from a machine with curl + node (e.g. the CT or your laptop):
./tools/verify-deploy.sh "http://<lxc-ip>:3200"
# or: npm run verify:deploy -- "http://<lxc-ip>:3200"
Typo check: the hostname must be ainews.bitreport.pt (not ainews.bitrepor.pt).
Future
- Multi-language output
- Auto-schedule & publish
- Image generation (DALL-E / Stable Diffusion)
- SEO scoring
- Telegram / Discord social listening
- Web dashboard (approve / edit / publish UI)