A specialized Telegram bot designed to work in multiple group chats and the owner's personal chat with these features:
- Instagram Video Downloader: Automatically downloads and reposts Instagram reels and videos when links are shared
- Ukrainian Keyword Response: Responds with a custom GIF when a specific user sends messages containing configurable trigger words
- π¬ Instagram Video Support: Silently downloads and sends videos from Instagram posts (images are skipped)
- πΊπ¦ Ukrainian Keyword Detection: Silently responds with GIF to configurable trigger words
- β‘ Rate Limiting: Limits GIF target users to 5 Instagram requests per minute (silently sends GIF if exceeded)
- πΎ Smart Caching: Rate-limited users get cached videos for repeated URLs (reduces Instagram API calls)
- π Multi-Chat Operation: Works in multiple groups + owner's personal chat
- π€ Owner-Only Configuration: Only the bot owner can access configuration commands
- π Automatic Cleanup: Downloads are automatically deleted after posting
- π Comprehensive Logging: Full logging of all operations
- Python 3.8 or higher
- A Telegram account
- Instagram video content you want to download (public posts)
- Message @BotFather on Telegram
- Send
/newbotand follow the instructions - Save the bot token you receive
You'll need these Telegram IDs:
- Your User ID: Message @userinfobot to get your ID (enables personal chat)
- Group Chat IDs: Add the bot to your groups, then use
/configto see chat IDs - Target User ID: Get the ID of the person who should trigger GIF responses
π‘ The bot automatically works in:
- β Your personal chat (using your user ID)
- β
All groups you specify in
TARGET_GROUPS
-
Clone or download this repository
-
Install dependencies:
pip install -r requirements.txt
-
Create environment configuration:
cp env_example.txt .env
-
Edit
.envfile with your values:TELEGRAM_BOT_TOKEN=your_bot_token_from_botfather OWNER_USER_ID=your_telegram_user_id TARGET_GROUPS=-4868143256,-1234567890,-9876543210 RATE_LIMITED_USERS=324187121,987654321,555666777 GIF_FILE_PATH=path_to_your_gif_file.gif
Multiple Groups: Use comma-separated list of group IDs Multiple Rate-Limited Users: Use comma-separated list of user IDs for rate limiting, caching, and GIF responses Single Group/User: You can still use
TARGET_GROUP_IDandGIF_TARGET_USER_IDfor backward compatibility -
Add your GIF file:
- Place your GIF file in the project directory
- Update
GIF_FILE_PATHin.envto point to your GIF
-
Setup Instagram Authentication (Optional but Recommended):
Instagram downloads may fail without proper authentication. Set up cookies for reliable access:
Method 1 - Browser Cookie Extraction (Easy):
# In your .env file, set to your browser name (NATIVE ENVIRONMENTS ONLY) INSTAGRAM_BROWSER=chrome # or firefox, safari, edge
Method 2 - Manual Cookie File (WSL/Fallback):
# Leave browser empty and provide cookie file INSTAGRAM_BROWSER= INSTAGRAM_COOKIES_FILE=instagram_cookies.txt
π§ WSL Users: Browser extraction won't work. Use the helper script:
python wsl_cookie_helper.py
π For detailed setup instructions, see INSTAGRAM_COOKIES_SETUP.md
python bot.py| Variable | Description | Required |
|---|---|---|
TELEGRAM_BOT_TOKEN |
Bot token from @BotFather | Yes |
OWNER_USER_ID |
Your Telegram user ID (also enables personal chat) | Yes |
TARGET_GROUPS |
Comma-separated list of group chat IDs where bot works | Yes* |
TARGET_GROUP_ID |
Single group chat ID (legacy - for backward compatibility) | Yes* |
RATE_LIMITED_USERS |
Comma-separated list of user IDs for rate limiting, caching, and GIF responses | Yes** |
GIF_TARGET_USERS |
DEPRECATED - Use RATE_LIMITED_USERS instead |
No |
GIF_TARGET_USER_ID |
Single user ID (legacy - for backward compatibility) | Yes** |
GIF_FILE_PATH |
Path to GIF file | Yes |
DOWNLOAD_DIR |
Directory for temporary downloads | No (default: downloads) |
TRIGGER_WORDS |
Trigger words for GIF (comma-separated) | No (default: Π±ΠΎΡ,Π±ΠΎΡΠ°Π½ΡΠΊ,Π±ΠΎΡΠΈΠΊ) |
LOG_LEVEL |
Logging level | No (default: INFO) |
LOG_FILE |
Log file path | No (default: bot.log) |
INSTAGRAM_BROWSER |
Browser name for cookie extraction (chrome/firefox/safari/edge) | No |
INSTAGRAM_USERNAME |
DEPRECATED - Use INSTAGRAM_BROWSER instead |
No |
INSTAGRAM_COOKIES_FILE |
Manual Instagram cookies file path | No (default: instagram_cookies.txt) |
CACHE_ENABLED |
Enable video caching for rate-limited users | No (default: true) |
CACHE_DIR |
Directory for cached videos | No (default: cache) |
CACHE_MAX_AGE_DAYS |
Keep cached files for X days | No (default: 7) |
CACHE_MAX_SIZE_MB |
Maximum cache size in MB | No (default: 500) |
* Either TARGET_GROUPS or TARGET_GROUP_ID is required (not both). Use TARGET_GROUPS for multiple groups, or TARGET_GROUP_ID for a single group (legacy).
** Either RATE_LIMITED_USERS or GIF_TARGET_USER_ID is required (not both). Use RATE_LIMITED_USERS for multiple users, or GIF_TARGET_USER_ID for a single user (legacy).
/start- Show bot information/status- Check bot status and statistics/config- Show current configuration
-
Instagram Videos:
- Just share any Instagram reel/post link in the group
- Bot silently downloads and sends video content (no status messages)
- Videos: Reels, IGTV, regular videos
- Note: Image-only posts are silently skipped
- Limitation: Only works with public posts or posts accessible without login
-
Ukrainian Response:
- When configured users send messages containing trigger words
- Default trigger words: "Π±ΠΎΡ", "Π±ΠΎΡΠ°Π½ΡΠΊ", "Π±ΠΎΡΠΈΠΊ" (configurable)
- Supports exact matches and words starting with trigger words
- Bot silently replies with the configured GIF
-
Rate Limiting:
- Who: Only applies to users configured in
RATE_LIMITED_USERS - Limit: Maximum 5 Instagram download requests per minute (per user)
- Counting: Each Instagram URL counts as one request (even if multiple URLs in one message)
- Behavior: If limit exceeded, bot silently sends GIF and stops processing remaining URLs
- Reset: Rate limit automatically resets after 1 minute
- Purpose: Prevents spam and reduces Instagram API usage
- Who: Only applies to users configured in
-
Smart Caching:
- Who: Only applies to rate-limited users (configured in
RATE_LIMITED_USERS) - Purpose: If rate-limited users spam the same Instagram URLs repeatedly, videos are served from local cache
- Benefits: Faster responses, reduced Instagram API calls, bandwidth savings
- Cache Key: Based on Instagram URL (same URL = same cached video)
- Storage: Videos stored in
cache/directory with automatic cleanup - Limits:
- Age: Files older than 7 days are automatically deleted
- Size: Cache limited to 500MB total (oldest files removed first)
- Behavior: Cache hit = instant video send, Cache miss = download and cache for future
- Configuration: Can be disabled by setting
CACHE_ENABLED=falsein.env
- Who: Only applies to rate-limited users (configured in
instagram.com/reel/...- Video reelsinstagram.com/p/...- Posts (only videos will be downloaded)instagram.com/tv/...- IGTV videosinstagr.am/reel/...- Short reel URLsinstagr.am/p/...- Short post URLs
- β Video reels - Instagram reels and short videos
- β Regular videos - Longer video posts
- β IGTV videos - Instagram TV content
- β Photos/Images - Will be skipped (not downloaded)
- β Photo carousels - Will be skipped (not downloaded)
- β Mixed content - Only video parts will be downloaded
- Only works in the specified group chat
- Configuration commands are owner-only
- No data is stored permanently (downloads are cleaned up)
- Comprehensive logging for monitoring
First, run the test script to verify your configuration:
python test_bot.pyThis will test:
- Configuration validity
- Instagram URL detection
- Ukrainian keyword matching
- File operations
-
Bot not responding:
- Check if bot is added to the correct groups
- Verify
TARGET_GROUPScontains correct chat IDs (usepython get_chat_id.py) - Check bot has necessary permissions in the groups
- Owner can also message bot directly in personal chat
- Look at
bot.logfor detailed error messages
-
Instagram video downloads failing:
- Setup cookies first: See INSTAGRAM_COOKIES_SETUP.md for detailed instructions
- Bot only downloads videos - image posts will be skipped
- Check if post contains video content (not just images)
- Authentication errors: "Login required" means cookies are needed or expired
- Rate limiting: Instagram may block downloads - wait 15-30 minutes or setup cookies
- Update yt-dlp:
pip install --upgrade yt-dlp - Private or restricted posts may not work even with cookies
-
GIF not sending:
- Verify
GIF_FILE_PATHpoints to a valid file - Check file permissions and format (GIF, MP4, etc.)
- Ensure file size is under Telegram limits (50MB)
- Make sure the message is from the correct user ID
- Test with
python test_bot.pyto verify keyword detection
- Verify
-
Ukrainian keyword not working:
- Check your
TRIGGER_WORDSconfiguration (comma-separated list) - Message must be from users specified in
RATE_LIMITED_USERS - Words must match or start with any of your configured trigger words
- Default trigger words: "Π±ΠΎΡ", "Π±ΠΎΡΠ°Π½ΡΠΊ", "Π±ΠΎΡΠΈΠΊ"
- Example: "hello Π±ΠΎΡ" will trigger if "Π±ΠΎΡ" is in your trigger words list
- Check
bot.logfor detailed debugging info
- Check your
-
Rate limiting questions:
- Only users in
RATE_LIMITED_USERSare rate limited for Instagram downloads - Other users can still download without limits
- Rate limit is 5 Instagram requests per minute (60 seconds per user)
- Each Instagram URL counts as one request (5 URLs in one message = 5 requests)
- When exceeded, GIF is silently sent and remaining URLs are skipped
- No user-visible messages - check
bot.logfor rate limit events: "Rate limit exceeded" - Use
/configcommand to see current rate limit settings
- Only users in
python get_chat_id.py- Add your bot to the group (temporarily)
- Send a message - you'll see:
- Group chat ID
- All administrators with IDs
- Your user ID
- To get other user IDs: Ask group members to send any message while the script runs
- All active users will be tracked and shown with their IDs
Use /config command with your main bot if you're the owner
Check bot.log for detailed information about bot operations, errors, and debugging information.
bot2/
βββ bot.py # Main bot script
βββ config.py # Configuration management
βββ get_chat_id.py # Utility to find chat and user IDs
βββ test_bot.py # Test script for debugging
βββ setup.py # Automated setup script
βββ wsl_cookie_helper.py # WSL cookie setup helper
βββ requirements.txt # Python dependencies
βββ env_example.txt # Environment variables example
βββ README.md # This file
βββ FIX_BOT_PERMISSIONS.md # Bot permission troubleshooting guide
βββ INSTAGRAM_COOKIES_SETUP.md # Instagram cookie authentication guide
βββ downloads/ # Temporary download directory (created automatically)
βββ bot.log # Log file (created automatically)
βββ your_gif.gif # Your GIF file for responses
python-telegram-bot- Telegram Bot API wrapperyt-dlp- Instagram video downloader (videos only)pathlib- Path handlingasyncio- Asynchronous operations
- The bot automatically creates a
downloadsfolder for temporary files - All downloaded Instagram content is deleted after posting to save space
- Videos Only: The bot will skip Instagram posts that contain only images
- The bot responds to configurable trigger words (default: "Π±ΠΎΡ", "Π±ΠΎΡΠ°Π½ΡΠΊ", "Π±ΠΎΡΠΈΠΊ")
- Cookie Authentication: Use browser cookies for reliable Instagram downloads
- Make sure your GIF file is accessible and not too large (Telegram has size limits)
If you encounter issues:
- Check the logs in
bot.log - Verify all configuration values are correct
- Ensure the bot has proper permissions in the group
- Set up Instagram cookie authentication using INSTAGRAM_COOKIES_SETUP.md
- Check that all required files exist and are accessible