How qarabaierooom/qazaqsha-ai-whatsapp-bot works
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: router.js Gemini Router · owner-commands.js Policy Pipeline · database.js Memory Access · settings-api.js Dashboard API & Live Updates
Components 19
WhatsApp Channel
- Application Bootstrap
src/index.js - Baileys Connection
src/whatsapp/connection.js - Message I/O · Extracts inbound messages and sends replies.
src/whatsapp/message-extractor.js
Processing & AI
- Policy Pipeline · Applies owner commands, blocks, eligibility, hours, safety, and busy disclosure.
src/pipeline/owner-commands.js - Deferred Work Queue
src/pipeline/message-queue.js - Gemini Router · Selects Gemini model and routes generation requests.
src/gemini/router.js - Prompt Builder
src/gemini/prompt-builder.js - Model & Rate Control · Rotates API keys and chooses Flash, Pro, or Lite models.
src/gemini/model-selector.js - Response Filter & Cache
src/gemini/output-filter.js - Google Gemini
State & Operations
- Local JSON Datastore · Shared persistent state with nominal 90-day history retention.
memory/qazaqsha.json - Memory Access · Manages chat history, profiles, sentiment, and retention.
src/memory/database.js - Maintenance & Backups
src/utils/cron-jobs.js - Application Settings · Combines environment credentials with application settings.
config/settings.js
Administration
- Telegram Admin Bot
telegram-bot/bot.js - Telegram Controls · Owner commands, inline controls, notifications, and settings synchronization.
telegram-bot/commands.js - Dashboard Server
dashboard/server.js - Dashboard API & Live Updates · Provides settings, block-list management, statistics, and live updates.
src/dashboard-api/settings-api.js - Browser Dashboard
dashboard/public/index.html
Connections 22
- 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