Unified SMS hotline for Filipino barangays that blends Alibaba Cloud SMS + LLM services with a lightweight RAG pipeline, Prisma data layer, and Mantine-powered dashboard.
- Next.js 16 (App Router, TypeScript)
- Mantine UI v8, Mantine Forms, Zustand state management
- Prisma ORM + SQLite (swap to Alibaba Cloud RDS when ready)
- Alibaba text-embedding-v3 (1024 dims) + DashScope LLM (qwen-flash)
- Multi-channel support: SMS (Dysms), Facebook Messenger, Email (IMAP)
- Document processing: pdf-parse, mammoth, tesseract.js (OCR)
src/
app/ # Next.js routes (dashboard + API)
components/ # Mantine UI building blocks
lib/ # Logger, formatters, RAG, SMS helpers
server/ # Prisma client + domain services
store/ # Zustand stores
types/ # Shared DTOs
prisma/schema.prisma # Database models
uploads/ # User-uploaded documents (PDF, DOCX, images)
- Install dependencies
pnpm install
- Environment variables – check
src/env.tsfor all options:# Required DATABASE_URL="file:./dev.db" ALIBABA_DASHSCOPE_API_KEY=sk-xxx # DashScope LLM + embeddings # SMS (optional, logs to console if missing) ALIBABA_SMS_ACCESS_KEY_ID=... ALIBABA_SMS_ACCESS_KEY_SECRET=... ALIBABA_SMS_SIGN_NAME=BarangayAI ALIBABA_SMS_TEMPLATE_CODE=SMS_123456789 # Multi-channel (optional) MESSENGER_PAGE_ACCESS_TOKEN=... # Facebook Messenger MESSENGER_VERIFY_TOKEN=... MESSENGER_APP_SECRET=...
- Database setup
pnpm db:push # create tables - Run locally
Visit
pnpm dev
http://localhost:3000for the Mantine dashboard.
POST /api/sms-webhook– Alibaba SMS callbacks. Auto-classifies reports or answers FAQs via RAGPOST /api/messenger-webhook– Facebook Messenger integration (verify & receive messages)POST /api/email-check– Manual trigger to poll IMAP inbox and process emailsPOST /api/voice-message– Voice-to-text transcription + processing (future feature)
GET /api/reports– List all reports with filtersPOST /api/reports– Create report manually (dashboard form)GET /api/reports/[id]– Get single reportPATCH /api/reports/[id]– Update report (status, priority, resolution)POST /api/documents/upload– Upload PDF/DOCX/image, extract text, generate embeddingsGET /api/documents– List all knowledge base documentsGET /api/documents/[id]/file– Download or preview uploaded file
GET /api/conversations– View all message threads grouped by sender
Upload Workflow:
- Navigate to Dashboard → Documents (
/dashboard/documents) - Click "Upload Document" and select PDF, DOCX, TXT, or image file
- System automatically:
- Extracts text (pdf-parse for PDF, mammoth for DOCX, tesseract.js OCR for images)
- Falls back to OCR if text extraction yields <50 chars (scanned documents)
- Chunks content into ~500 char segments
- Generates 1024-dim embeddings via Alibaba text-embedding-v3
- Stores in
DocumentChunktable for similarity search
Query Processing:
- User asks question via SMS/Messenger/Email
- System embeds query → cosine similarity search → top 4 chunks
- If confidence > 0.65: LLM generates answer with context
- If confidence < 0.65: "Please check with the barangay hall" fallback
Supported Formats: PDF, DOCX, TXT, PNG, JPG, GIF
Alibaba DashScope (via OpenAI SDK):
text-embedding-v3for 1024-dim semantic embeddings (TF-IDF fallback)qwen-flashfor LLM chat completions (context fallback)- JSON-structured classification with regex fallback
Document Processing:
pdf-parsefor text-based PDFsmammothfor DOCX filestesseract.jsfor OCR (scanned docs, images) - multi-language support
Multi-Channel:
- SMS:
@alicloud/pop-coreDysms API (logs if credentials missing) - Messenger: Facebook Graph API webhooks
- Email:
imaplibrary for polling (receive-only, send via dashboard)
- Hosting – Deploy on ECS, Serverless App Engine, or Function Compute (Node.js 20+)
- Database – Switch
DATABASE_URLto ApsaraDB RDS PostgreSQL URI, updateprisma/schema.prismaprovider - Webhooks – Expose publicly:
/api/sms-webhook→ Configure in Alibaba SMS console/api/messenger-webhook→ Configure in Facebook Developer portal
- Secrets – Store in Alibaba Cloud KMS:
ALIBABA_DASHSCOPE_API_KEY, SMS credentials, Messenger tokens - File Storage – Mount persistent volume for
uploads/directory or use OSS bucket - Knowledge Base – Admins upload documents via
/dashboard/documents(no CLI required)
pnpm dev # Start Next.js dev server (port 3000)
pnpm build # Production build
pnpm start # Serve production build
pnpm lint # ESLint type-safe checks
pnpm db:push # Sync Prisma schema to database (no migration)
pnpm db:generate # Regenerate Prisma client after schema changes
pnpm db:migrate # Create named migration (use before production)- Go to
/dashboard/documents - Upload a PDF with barangay policies (e.g., curfew rules, office hours)
- System extracts text, chunks, and embeds automatically
- Go to
/simulatoror POST to/api/sms-webhook:{ "phoneNumber": "09171234567", "message": "Broken streetlight near barangay hall", "skipSmsReply": true } - Check
/dashboard/reportsfor auto-classified incident (category, priority)
- Send FAQ via simulator: "What is the curfew for minors?"
- System retrieves relevant chunks → LLM generates answer from uploaded docs
- View conversation thread in
/dashboard/conversations
- Configure Facebook Messenger webhook → Send messages via Messenger
- Configure email IMAP → POST to
/api/email-checkto process inbox
Happy hacking! 🎯