Skip to content

Repository files navigation

Lion and Sun Flag of Iran

🐦 Twitter/X → Telegram Bot

Python Telegram FastAPI PostgreSQL Gemini AI

Forward tweets to Telegram — no paid API needed.
Uses RSS feeds. Supports AI-powered Persian translation.
Includes a live web dashboard + PWA.


✨ Features

Feature Description
🔄 Auto Forward Tweets delivered to Telegram automatically
🦁 AI Translation Gemini / Google Translate to Persian
🖼️ Media Support Photos included with tweets
📊 Live Dashboard Web UI to view all tweets
🔍 Inline Search Search tweets from any chat
⌨️ Keyboard Menu Quick buttons for easy use
🌐 PWA Support Installable web app
Parallel Fetching 8 accounts checked simultaneously
🧠 Smart Caching Translation cache (500 entries)
🛡️ Auto Recovery DB reconnect + API fallback
📱 Mobile Friendly Responsive dark UI

🚀 Quick Start

1. Create Telegram Bot

  1. Open @BotFather in Telegram
  2. Send /newbot and choose a name & username
  3. Copy the token (looks like: 123456:ABC-DEF...)

2. Install

git clone https://github.com/Misagh95/twitter-telegram-bot.git
cd twitter-telegram-bot
pip install -r requirements.txt

3. Configure

Create a .env file:

TELEGRAM_BOT_TOKEN=your_bot_token_here
DATABASE_URL=postgresql://user:pass@localhost:5432/dbname

# AI Translation (optional)
TRANSLATE_FA=true
REQUESTY_API_KEY=your_gemini_api_key
REQUESTY_BASE_URL=https://generativelanguage.googleapis.com/v1beta/openai
REQUESTY_MODEL=gemini-2.0-flash

4. Run

python twitter_telegram_bot.py

Open http://localhost:8080 for the live dashboard.


📱 Bot Commands

Command Description
/start Welcome message + keyboard
/add username Add Twitter account(s)
/del username Remove account
/list View tracked accounts
/test Test with @ElonMusk
/search text Search saved tweets
/status Bot health check

⌨️ Keyboard Buttons

Button Action
➕ اضافه کردن Add account
📋 لیست اکانت‌ها View list
🔍 جستجو Search tweets
🌐 داشبورد Open web dashboard
📊 وضعیت Bot status
ℹ️ راهنما Help

🔍 Inline Mode

From any Telegram chat, type:

@your_bot_name elonmusk

Searches saved tweets and shares them inline.


🌐 Deploy to Railway

  1. Push to GitHub
  2. Go to railway.app
  3. New ProjectDeploy from GitHub Repo
  4. Select your repo
  5. Add PostgreSQL database
  6. Set environment variables:
Variable Value
TELEGRAM_BOT_TOKEN Your bot token
DATABASE_URL Auto-set by Railway
REQUESTY_API_KEY Your Gemini key
REQUESTY_BASE_URL https://generativelanguage.googleapis.com/v1beta/openai
REQUESTY_MODEL gemini-2.0-flash
TRANSLATE_FA true
  1. Deploy! 🎉

📁 Project Structure

twitter-telegram-bot/
├── twitter_telegram_bot.py   # Main bot + web server
├── database.py               # PostgreSQL manager
├── templates/
│   └── index.html            # Dashboard UI (PWA)
├── sw.js                     # Service Worker
├── manifest.json             # PWA manifest
├── requirements.txt          # Dependencies
├── Procfile                  # Railway config
├── README_FA.md              # 🇮🇷 Persian docs
└── README.md                 # 🇬🇧 English docs

⚙️ Configuration

Variable Default Description
CHECK_INTERVAL 300 Check interval (seconds)
TRANSLATE_FA true Enable Persian translation
DASHBOARD_URL http://localhost:8080 Dashboard URL
CONCURRENT_LIMIT 8 Parallel fetch limit

📝 Notes

  • ❌ Private accounts won't work (RSS limitation)
  • ✅ Multiple Nitter instances for reliability
  • ✅ Bot auto-switches if one instance is down
  • ✅ Captions split when too long for photos
  • ✅ Deduplication prevents duplicate messages
  • ✅ Old data auto-cleaned (30 days)

🛠️ Tech Stack

  • Backend: Python 3.10+, python-telegram-bot v22
  • Web: FastAPI + Jinja2 + Tailwind CSS
  • Database: PostgreSQL (psycopg2)
  • AI: Google Gemini API
  • RSS: feedparser + httpx
  • PWA: Service Worker + Manifest

Made with ❤️ by Misagh95

About

🐦 Forwards tweets from Twitter/X to Telegram channels automatically

Topics

Resources

Stars

8 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages