Sehat: Health Tracking Telegram Bot

Project Overview: Sehat (meaning “health” in Hindi/Urdu) is a production-ready Telegram bot for personal health and fitness tracking. Built as a complete rewrite from Go to Python, it features conversational workflows for logging daily metrics, automated reminders, and comprehensive data persistence with SQLAlchemy.

Objectives

  1. Create a frictionless health tracking experience through messaging
  2. Eliminate the need for dedicated health tracking apps
  3. Provide quick data entry and retrieval through natural commands
  4. Enable long-term health trend analysis

Features

  1. User Onboarding:

    • Collects date of birth for personalization
    • Weight unit preference configuration (kg/lbs)
    • Goal weight setting for tracking progress
    • Persistent user profile storage
  2. Daily Metrics Tracking:

    • Weight measurements
    • Steps achievement (5k+ goal)
    • Sleep quality (8h+ goal)
    • Diet adherence tracking
    • Cardio activity logging
    • Weightlifting session tracking
  3. Automated Reminders:

    • Daily prompts at 7:10 AM Eastern Time
    • Personalized greeting messages
    • Gentle nudges to maintain consistency
    • Timezone-aware scheduling
  4. Weekly Summaries:

    • Aggregated statistics for each metric
    • Days achieved per goal tracking
    • Progress visualization
    • Historical trend analysis
  5. Production-Ready Deployment:

    • Systemd service integration
    • Automatic restart on failure
    • Git hook-based auto-deployment
    • Rotating log files with comprehensive logging

Technology Stack

  • Language: Python 3.12+
  • Bot Framework: python-telegram-bot (v22.5+)
  • ORM: SQLAlchemy with declarative models
  • Database: SQLite for persistent storage
  • Deployment: systemd service on Linux
  • Package Management: uv for fast dependency resolution
  • Configuration: Environment variables via dotenv
  • Code Quality: Black formatter, type hints, pre-commit hooks

Architecture

The bot follows a clean, modular architecture:

  • Conversation Handlers: Separate handlers for onboarding, daily tracking, and weekly summaries
  • Database Layer: SQLAlchemy models for User, Daily, and Weekly tables
  • Base Handler Pattern: Extensible base class for consistent handler implementation
  • Job Queue: Scheduled tasks for automated daily reminders
  • Logging: Dual logging to both file and stdout for debugging

Commands

  • /start - Begin onboarding and create user profile
  • /daily - Log daily health metrics
  • /weekly - View weekly summary report

Design Philosophy

  • Minimal Friction: Log metrics in seconds without leaving Telegram
  • Privacy First: Self-hosted with local SQLite storage
  • Production Ready: Automated deployment, monitoring, and error handling
  • Maintainable: Type hints, code formatting, and modular architecture

Key Improvements from Go Version

This Python rewrite introduces several enhancements:

  • Conversation State Management: Leverages python-telegram-bot’s built-in conversation handlers
  • ORM Integration: SQLAlchemy provides type-safe database operations
  • Automated Deployment: Git hooks and systemd integration for seamless updates
  • Better Logging: Structured logging with rotation and multiple outputs
  • Code Quality Tools: Pre-commit hooks, formatters, and type checking
  • Simplified Maintenance: More Pythonic code patterns and better documentation