Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Joyin2/whatsapp-bot

Repository files navigation

# WhatsApp Chatbot A production-ready WhatsApp chatbot built with Next.js that integrates with WhatsApp Cloud API, OpenAI, and Google Sheets. ## Features - WhatsApp webhook verification and message handling - Rule-based replies (free) - AI fallback for complex queries (OpenAI) - Lead saving to Google Sheets - Multi-coaching centre support - WhatsApp safety compliance ## Architecture ``` WhatsApp Message ↓ Meta Webhook ↓ Next.js API Route ↓ Rule Engine (FREE) ↓ AI (ONLY if needed) ↓ WhatsApp Reply ↓ Google Sheet Lead Save ``` ## Prerequisites - WhatsApp Cloud API - Phone Number ID - Permanent Access Token - OpenAI API key - Google Service Account for Google Sheets ## Installation 1. Install dependencies: ```bash npm install ``` 2. Set up environment variables in `.env.local`: ```env WHATSAPP_TOKEN=YOUR_PERMANENT_TOKEN PHONE_NUMBER_ID=YOUR_PHONE_NUMBER_ID VERIFY_TOKEN=verify123 OPENAI_API_KEY=sk-xxxx GOOGLE_SHEET_ID=xxxx [email protected] GOOGLE_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n" ``` 3. Run the development server: ```bash npm run dev ``` ## Environment Variables - `WHATSAPP_TOKEN`: Your permanent access token from WhatsApp Cloud API - `PHONE_NUMBER_ID`: Your WhatsApp phone number ID - `VERIFY_TOKEN`: Token used to verify webhook requests - `OPENAI_API_KEY`: Your OpenAI API key - `GOOGLE_SHEET_ID`: Google Sheet ID where leads will be stored - `GOOGLE_CLIENT_EMAIL`: Google service account email - `GOOGLE_PRIVATE_KEY`: Google service account private key ## Deployment This app can be deployed on Vercel, Railway, or Render. Just set the environment variables and deploy. ## WhatsApp Safety Rules - ✅ Reply only after customer message - ✅ No spam - ✅ No bulk sending - ✅ Short replies - ✅ No links initially Following these rules will prevent your WhatsApp number from being banned. ## Cost - Rule replies: Free - AI fallback: ₹100–₹300/month - WhatsApp: Free - Hosting: Free (with free tier) ## File Structure - `app/api/webhook/route.js`: WhatsApp webhook handler - `lib/handler.js`: Main message processing logic - `lib/rules.js`: Rule-based reply definitions - `lib/matchRule.js`: Rule matching logic - `lib/ai.js`: AI integration - `lib/sheet.js`: Google Sheets integration# whatsapp-bot

Releases

No releases published

Packages

No packages published