Production-grade fail-closed AI agent delivering verified daily Hadith notifications via Telegram.
1.4K
A production-grade, fail-closed AI system and publicly accessible Telegram bot delivering verified daily Hadith notifications to subscribers, orchestrated with LangGraph, verified deterministically with Pydantic, persisted on Neon PostgreSQL, and powered by Groq.
π Deep Dive Architectural Guide: For an exhaustive, step-by-step technical explanation, interview cheatsheet, and full system diagrams, read
docs/PROJECT_WORKFLOW.mdβ .
Generative AI models are probabilistic by natureβthey hallucinate, paraphrase without permission, and confabulate citations. When building software around sacred or canonical domain texts (such as Hadith literature, legal statutes, or clinical guidelines), probabilistic generation without deterministic boundaries is unacceptable.
A naive implementation might ask an LLM to "send a daily Hadith from Sahih al-Bukhari". This frequently results in invented chains of narration (Isnad), paraphrased translations, or fabricated references.
Daily Hadith AI Agent solves this through architectural rigor:
flowchart TD
subgraph Ingestion ["1. Deterministic Ingestion Pipeline"]
API["HadeethEnc API (GET)"] --> Fetch["Candidate Ingestion (EN + AR)"]
Fetch --> Match["Metadata Cross-Match & Provenance"]
Match --> Gate{"Passes Deterministic Gate?"}
Gate -- "No" --> Abort["Persist Audit Log & Abort"]
Gate -- "Yes" --> DB[("Neon PostgreSQL Candidate Queue")]
end
subgraph Webhook ["2. Inbound Webhook & Multi-User Router"]
Users["Public Telegram Users"] -->|"/start, /today, /stop, /help"| WH["FastAPI POST /telegram/webhook"]
WH --> Sec{"Verify Secret Token"}
Sec -- "Invalid" --> Reject["403 Forbidden"]
Sec -- "Valid" --> Router["Command Router"]
Router -->|"/start (Idempotent)"| SubDB[("Telegram Subscribers")]
Router -->|"/today"| InstantSend["Dispatch Today's Hadith"]
Router -->|"/stop"| Deact["Deactivate (is_active = false)"]
end
subgraph Daily ["3. Daily Orchestration & Fan-Out Broadcast"]
Sched["Daily Trigger: 10:00 AM IST"] --> LG["LangGraph Daily StateGraph"]
LG --> Idemp{"Idempotency Check"}
Idemp -- "Already Sent" --> Skip["Skip Execution"]
Idemp -- "Fresh Run" --> LLM["Groq LLM Reflection (Segregated)"]
LLM --> Guard{"Guardrails AI Gate"}
Guard -- "Violation" --> Fallback["Omit Reflection (Fail-Safe)"]
Guard -- "Valid" --> Format["Format Canonical Notification"]
Fallback --> Format
Format --> Broadcaster["Multi-User Fan-Out Broadcaster"]
SubDB -->|"Active Subscribers"| Broadcaster
Broadcaster -->|"30 msg/sec Throttling"| TG["Telegram Bot API"]
TG -->|"403 Blocked"| AutoDeact["Auto-Deactivate Blocked User"]
TG -->|"Delivered"| Record["Record Per-Subscriber Delivery Log"]
end
Every folder in this codebase includes its own dedicated README guide:
| Directory | Package / Component | Documentation Guide |
|---|---|---|
docs/β | Technical Specifications & Handover Records | docs/README.mdβ |
src/β | Application Source Root & Architecture Overview | src/README.mdβ |
src/database/β | Neon PostgreSQL Persistence, Models & Migrations | src/database/README.mdβ |
src/hadeethenc/β | HadeethEnc API Client, Schemas & Invariant Verifier | src/hadeethenc/README.mdβ |
src/ingestion/β | Candidate Ingestion Service & Deduplication | src/ingestion/README.mdβ |
src/workflow/β | LangGraph StateGraph Orchestration & Routing | src/workflow/README.mdβ |
src/llm/β | Groq LLM Reflections, Prompts & Graceful Fallback | src/llm/README.mdβ |
src/guardrails/β | Deterministic Hallucination & Theological Guardrails | src/guardrails/README.mdβ |
src/formatter/β | Telegram Notification Formatter & HTML Payload Assembly | src/formatter/README.mdβ |
src/delivery/β | Telegram Bot API Client, Webhook Router & Multi-User Broadcaster | src/delivery/README.mdβ |
tests/β | Automated Test Suite (99 unit & integration tests across 13 modules) | tests/README.mdβ |
scripts/β | Standalone Operational & Live E2E Verification Scripts | scripts/README.mdβ |
alembic/β | Database Schema Migration Versions | alembic/README.mdβ |
UNIQUE constraint on DailyDeliveryLog.scheduled_date, ensuring at most one delivery per calendar day.AI-generated reflection β not part of the original Hadith.The project is actively developed following a 20-milestone engineering roadmap:
| Milestone | Status | Key Deliverable |
|---|---|---|
| 01 β Project Foundation | Verified | Git repo, environment audit, documentation scaffolding |
| 02 β Source Validation Spec | Verified | HadeethEnc API contracts & Arabic reference regex grammar |
| 03 β Configuration & Secrets | Verified | Pydantic Settings, secret masking, timezone validation |
| 04 β Database Foundation | Verified | Neon PostgreSQL connection, SQLAlchemy models, Alembic migrations |
| 05 β HadeethEnc Integration | Verified | Typed HTTP client, Pydantic schemas, invariant verifier |
| 06 β Source Catalogue Audit | Verified | Empirical audit of 2,776+ Hadiths, acceptance policy |
| 07 β Deterministic Engine | Verified | Ingestion service, queue persistence, live cloud verification |
| 08 β LangGraph Workflow | Verified | StateGraph state machine with conditional routing & live idempotency |
| 09 β Groq Integration | Verified | LLM reflection generation with strict segregation |
| 10 β Guardrails AI | Verified | Content validation & hallucination boundary enforcement |
| 11 β Final Content Validation | Verified | Assembled notification payloads & HTML escaping checks |
| 12 β Telegram Integration & Public Subscription Engine | Verified | Public bot webhook, subscriber registration, commands & rate-limited broadcast |
| 13 β Scheduling & Daily Multi-User Broadcast | Verified | Headless runner CLI, APScheduler (10 AM IST, 1hr misfire grace), Cloud Scheduler cron endpoint |
| 14 β History & Per-Subscriber Daily Idempotency | Verified | Composite idempotency unique constraints, mid-run recovery queries & per-user delivery logs |
| 15 β Failure Handling | Verified | Fault tolerance, retry exponential backoff & fail-closed resilience stress tests (19 tests) |
| 16 β Automated Testing & Code Quality | Verified | Automated test suite (99 tests, 89% coverage), Ruff linting & PEP 8 code formatting |
| 17 β Docker Containerization & GitHub Actions CI/CD | Verified | Multi-stage Docker containerization (non-root appuser, ~160MB), automated test-gated CI/CD publishing to Docker Hub |
| 18 β Cloud Deployment & Public Production Bot | Verified | Deployed on Render Free Web Service with automated daily 10:00 AM IST scheduler on cron-job.org and live Telegram subscribers |
| 19 β Production Readiness & System Audit | Verified | 20/20 Definition of Done verified, production runbook, and defensive security audit |
For the complete 20-milestone breakdown, see docs/PROJECT_ROADMAP.mdβ .
# Clone the repository
git clone https://github.com/Amankhan1009/daily-hadith-ai-agent.git
cd daily-hadith-ai-agent
# Create and activate virtual environment
python3 -m venv .venv
source .venv/bin/activate
# Install dependencies
pip install -r requirements.txt
.venv/bin/pytest -v
Content type
Image
Digest
sha256:fa549b7b3β¦
Size
99.3 MB
Last updated
about 9 hours ago
docker pull amankhan1009/daily-hadith-ai-agent