Bulk Messaging System

An Electron Desktop app for marketing campaign & broadcast management, using either whatsapp, gmail or a SMTP server

Python backend services

Introduction#

Python side of contact work: Flask API, file extractors, validation, and the bits mirrored into the renderer via Pyodide.

  • Support multiple file formats with automatic format detection
  • Extract and clean phone numbers with flexible international formatting
  • Provide reliable error handling and fallback parsing
  • Integrate cleanly with the Electron frontend via Pyodide
  • Offer a lightweight local development server for testing and validation

Project structure#

The Python backend is organized into modular components:

  • Flask API server for contact processing and validation
  • Standalone utilities for file-based and manual number parsing
  • Pyodide integration for browser-based Python execution
  • Local development server and CLI functions for testing

Core components#

This section outlines the primary backend components and their responsibilities.

  • Flask API server

  • Provides endpoints for health checks, file uploads, manual number parsing, and phone number validation

  • Implements file type validation, secure filename handling, and cleanup of uploaded files

  • Uses pandas for CSV and Excel parsing with fallback to pure Python parsing

  • Contact extraction utilities

  • Extracts contacts from CSV, TXT, and Excel files

  • Detects phone number and name columns automatically

  • Cleans and standardizes phone numbers with flexible international formatting

  • Phone number validation utility

  • Validates and cleans individual phone numbers

  • Enforces length constraints and international formatting rules

  • Manual numbers parser

  • Parses manually entered phone numbers with optional names

  • Supports multiple input formats and separators

  • Pyodide integration

  • Loads Pyodide runtime and executes Python scripts in the browser

  • Enables manual number parsing directly from the Electron UI

  • Local development server and CLI functions

  • Provides a local Flask server for development and testing

  • Offers CLI functions for sending WhatsApp messages and managing contacts

Architecture overview#

The backend architecture follows a layered design:

  • Presentation layer: Flask API endpoints
  • Processing layer: Contact extraction and validation utilities
  • Integration layer: Pyodide runtime for browser-based Python execution
  • Persistence layer: SQLite database for user management (local development server)

Detailed component analysis#

Flask API implementation#

The Flask API provides four main endpoints:

  • Health check endpoint for monitoring
  • File upload endpoint for CSV, TXT, XLSX, and XLS files
  • Manual numbers parsing endpoint
  • Single number validation endpoint

Key implementation details:

  • CORS is enabled for cross-origin requests
  • File upload handling with secure filename validation
  • Automatic file type detection and processing
  • Cleanup of uploaded files after processing
  • Detailed error handling with appropriate HTTP status codes

Contact extraction utilities#

The contact extraction utilities support three file formats with automatic format detection:

CSV processing#

  • Uses pandas for efficient CSV reading
  • Automatically detects phone number and name columns based on header keywords
  • Falls back to pure Python CSV reader for malformed files
  • Handles UTF-8 encoding and various separator formats

TXT processing#

  • Processes plain text files with flexible formatting
  • Splits lines by common separators (comma, semicolon, tab, pipe)
  • Attempts to identify phone numbers using regex patterns
  • Extracts names from the remaining parts of each line

Excel processing#

  • Supports both .xlsx and .xls formats via pandas
  • Automatically detects column headers for phone numbers and names
  • Handles missing values and NaN entries gracefully

Phone number validation logic#

The phone number validation logic implements the following rules:

  • Removes all non-digit characters except plus signs
  • Strips leading zeros for domestic numbers
  • Adds plus sign prefix for international numbers (>10 digits)
  • Validates length constraints (minimum 7, maximum 15 digits)
  • Handles mixed formatting (parentheses, dashes, spaces)

Manual numbers parser#

The manual numbers parser handles various input formats:

  • One number per line
  • Name: Number format
  • Number - Name format
  • Mixed separators (colon, dash, pipe)

Processing logic:

  • Splits input by newlines and common separators
  • Attempts to identify phone numbers using regex patterns
  • Extracts names from the remaining parts
  • Applies the same cleaning and validation logic as file-based processing

Pyodide runtime integration#

The Electron application integrates with Pyodide to enable browser-based Python execution:

  • Dynamically loads Pyodide runtime from CDN
  • Fetches and executes Python scripts in the browser
  • Provides a wrapper function for parsing manual numbers
  • Escapes special characters for safe Python string handling

Local development server#

The local development server provides:

  • User authentication and session management
  • File upload and storage
  • Dynamic table creation for user data
  • API endpoints for frontend integration
  • SQLite database persistence

Key features:

  • SQL authentication with user registration
  • File upload with secure filename handling
  • Dynamic table creation based on user input
  • CORS-enabled API endpoints for frontend integration
  • SQLite database for persistent storage

Command-Line interface functions#

The CLI functions provide:

  • WhatsApp message sending via Selenium WebDriver
  • Contact import and filtering capabilities
  • Message scheduling functionality
  • File-based contact processing

Capabilities:

  • Send messages to all numbers in CSV/Excel files
  • Filter contacts by tags or names
  • Schedule messages for future delivery
  • Handle various file formats and error conditions

Dependency analysis#

The Python backend has minimal external dependencies focused on web serving and data processing:

Performance considerations#

The backend implements several performance optimization techniques:

  • Efficient file processing

  • Uses pandas for fast CSV and Excel parsing

  • Implements fallback parsing for edge cases

  • Minimizes memory usage by processing files line-by-line for TXT files

  • Regex optimization

  • Pre-compiles regex patterns for phone number detection

  • Uses efficient character class matching

  • Avoids excessive backtracking in patterns

  • Memory management

  • Cleans up uploaded files immediately after processing

  • Uses generators for large file processing

  • Avoids loading entire files into memory unnecessarily

  • Error handling efficiency

  • Implements early exit for invalid inputs

  • Uses try-except blocks around expensive operations

  • Provides fallback parsing to minimize processing failures

  • Browser-based execution

  • Pyodide runtime is loaded once and reused

  • Python scripts are cached after initial load

  • Minimal overhead for repeated parsing operations

Troubleshooting guide#

Common issues and their solutions:

File processing errors#

  • Empty or malformed files. The system provides fallback parsing for CSV files when pandas fails
  • Encoding issues. Files are processed with UTF-8 encoding; ensure proper file encoding
  • Column header variations. The system searches for common keywords in column names

Phone number validation failures#

  • Invalid length. Numbers must be between 7 and 15 digits after cleaning
  • Unsupported characters. Only digits, plus signs, and common separators are allowed
  • Format inconsistencies. The cleaner removes separators and applies international formatting rules

Pyodide integration issues#

  • CDN loading failures. The system attempts to load Pyodide from CDN; check network connectivity
  • Script execution errors. Python scripts are executed asynchronously; check console for error messages
  • Memory limitations. Large input texts may exceed Pyodide memory limits

Local development server issues#

  • Database initialization. The server creates tables on startup; ensure proper permissions
  • File upload errors. Check upload directory permissions and available disk space
  • CORS issues. Ensure proper CORS configuration for frontend integration

Security considerations#

The backend implements several security measures:

  • Input validation and sanitization

  • Secure filename handling prevents directory traversal attacks

  • File type validation restricts uploads to allowed formats

  • Phone number cleaning removes potentially malicious characters

  • File processing security

  • Uploaded files are deleted after processing

  • CSV parsing falls back to pure Python reader to avoid pandas vulnerabilities

  • TXT file processing strips whitespace and validates input

  • Pyodide runtime security

  • Python scripts are executed in isolated browser context

  • Input text is escaped before Python string injection

  • Runtime is loaded from trusted CDN

  • Database security (local development)

  • SQLite database stored locally with proper permissions

  • SQL injection prevention through ORM usage

  • User credentials stored securely

  • CORS configuration

  • Flask-CORS enabled for controlled cross-origin requests

  • Proper headers set for API responses

Production deployment habits:

  • Use HTTPS for all API endpoints
  • Implement rate limiting for file uploads
  • Add authentication and authorization for sensitive operations
  • Monitor and log all API requests
  • Regular security updates for dependencies

Conclusion#

Treat this package as a library the desktop app happens to ship . Keep side effects in the Flask layer, not in the pure parsers.