# jubayeronrob/tg2fb architecture

> Architecture diagram of the GitHub repository jubayeronrob/tg2fb, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jubayeronrob/tg2fb
- Repository: https://github.com/jubayeronrob/tg2fb

## Overview

#### 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`](https://github.com/jubayeronrob/tg2fb/blob/main/app.py) (Update ingestion), [`.github/workflows/hourly_run.yml`](https://github.com/jubayeronrob/tg2fb/blob/main/.github/workflows/hourly_run.yml) (State commit), [`queue.json`](https://github.com/jubayeronrob/tg2fb/blob/main/queue.json) (Pending FIFO queue), [`state.json`](https://github.com/jubayeronrob/tg2fb/blob/main/state.json) (Update offset)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Automation Runtime"]
  node_workflow["Hourly workflow<br/>GitHub Actions<br/>[hourly_run.yml]"]
  node_requirements["Python + ffmpeg deps<br/>dependencies<br/>[requirements.txt]"]
  node_environment["Secrets and switches<br/>environment"]
  node_state_commit["State commit<br/>workflow step<br/>[hourly_run.yml]"]
end

subgraph group_core["Mirror Pipeline"]
  node_app["CLI entry point<br/>Python batch app<br/>[app.py]"]
  node_ingestion["Update ingestion<br/>pipeline stage<br/>[app.py]"]
  node_normalizer["Post normalizer<br/>pipeline stage<br/>[app.py]"]
  node_transform["Content preparation<br/>pipeline stage<br/>[app.py]"]
  node_publisher["Publication adapter<br/>pipeline stage<br/>[app.py]"]
  node_health["Health check<br/>CLI mode<br/>[app.py]"]
end

subgraph group_persistence["Durable State"]
  node_offset["Update offset<br/>JSON state<br/>[state.json]"]
  node_queue["Pending FIFO queue<br/>JSON queue<br/>[queue.json]"]
  node_dead_letter["Dead-letter items<br/>JSON failure store<br/>[state.json]"]
  node_heartbeat["Run heartbeat<br/>run marker<br/>[last_run.txt]"]
end

subgraph group_external["External Services"]
  node_telegram_updates{{"Telegram Bot API<br/>source API"}}
  node_telegram_media{{"Telegram file download<br/>media API"}}
  node_facebook{{"Facebook Graph API<br/>publication API"}}
end

node_workflow -->|"installs"| node_requirements
node_workflow -->|"runs hourly"| node_app
node_environment -->|"configures"| node_app
node_app -->|"polls"| node_ingestion
node_offset -->|"supplies offset"| node_ingestion
node_ingestion -->|"getUpdates"| node_telegram_updates
node_ingestion -->|"persists immediately"| node_offset
node_ingestion -->|"accepted channel posts"| node_normalizer
node_normalizer -->|"enqueues"| node_queue
node_queue -->|"up to POSTS_PER_RUN"| node_transform
node_transform -->|"downloads media"| node_telegram_media
node_transform -->|"prepared item"| node_publisher
node_publisher -->|"publishes"| node_facebook
node_publisher -->|"acknowledges on success"| node_queue
node_publisher -->|"three failures / oversized video"| node_dead_letter
node_workflow -->|"after real run"| node_state_commit
node_state_commit -->|"commits"| node_offset
node_state_commit -->|"commits"| node_queue
node_state_commit -->|"updates and commits"| node_heartbeat
node_app -.->|"--health"| node_health
node_health -.->|"validates credentials"| node_telegram_updates
node_health -.->|"validates credentials"| node_facebook

click node_workflow "https://github.com/jubayeronrob/tg2fb/blob/main/.github/workflows/hourly_run.yml"
click node_requirements "https://github.com/jubayeronrob/tg2fb/blob/main/requirements.txt"
click node_app "https://github.com/jubayeronrob/tg2fb/blob/main/app.py"
click node_ingestion "https://github.com/jubayeronrob/tg2fb/blob/main/app.py"
click node_normalizer "https://github.com/jubayeronrob/tg2fb/blob/main/app.py"
click node_transform "https://github.com/jubayeronrob/tg2fb/blob/main/app.py"
click node_publisher "https://github.com/jubayeronrob/tg2fb/blob/main/app.py"
click node_offset "https://github.com/jubayeronrob/tg2fb/blob/main/state.json"
click node_queue "https://github.com/jubayeronrob/tg2fb/blob/main/queue.json"
click node_dead_letter "https://github.com/jubayeronrob/tg2fb/blob/main/state.json"
click node_heartbeat "https://github.com/jubayeronrob/tg2fb/blob/main/.github/last_run.txt"
click node_state_commit "https://github.com/jubayeronrob/tg2fb/blob/main/.github/workflows/hourly_run.yml"
click node_health "https://github.com/jubayeronrob/tg2fb/blob/main/app.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_workflow,node_requirements,node_environment,node_state_commit toneBlue
class node_app,node_ingestion,node_normalizer,node_transform,node_publisher,node_health toneAmber
class node_offset,node_queue,node_dead_letter,node_heartbeat toneMint
class node_telegram_updates,node_telegram_media,node_facebook toneRose
```

## Components

### Automation Runtime

- [Hourly workflow](https://github.com/jubayeronrob/tg2fb/blob/main/.github/workflows/hourly_run.yml): GitHub Actions · Scheduled production runner with manual dry-run dispatch. · `.github/workflows/hourly_run.yml`
- [Python + ffmpeg deps](https://github.com/jubayeronrob/tg2fb/blob/main/requirements.txt): dependencies · `requirements.txt`
- **Secrets and switches**: environment · Credentials, DRY_RUN, translation, and processing limits.
- [State commit](https://github.com/jubayeronrob/tg2fb/blob/main/.github/workflows/hourly_run.yml): workflow step · Persists queue, offset, and heartbeat to the default branch. · `.github/workflows/hourly_run.yml`

### Mirror Pipeline

- [CLI entry point](https://github.com/jubayeronrob/tg2fb/blob/main/app.py): Python batch app · Runs one bounded queue-processing invocation. · `app.py`
- [Update ingestion](https://github.com/jubayeronrob/tg2fb/blob/main/app.py): pipeline stage · Polls getUpdates with the persisted update offset. · `app.py`
- [Post normalizer](https://github.com/jubayeronrob/tg2fb/blob/main/app.py): pipeline stage · Builds text, photo, album, or video queue items. · `app.py`
- [Content preparation](https://github.com/jubayeronrob/tg2fb/blob/main/app.py): pipeline stage · Cleans captions, translates or converts bold text, and processes media. · `app.py`
- [Publication adapter](https://github.com/jubayeronrob/tg2fb/blob/main/app.py): pipeline stage · Publishes by content type and confirms success. · `app.py`
- [Health check](https://github.com/jubayeronrob/tg2fb/blob/main/app.py): CLI mode · Validates both external API credentials. · `app.py`

### Durable State

- [Update offset](https://github.com/jubayeronrob/tg2fb/blob/main/state.json): JSON state · Independently records received Telegram update IDs. · `state.json`
- [Pending FIFO queue](https://github.com/jubayeronrob/tg2fb/blob/main/queue.json): JSON queue · Retains items until confirmed publication. · `queue.json`
- [Dead-letter items](https://github.com/jubayeronrob/tg2fb/blob/main/state.json): JSON failure store · Holds exhausted retries and oversized videos. · `state.json`
- [Run heartbeat](https://github.com/jubayeronrob/tg2fb/blob/main/.github/last_run.txt): run marker · `.github/last_run.txt`

### External Services

- **Telegram Bot API**: source API
- **Telegram file download**: media API · Downloads media by stable file_id.
- **Facebook Graph API**: publication API · Routes feed, photo, album, and video publishing.

## Connections

- 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
