Skip to content

vicharanashala/wa-client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

95 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AjraSakha β€” WhatsApp AI Agricultural Assistant

An AI-powered WhatsApp bot built for Indian farmers, delivering expert agricultural advice through text, voice, and real-time phone calls β€” backed by human reviewer verification, multi-lingual localization, and a rich ecosystem of domain-specific MCP tool servers.


Table of Contents


Overview

AjraSakha (wa-client) is a production-grade WhatsApp Business API integration built on NestJS using CQRS architecture. It serves as the communication layer of the Annam.AI Foundation platform β€” connecting Indian farmers with AI-driven agricultural intelligence through WhatsApp text, voice notes, and real-time VoIP calls.

Business Objective

Provide accessible, multilingual agricultural advisory services to Indian farmers who may be illiterate or semi-literate, by:

  1. Answering farming questions via text or voice in the farmer's local language
  2. Routing unanswered questions to human expert reviewers for quality assurance
  3. Delivering expert-reviewed answers back to farmers, automatically localized
  4. Providing real-time voice consultations via WhatsApp VoIP calls powered by Gemini Live

Key Features

Category Capability
Multi-Modal Input Text messages, voice notes (STT via Sarvam AI), real-time VoIP calls
AI-Powered Responses LangGraph agent orchestration with MCP tool integration
Voice Output Text-to-Speech via Sarvam AI, delivered as WhatsApp voice notes
Real-Time Calling WebRTC ↔ Gemini Live bridge for live voice conversations
Expert Review Pipeline Questions auto-uploaded to reviewer system; answers polled/webhoooked and localized
Multi-Lingual Auto-detects 10+ Indian languages; translates expert answers via Claude
Access Control Whitelist (dev) / Blacklist (prod) system backed by MongoDB
User Analytics Unique user counts, message history, engagement tracking
CQRS Architecture Clean command/query separation for maintainable business logic
Production Hardened Webhook signature verification, thread repair, graceful error handling

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Meta WhatsApp Cloud  β”‚
β”‚    Business API       β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
           β”‚ Webhook (POST /whatsapp/webhook)
           β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  WhatsApp Controller │────▢│   CQRS Command  β”‚
β”‚  (Signature verify)  β”‚     β”‚   Bus (NestJS)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                      β”‚
           β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
           β–Ό                          β–Ό                          β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ AddUserText     β”‚     β”‚ AddUserVoice     β”‚     β”‚ SetUserLocation  β”‚
β”‚ MessageHandler  β”‚     β”‚ MessageHandler   β”‚     β”‚ Handler          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                       β”‚                         β”‚
         β”‚              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β–Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”               β”‚
         β”‚              β”‚  Sarvam AI STT   β”‚               β”‚
         β”‚              β”‚  (Voice β†’ Text)  β”‚               β”‚
         β”‚              β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜               β”‚
         β”‚                       β”‚                         β”‚
         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                         β”‚
                     β–Ό                                     β–Ό
          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
          β”‚  LangGraph Client   β”‚              β”‚  LangGraph Client  β”‚
          β”‚  (Aegra Server)     β”‚              β”‚  (Location Update) β”‚
          β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
          β”‚  β”‚ MCP Tool      β”‚  β”‚
          β”‚  β”‚ Servers (7+)  β”‚  β”‚
          β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                     β”‚
        β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
        β–Ό            β–Ό            β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ MongoDB  β”‚  β”‚ Reviewer  β”‚  β”‚ WhatsApp API β”‚
β”‚ (State)  β”‚  β”‚ System    β”‚  β”‚ (Outbound)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

For complete architecture documentation including data flow diagrams and sequence diagrams, see docs/architecture.md.


Tech Stack

Layer Technology
Runtime Node.js 20 (Debian Bookworm Slim)
Framework NestJS 11 with CQRS (@nestjs/cqrs)
Language TypeScript 5.7
Database MongoDB 7 (Mongoose 9)
Cache Redis 7 (ioredis) β€” referenced but not actively used
AI Orchestration LangGraph SDK (@langchain/langgraph-sdk) via Aegra server
LLM Providers OpenAI-compatible APIs, Anthropic (Claude), Google Gemini
Voice AI Sarvam AI (STT/TTS), Gemini Live (real-time voice)
WebRTC werift (Node.js WebRTC), @discordjs/opus
Protocol MCP (Model Context Protocol) for tool integration
Secrets Infisical (production), .env (development)
Container Docker (single-stage), docker-compose
CI/CD GitHub Actions β†’ Docker Hub β†’ GCP VM via Tailscale

Quick Start

Prerequisites

  • Node.js β‰₯ 20.x
  • MongoDB β‰₯ 7.x (local or cloud β€” e.g., MongoDB Atlas)
  • Redis β‰₯ 7.x (optional β€” not actively used)
  • Meta WhatsApp Business API account with verified phone number
  • API Keys: Sarvam AI, Gemini, LLM provider, LangGraph/Aegra server

1. Clone & Install

git clone <repository-url>
cd wa-client
npm install

2. Configure Environment

cp .env.example .env
cp config.example.yaml config.yaml

Edit .env with your secrets (API keys, tokens, MongoDB URI). Edit config.yaml for non-secret configuration (system prompts, MCP servers, feature flags).

See docs/setup.md for detailed configuration guide.

3. Start Infrastructure

# Option A: Docker (MongoDB + Redis)
npm run docker:dev

# Option B: Use managed cloud services
# Set MONGO_URI in .env to your Atlas connection string
npm run start:dev

4. Configure WhatsApp Webhook

  1. Go to Meta Developer Dashboard
  2. Set webhook URL: https://yourdomain.com/whatsapp/webhook
  3. Set verify token to match WHATSAPP_WEBHOOK_VERIFY_TOKEN in .env
  4. Subscribe to messages and calls fields

5. Run

# Development (with hot-reload)
npm run start:dev

# Production
npm run build
npm run start:prod

Project Structure

wa-client/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main.ts                          # Application bootstrap
β”‚   β”œβ”€β”€ app.module.ts                    # Root module (Config, MongoDB, WhatsApp)
β”‚   β”œβ”€β”€ config/                          # Configuration subsystem
β”‚   β”‚   β”œβ”€β”€ configuration.ts             # YAML loader with env overrides
β”‚   β”‚   β”œβ”€β”€ config.schema.ts             # class-validator schemas
β”‚   β”‚   β”œβ”€β”€ validate-config.ts           # Validation logic
β”‚   β”‚   β”œβ”€β”€ app-config.service.ts        # Typed config accessor
β”‚   β”‚   └── index.ts                     # Barrel exports
β”‚   └── whatsapp/                        # Core WhatsApp module
β”‚       β”œβ”€β”€ whatsapp.module.ts           # Module registration
β”‚       β”œβ”€β”€ whatsapp.controller.ts       # HTTP endpoints (webhook, API)
β”‚       β”œβ”€β”€ manual-outbound-message.ts   # Expert message formatting
β”‚       β”œβ”€β”€ whatsapp-api/                # Meta Graph API integration
β”‚       β”‚   β”œβ”€β”€ whatsapp.service.ts      # Send text/voice/location/media
β”‚       β”‚   β”œβ”€β”€ whatsapp.config.ts       # API URL construction
β”‚       β”‚   └── whatsapp-api.module.ts   # Module
β”‚       β”œβ”€β”€ conversations/               # CQRS conversation pipeline
β”‚       β”‚   β”œβ”€β”€ conversation.module.ts   # Module registration
β”‚       β”‚   β”œβ”€β”€ langgraph-client.service.ts  # LangGraph SDK wrapper
β”‚       β”‚   β”œβ”€β”€ langgraph.module.ts      # Module
β”‚       β”‚   └── application/             # Command handlers
β”‚       β”‚       β”œβ”€β”€ add-user-text-message/
β”‚       β”‚       β”œβ”€β”€ add-user-voice-message/
β”‚       β”‚       └── set-user-location/
β”‚       β”œβ”€β”€ calling/                     # Real-time VoIP module
β”‚       β”‚   β”œβ”€β”€ calling.service.ts       # WebRTC lifecycle & RTP pacing
β”‚       β”‚   β”œβ”€β”€ gemini-live.service.ts   # Gemini Live WebSocket bridge
β”‚       β”‚   β”œβ”€β”€ audio-codec.service.ts   # Opus ↔ PCM transcoding
β”‚       β”‚   β”œβ”€β”€ mcp-tools.service.ts     # MCP tool discovery for voice
β”‚       β”‚   └── calling.module.ts        # Module
β”‚       β”œβ”€β”€ pending-questions/           # Expert review pipeline
β”‚       β”‚   β”œβ”€β”€ reviewer-polling.service.ts      # Cron polling + webhook handler
β”‚       β”‚   β”œβ”€β”€ reviewer-answer-localization.service.ts # Multi-lingual translation
β”‚       β”‚   β”œβ”€β”€ pending-question.schema.ts       # Mongoose schema
β”‚       β”‚   β”œβ”€β”€ pending-question.repository.ts   # Abstract repository
β”‚       β”‚   β”œβ”€β”€ mongo-pending-question.repository.ts # MongoDB implementation
β”‚       β”‚   └── pending-questions.module.ts       # Module
β”‚       β”œβ”€β”€ access-control/             # Whitelist/Blacklist system
β”‚       β”‚   β”œβ”€β”€ access-control.service.ts
β”‚       β”‚   β”œβ”€β”€ whitelist.schema.ts
β”‚       β”‚   β”œβ”€β”€ blacklist.schema.ts
β”‚       β”‚   └── access-control.module.ts
β”‚       β”œβ”€β”€ sarvam-api/                 # Speech-to-Text & Text-to-Speech
β”‚       β”‚   β”œβ”€β”€ sarvam.service.ts
β”‚       β”‚   └── sarvam.module.ts
β”‚       └── user-stats/                 # User analytics
β”‚           β”œβ”€β”€ whatsapp-user.schema.ts
β”‚           β”œβ”€β”€ whatsapp-user.repository.ts
β”‚           β”œβ”€β”€ mongo-whatsapp-user.repository.ts
β”‚           └── user-stats.module.ts
β”œβ”€β”€ test/                               # E2E tests
β”œβ”€β”€ config.yaml                         # Non-secret configuration
β”œβ”€β”€ config.example.yaml                 # Example config (version-controlled)
β”œβ”€β”€ .env                                # Secrets (git-ignored)
β”œβ”€β”€ .env.example                        # Secret template
β”œβ”€β”€ Dockerfile                          # Production container
β”œβ”€β”€ docker-compose.yml                  # Local dev infrastructure
β”œβ”€β”€ .github/workflows/                  # CI/CD
β”‚   └── dockerhub-build.yml             # Build β†’ Deploy pipeline
└── package.json                        # Dependencies & scripts

API Endpoints

Method Path Auth Description
GET /whatsapp/webhook Verify Token WhatsApp webhook subscription verification
POST /whatsapp/webhook HMAC-SHA256 Incoming messages, statuses, and call events
GET /whatsapp/health None Health check
POST /whatsapp/send-message x-internal-api-key Send outbound message (expert/admin)
POST /whatsapp/reviewer-webhook x-internal-api-key Receive real-time expert answers
GET /whatsapp/test-poll x-internal-api-key Manually trigger reviewer polling
GET /whatsapp/users/count x-internal-api-key Unique user count
GET /whatsapp/users x-internal-api-key Paginated user list

Full API documentation: docs/api-reference.md


Configuration

The application uses a dual-layer configuration system:

Layer File Purpose
Secrets .env API keys, tokens, database URIs β€” never committed
Config config.yaml System prompts, MCP servers, feature flags, thresholds β€” safe to commit

Environment variables in .env can override config.yaml values at runtime.

Full configuration reference: docs/setup.md


Deployment

Production deployment uses:

  • Docker containers on a GCP VM
  • Infisical for secrets management in production
  • Tailscale mesh VPN for secure CI/CD access
  • GitHub Actions for automated build β†’ push β†’ deploy pipeline
# Manual Docker deployment
docker build -t wa-bot .
docker run -d --env-file .env -p 3000:3000 wa-bot

# Via docker-compose (includes MongoDB + Redis)
docker-compose up -d

Full deployment guide: docs/deployment.md


Documentation

Document Description
Architecture System design, data flows, sequence diagrams
Setup Guide Prerequisites, environment variables, local development
API Reference All endpoints, payloads, authentication
Database MongoDB collections, schemas, indexes
Deployment CI/CD, Docker, infrastructure, rollback
Security Auth, secrets, webhook verification
Troubleshooting Common errors and resolution steps

Contributing

  1. Create a feature branch from main
  2. Follow the existing NestJS module pattern (module β†’ service β†’ repository)
  3. Use the CQRS pattern for new message-handling flows
  4. Add entries to config.yaml for new configurable values
  5. Keep secrets in .env only
  6. Run npm run lint and npm run format before committing
  7. See docs/architecture.md for detailed developer guidance

License

UNLICENSED β€” Proprietary. All rights reserved by Annam.AI Foundation.

About

No description, website, or topics provided.

Resources

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages