# qarabaierooom/qazaqsha-ai-whatsapp-bot architecture

> Architecture diagram of the GitHub repository qarabaierooom/qazaqsha-ai-whatsapp-bot, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/qarabaierooom/qazaqsha-ai-whatsapp-bot
- Repository: https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot

## Overview

#### Purpose and entry points
QazaQsha-001 is a Node.js 18+ personal AI assistant that receives WhatsApp messages, generates Kazakh-oriented Gemini responses, and exposes owner administration through Telegram and a local web dashboard. The primary bootstrap is `src/index.js`; dashboard HTTP serving starts in `dashboard/server.js`; Telegram administration starts in `telegram-bot/bot.js`.

#### Channel adapters
WhatsApp is the primary conversational transport, implemented around Baileys connection lifecycle, message extraction, media handling, outbound sending, and active-chat tracking: `src/whatsapp/connection.js`, `src/whatsapp/message-extractor.js`, `src/whatsapp/sender.js`. Active-chat state supports notifying recently active contacts when the owner becomes present.

Telegram is a separate owner-only control plane rather than a chat-facing AI channel. Commands, inline controls, notifications, and settings synchronization live in `telegram-bot/commands.js`, `telegram-bot/inline-keyboard.js`, and `telegram-bot/settings-sync.js`.

#### Inbound processing pipeline
Incoming WhatsApp content crosses a staged policy/control boundary before Gemini is invoked. The pipeline checks owner commands, blocks, group eligibility, operating hours, duplicate suppression, and sentiment safety; it can also enqueue work under API pressure: `src/pipeline/owner-commands.js`, `src/pipeline/block-check.js`, `src/pipeline/message-queue.js`. Reply composition includes owner-busy disclosure and sentiment-aware behavior via `src/pipeline/disclaimer.js` and `src/pipeline/sentiment-guard.js`.

#### Gemini response subsystem
The AI boundary is centralized in `src/gemini/router.js`. It selects among Gemini 2.5 Flash/Pro/Lite and rotates available API keys through `src/gemini/model-selector.js` and `src/gemini/rate-tracker.js`; rate-constrained requests can be deferred by the pipeline queue. Prompts incorporate persona and stored context through `src/gemini/prompt-builder.js`; generated output is filtered and may be cached in `src/gemini/output-filter.js` and `src/gemini/response-cache.js`. Google Gemini is the principal external AI service.

#### Persistent state and retention
The system uses a JSON/lowdb-style local datastore, centered on `memory/qazaqsha.json` and access modules in `src/memory/database.js`. It persists chat history, user profiles, sentiment signals, and configurable retention—nominally 90-day history—through `src/memory/chat-history.js`, `src/memory/user-profile.js`, and `src/memory/retention.js`. This local persistence is shared state for WhatsApp behavior and all admin surfaces.

#### Administration and dashboard
Dashboard HTTP/API functions provide settings, block-list management, statistics, and live updates: `dashboard/server.js`, `src/dashboard-api/settings-api.js`, `src/dashboard-api/websocket-server.js`. The browser UI is a static frontend served from `dashboard/public/index.html` and driven by `dashboard/public/app.js`. It operates locally on `ADMIN_PORT` (default 3000), providing another control path to the same settings/state used by Telegram and WhatsApp owner commands.

#### Operations and configuration
Runtime configuration combines environment secrets—Gemini keys, Telegram token/owner ID, owner identity, and dashboard port—with application settings in `config/settings.js`. Scheduled cleanup/maintenance and backups are handled by `src/utils/cron-jobs.js` and `src/utils/backup.js`; logs are under `logs`. Core infrastructure is Node.js, Baileys/WhatsApp, Telegram Bot API, Google Gemini, local JSON storage, HTTP/WebSocket dashboard delivery, and environment-provided credentials.

Main entry points: [`src/gemini/router.js`](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/gemini/router.js) (Gemini Router), [`src/pipeline/owner-commands.js`](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/pipeline/owner-commands.js) (Policy Pipeline), [`src/memory/database.js`](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/memory/database.js) (Memory Access), [`src/dashboard-api/settings-api.js`](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/dashboard-api/settings-api.js) (Dashboard API & Live Updates)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_whatsapp["WhatsApp Channel"]
  node_bootstrap["Application Bootstrap<br/>Node.js entry<br/>[index.js]"]
  node_whatsapp_connection["Baileys Connection<br/>WhatsApp adapter<br/>[connection.js]"]
  node_message_io["Message I/O<br/>WhatsApp adapter"]
end

subgraph group_processing["Processing &amp; AI"]
  node_pipeline["Policy Pipeline<br/>message control<br/>[owner-commands.js]"]
  node_message_queue["Deferred Work Queue<br/>rate-pressure queue<br/>[message-queue.js]"]
  node_gemini_router{{"Gemini Router<br/>AI gateway<br/>[router.js]"}}
  node_prompt_builder["Prompt Builder<br/>context composer<br/>[prompt-builder.js]"]
  node_model_control["Model &amp; Rate Control<br/>Gemini selection<br/>[model-selector.js]"]
  node_response_processing["Response Filter &amp; Cache<br/>output processing<br/>[output-filter.js]"]
  node_gemini(("Google Gemini<br/>external AI service"))
end

subgraph group_state["State &amp; Operations"]
  node_database[("Local JSON Datastore<br/>lowdb storage<br/>[qazaqsha.json]")]
  node_memory_access["Memory Access<br/>state repository<br/>[database.js]"]
  node_maintenance["Maintenance &amp; Backups<br/>scheduled operations<br/>[cron-jobs.js]"]
  node_settings["Application Settings<br/>configuration<br/>[settings.js]"]
end

subgraph group_admin["Administration"]
  node_telegram_bot["Telegram Admin Bot<br/>owner control plane<br/>[bot.js]"]
  node_telegram_controls["Telegram Controls<br/>commands and sync<br/>[commands.js]"]
  node_dashboard_server["Dashboard Server<br/>local HTTP server<br/>[server.js]"]
  node_dashboard_api["Dashboard API &amp; Live Updates<br/>HTTP/WebSocket API<br/>[settings-api.js]"]
  node_dashboard_ui["Browser Dashboard<br/>static frontend<br/>[index.html]"]
end

node_bootstrap -->|"starts"| node_whatsapp_connection
node_whatsapp_connection -->|"inbound events"| node_message_io
node_message_io -->|"incoming content"| node_pipeline
node_pipeline -.->|"defer under pressure"| node_message_queue
node_message_queue -->|"scheduled request"| node_gemini_router
node_pipeline -->|"approved request"| node_gemini_router
node_gemini_router -->|"build prompt"| node_prompt_builder
node_prompt_builder -->|"loads context"| node_memory_access
node_gemini_router -->|"selects model/key"| node_model_control
node_model_control -->|"generation request"| node_gemini
node_gemini -->|"generated output"| node_response_processing
node_response_processing -->|"reply"| node_message_io
node_memory_access -->|"persists state"| node_database
node_maintenance -->|"cleanup and backup"| node_database
node_settings -.->|"policy settings"| node_pipeline
node_settings -.->|"credentials"| node_gemini_router
node_telegram_bot -->|"runs controls"| node_telegram_controls
node_telegram_controls -->|"reads and updates state"| node_memory_access
node_dashboard_server -->|"serves API"| node_dashboard_api
node_dashboard_server -->|"serves static UI"| node_dashboard_ui
node_dashboard_api -->|"reads and updates state"| node_memory_access
node_dashboard_api -->|"updates settings"| node_settings

click node_bootstrap "https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/index.js"
click node_whatsapp_connection "https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/whatsapp/connection.js"
click node_message_io "https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/whatsapp/message-extractor.js"
click node_pipeline "https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/pipeline/owner-commands.js"
click node_message_queue "https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/pipeline/message-queue.js"
click node_gemini_router "https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/gemini/router.js"
click node_prompt_builder "https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/gemini/prompt-builder.js"
click node_model_control "https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/gemini/model-selector.js"
click node_response_processing "https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/gemini/output-filter.js"
click node_database "https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/memory/qazaqsha.json"
click node_memory_access "https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/memory/database.js"
click node_maintenance "https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/utils/cron-jobs.js"
click node_settings "https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/config/settings.js"
click node_telegram_bot "https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/telegram-bot/bot.js"
click node_telegram_controls "https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/telegram-bot/commands.js"
click node_dashboard_server "https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/dashboard/server.js"
click node_dashboard_api "https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/dashboard-api/settings-api.js"
click node_dashboard_ui "https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/dashboard/public/index.html"

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_bootstrap,node_whatsapp_connection,node_message_io toneBlue
class node_pipeline,node_message_queue,node_gemini_router,node_prompt_builder,node_model_control,node_response_processing,node_gemini toneAmber
class node_database,node_memory_access,node_maintenance,node_settings toneMint
class node_telegram_bot,node_telegram_controls,node_dashboard_server,node_dashboard_api,node_dashboard_ui toneRose
```

## Components

### WhatsApp Channel

- [Application Bootstrap](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/index.js): Node.js entry · `src/index.js`
- [Baileys Connection](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/whatsapp/connection.js): WhatsApp adapter · `src/whatsapp/connection.js`
- [Message I/O](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/whatsapp/message-extractor.js): WhatsApp adapter · Extracts inbound messages and sends replies. · `src/whatsapp/message-extractor.js`

### Processing & AI

- [Policy Pipeline](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/pipeline/owner-commands.js): message control · Applies owner commands, blocks, eligibility, hours, safety, and busy disclosure. · `src/pipeline/owner-commands.js`
- [Deferred Work Queue](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/pipeline/message-queue.js): rate-pressure queue · `src/pipeline/message-queue.js`
- [Gemini Router](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/gemini/router.js): AI gateway · Selects Gemini model and routes generation requests. · `src/gemini/router.js`
- [Prompt Builder](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/gemini/prompt-builder.js): context composer · `src/gemini/prompt-builder.js`
- [Model & Rate Control](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/gemini/model-selector.js): Gemini selection · Rotates API keys and chooses Flash, Pro, or Lite models. · `src/gemini/model-selector.js`
- [Response Filter & Cache](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/gemini/output-filter.js): output processing · `src/gemini/output-filter.js`
- **Google Gemini**: external AI service

### State & Operations

- [Local JSON Datastore](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/memory/qazaqsha.json): lowdb storage · Shared persistent state with nominal 90-day history retention. · `memory/qazaqsha.json`
- [Memory Access](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/memory/database.js): state repository · Manages chat history, profiles, sentiment, and retention. · `src/memory/database.js`
- [Maintenance & Backups](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/utils/cron-jobs.js): scheduled operations · `src/utils/cron-jobs.js`
- [Application Settings](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/config/settings.js): configuration · Combines environment credentials with application settings. · `config/settings.js`

### Administration

- [Telegram Admin Bot](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/telegram-bot/bot.js): owner control plane · `telegram-bot/bot.js`
- [Telegram Controls](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/telegram-bot/commands.js): commands and sync · Owner commands, inline controls, notifications, and settings synchronization. · `telegram-bot/commands.js`
- [Dashboard Server](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/dashboard/server.js): local HTTP server · `dashboard/server.js`
- [Dashboard API & Live Updates](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/src/dashboard-api/settings-api.js): HTTP/WebSocket API · Provides settings, block-list management, statistics, and live updates. · `src/dashboard-api/settings-api.js`
- [Browser Dashboard](https://github.com/qarabaierooom/qazaqsha-ai-whatsapp-bot/blob/main/dashboard/public/index.html): static frontend · `dashboard/public/index.html`

## Connections

- Application Bootstrap → Baileys Connection: starts
- Baileys Connection → Message I/O: inbound events
- Message I/O → Policy Pipeline: incoming content
- Policy Pipeline → Deferred Work Queue: defer under pressure
- Deferred Work Queue → Gemini Router: scheduled request
- Policy Pipeline → Gemini Router: approved request
- Gemini Router → Prompt Builder: build prompt
- Prompt Builder → Memory Access: loads context
- Gemini Router → Model & Rate Control: selects model/key
- Model & Rate Control → Google Gemini: generation request
- Google Gemini → Response Filter & Cache: generated output
- Response Filter & Cache → Message I/O: reply
- Memory Access → Local JSON Datastore: persists state
- Maintenance & Backups → Local JSON Datastore: cleanup and backup
- Application Settings → Policy Pipeline: policy settings
- Application Settings → Gemini Router: credentials
- Telegram Admin Bot → Telegram Controls: runs controls
- Telegram Controls → Memory Access: reads and updates state
- Dashboard Server → Dashboard API & Live Updates: serves API
- Dashboard Server → Browser Dashboard: serves static UI
- Dashboard API & Live Updates → Memory Access: reads and updates state
- Dashboard API & Live Updates → Application Settings: updates settings
