How jubayeronrob/tg2fb works
Purpose and Entry Points
This is a batch automation that mirrors Telegram channel posts to a Facebook Page. Its executable entry point is app.py; it runs locally as a one-shot CLI and in GitHub Actions on an hourly schedule. Persistent JSON files make the workflow resilient across ephemeral CI runners.
Paths: app.py, .github/workflows/hourly_run.yml
Scheduler and Execution Environment
GitHub Actions is the primary production runtime. The hourly workflow injects Telegram and Facebook credentials from repository secrets, installs Python dependencies and ffmpeg, runs the script, then commits persistent state and a heartbeat back to the default branch. Manual dispatch supports a dry-run path; scheduled runs publish normally.
Paths: .github/workflows/hourly_run.yml, .github/last_run.txt, requirements.txt
Ingestion and Offset Boundary
app.py polls the Telegram Bot API getUpdates, using the last processed update_id as an offset. It accepts channel_post updates and normalizes them into text, photo, album, or video queue items. Album messages sharing a Telegram media_group_id are consolidated. The offset is persisted independently immediately after ingestion, separating “received from Telegram” from “successfully published to Facebook.”
Paths: app.py, state.json
Durable Queue and Failure Isolation
Pending normalized posts are persisted as a FIFO queue. Each invocation processes only up to POSTS_PER_RUN items (default one), enforcing rate limiting through the scheduled cadence. Items leave the queue only after confirmed Facebook success. Failed front-of-queue items accumulate retry counts; after three failures, or an unrecoverable media-size condition, they move to a dead-letter file so they cannot block later content.
Paths: app.py, queue.json, state.json
Content Transformation Pipeline
Before publishing, captions/messages are cleaned by stripping trailing @handle promotion text. Telegram bold entities are converted from UTF-16-offset spans into Unicode bold lookalike characters. An optional Google Translate integration translates text to Bangla, replacing the bold-conversion route. Media is downloaded from Telegram by stable file_id; photos receive a logo overlay, while videos are watermarked through ffmpeg. Telegram download limits make videos over 20 MB a dead-letter case.
Paths: app.py, requirements.txt
Facebook Publication Adapter
Publishing routes by normalized content type through the Facebook Graph API: text posts go to the Page feed; a single image goes to the photos endpoint; albums upload constituent images unpublished before creating one feed post with attached media; videos are uploaded after watermark processing. Authentication requires a Page access token with posting and engagement permissions. Confirmation from this boundary controls queue acknowledgement.
Paths: app.py, queue.json
Operational Modes and State Safety
Environment variables hold all credentials and behavior switches. DRY_RUN performs ingestion and content preparation without Facebook publication or state advancement, preserving repeatability for validation. --health validates both external API credentials. Real runs update the offset/queue and heartbeat; CI commits these artifacts so later runs resume exactly where the prior runner ended.
Paths: app.py, state.json, queue.json, .github/last_run.txt
Main entry points: app.py Update ingestion · hourly_run.yml State commit · queue.json Pending FIFO queue · state.json Update offset
Components 17
Automation Runtime
- Hourly workflow · Scheduled production runner with manual dry-run dispatch.
.github/workflows/hourly_run.yml - Python + ffmpeg deps
requirements.txt - Secrets and switches · Credentials, DRY_RUN, translation, and processing limits.
- State commit · Persists queue, offset, and heartbeat to the default branch.
.github/workflows/hourly_run.yml
Mirror Pipeline
- CLI entry point · Runs one bounded queue-processing invocation.
app.py - Update ingestion · Polls getUpdates with the persisted update offset.
app.py - Post normalizer · Builds text, photo, album, or video queue items.
app.py - Content preparation · Cleans captions, translates or converts bold text, and processes media.
app.py - Publication adapter · Publishes by content type and confirms success.
app.py - Health check · Validates both external API credentials.
app.py
Durable State
- Update offset · Independently records received Telegram update IDs.
state.json - Pending FIFO queue · Retains items until confirmed publication.
queue.json - Dead-letter items · Holds exhausted retries and oversized videos.
state.json - Run heartbeat
.github/last_run.txt
External Services
- Telegram Bot API
- Telegram file download · Downloads media by stable file_id.
- Facebook Graph API · Routes feed, photo, album, and video publishing.
Connections 22
- Hourly workflow → Python + ffmpeg deps: installs
- Hourly workflow → CLI entry point: runs hourly
- Secrets and switches → CLI entry point: configures
- CLI entry point → Update ingestion: polls
- Update offset → Update ingestion: supplies offset
- Update ingestion → Telegram Bot API: getUpdates
- Update ingestion → Update offset: persists immediately
- Update ingestion → Post normalizer: accepted channel posts
- Post normalizer → Pending FIFO queue: enqueues
- Pending FIFO queue → Content preparation: up to POSTS_PER_RUN
- Content preparation → Telegram file download: downloads media
- Content preparation → Publication adapter: prepared item
- Publication adapter → Facebook Graph API: publishes
- Publication adapter → Pending FIFO queue: acknowledges on success
- Publication adapter → Dead-letter items: three failures / oversized video
- Hourly workflow → State commit: after real run
- State commit → Update offset: commits
- State commit → Pending FIFO queue: commits
- State commit → Run heartbeat: updates and commits
- CLI entry point → Health check: --health
- Health check → Telegram Bot API: validates credentials
- Health check → Facebook Graph API: validates credentials