Automating Telegram Content with Python and AI
A Python bot using OpenAI and Google Sheets generates unique posts for your Telegram channel based on a spreadsheet schedule. It pulls data, crafts text tailored to product specs or topics, adds hashtags, and publishes on time. Say goodbye to copy-pasting and templates while keeping full control over your strategy.
Bot Architecture
Data flow:
- Google Sheets holds the posting schedule.
gsheet.pypulls the rows.generator.pycreates text via OpenAI API (GPT-4o-mini).poster.pysends to Telegram with photos.scheduler.pyruns on Moscow time.
Tech stack:
python-telegram-botfor Telegram API.openaifor generation.google-api-python-clientfor Sheets.asyncioand custom scheduler.python-dotenvfor tokens.
Google Sheets as Your CMS
Spreadsheet columns:
- Post date.
- Post topic.
- AI prompt.
- Links to 1–7 photos.
- Product specs.
You fill in the basics: topic, prompt, photos, specs. The bot handles the rest. It's a game-changer for small businesses ditching manual CMS entry.
Content Generation Modes
Code in generator.py handles different scenarios:
Catalog mode (with specs):
Input: SKU, dimensions, material.
Output: Punchy sales copy with emotion and CTA.
Prompts block made-up data.
Atmosphere mode (no specs):
Input: Topic like "How Color Shapes Design".
Output: 2–3 storytelling paragraphs in your brand voice.
Auto-hashtags: build_hashtags() scans topic and specs, adds #newarrival, #review, brand tags.
Posting and Scheduling
- Photo groups (up to 7) + caption up to 1024 chars.
fit_to_caption()trims text at sentence or line breaks.- No photos? Pure text post.
Scheduler scheduler_async() calculates time to next run based on POST_TIME (e.g., "13:00" MSK). Skips timezone headaches from libs like apscheduler.
Technical Limits and Fixes
- Telegram caption: Smart trimming without breaking words.
- GPT hallucinations: Prompts ban fabrication.
- Sheets API quota: 500 requests/day—plenty for 1–2 channels.
Scaling? Monitor quotas and rotate keys.
Tasks before/after automation:
| Task | Manual | Bot |
|--------|--------------|-----|
| Text generation | Human | ✅ OpenAI |
| Formatting | Human | ✅ Auto |
| Hashtags | Human | ✅ Topic analysis |
| Posting | Manual | ✅ Scheduled |
| Planning | Human | ❌ Human only |
Human's Role
Bot handles grunt work, you own strategy:
- Topic selection.
- Prompt tuning.
- Quality checks.
- Schedule tweaks.
Result: Monthly consistent posts, boosted engagement from fresh content.
Key Takeaways
- Craft precise prompts to dodge hallucinations on specs.
- Cap captions at 1024 chars with trim function.
- Custom scheduler fixes timezone issues.
- Google Sheets as DB: free and simple for small scale.
- Stay the strategist: pick topics, ensure quality.
— Editorial Team
No comments yet.