Twitter Manager (twittermgr)

Overview

A web-based Twitter/X management tool for managing multiple accounts and scheduling posts. Evolving from a simple Flask poster into a full-featured FastAPI application with Telegram bot integration, AI-powered tweet rewriting, and automated content sourcing via Apify.

Tech Stack

  • Backend: Python, FastAPI (primary), Flask (legacy, migrating away)
  • Frontend: Jinja2 HTML templates
  • Bot Integration: Telegram Bot API (webhook-based)
  • AI/LLM: OpenAI-compatible API (OpenRouter/OpenAI) for tweet rewriting
  • Data Sources: Apify actors (Twitter scraping)
  • State/Queue: Redis (chat state), Celery (planned scheduler)
  • Deployment: Coolify (manages env vars via API, planned persistent volumes)
  • Storage: JSON file (data/last_posts.json) for last-post tracking

Key Features

  • Post to multiple Twitter accounts simultaneously
  • Reply to existing tweets
  • Schedule posts (immediate, delayed, or specific time)
  • Image upload support
  • Telegram bot with inline keyboard workflow: category → candidates → pick → rewrite? → account → confirm → post
  • LLM-based tweet rewriter with fallback heuristic
  • Apify-powered tweet fetching and candidate sourcing
  • Last-post tracker per account
  • Account management via Coolify API (add/remove Twitter accounts, restart app)

Project Structure

src/twitter_manager/
├── api.py                 # FastAPI app, routes, Telegram webhook (primary)
├── app.py                 # Flask app (legacy, being migrated)
├── apify_client.py        # Apify API client for Twitter search
├── coolify_client.py      # Coolify API client for env vars
├── rewriter.py            # LLM-based tweet rewriter
├── state.py               # Redis-backed Telegram state management
├── telegram.py            # Telegram bot helpers
├── tracker.py             # Last-post tracking (JSON file)
└── templates/
    ├── index.html         # Main dashboard
    ├── login.html         # Login page
    └── accounts.html      # Account management
data/
└── last_posts.json        # Post tracker data

API Endpoints

EndpointMethodPurpose
/api/telegram/webhookPOSTTelegram bot webhook
/api/accountsGETList accounts from Coolify
/api/accounts/twitterPOSTAdd Twitter account
/api/accounts/twitter/{num}DELETERemove account
/api/accounts/restartPOSTRestart app to apply changes
/api/rewritePOSTRewrite tweet text via LLM
/api/post/simplePOSTPost tweet immediately
/api/tracker/lastGETRead tracker state

Environment Variables

  • TWITTER_API_KEY_N, TWITTER_API_SECRET_N, TWITTER_ACCESS_TOKEN_N, TWITTER_ACCESS_TOKEN_SECRET_N — per-account Twitter creds
  • API_SECRET_KEY — internal endpoint auth
  • TELEGRAM_* — bot token, chat IDs
  • APIFY_TOKENS / APIFY_TOKEN_1..N — Apify API tokens
  • OPENAI_API_KEY — LLM rewriter
  • CELERY_* — task queue (planned)
  • SCHEDULER_ENABLED, PROMPT_EVERY_MINUTES — scheduler control

Project Status & Roadmap

Done

  • Last-post tracker module
  • Tracker wired into all post flows
  • Telegram helpers and webhook endpoint
  • Inline keyboard flow (category → candidates → rewrite → account → post)
  • LLM rewriter with fallback
  • Simple post endpoint

In Progress / Planned

  • Multi-source Apify rotation with health backoff and caching
  • Celery Beat scheduler for periodic Telegram prompts
  • Redis-backed chat state persistence with TTL
  • Telegram commands: /prompt, /snooze, /status, /last, /toggle_scheduler
  • Per-account cooldown, duplicate detection, quiet hours
  • Webhook secret token validation, internal API key auth
  • Coolify persistent volumes, process commands
  • Structured logging, health endpoints, unit tests

Notes

  • Tracker uses atomic file replace; for multi-worker safety, Redis is planned.
  • Coolify manages env vars dynamically — accounts can be added/removed without manual .env edits.

Location

  • Repo: C:\Users\hanse\twittermgr\
  • This note: Obsidian/MainMind/Projects/Twittermgr/Twittermgr.md

Created: 2026-04-16

0 items under this folder.