A Next.js-based Facebook Messenger chatbot that uses AI to process text and images, maintain conversation context, and provide intelligent responses. The bot can understand images and incorporate them into the conversation flow.
- 🤖 AI-powered conversations using Groq's LLM models
- 🖼️ Image understanding and analysis
- 💬 Maintains conversation history for contextual responses
- ⚡ Fast response times with asynchronous processing
- 🔄 Handles Facebook's split message delivery
- 📝 Persistent storage of conversations using PostgreSQL
- ✨ Type-safe implementation using TypeScript
- Node.js 18 or higher
- PostgreSQL database
- Facebook Developer Account
- Groq API account
Create a .env
file in the root directory with the following variables:
# DATABASE
DATABASE_URL="postgresql://postgres:password@localhost:5432/postgres"
# AI SERVICES
GROQ_API_KEY=
# FACEBOOK
FACEBOOK_APP_ID=
FACEBOOK_APP_SECRET=
FACEBOOK_PAGE_ID=
FACEBOOK_PAGE_ACCESS_TOKEN=
FACEBOOK_VERIFICATION_TOKEN=
- Clone the repository:
git clone <repository-url>
cd facebook-messenger-ai-bot
- Install dependencies:
npm install
- Set up the database:
npx prisma migrate dev
- Run the development server:
npm run dev
-
Create a Facebook App in the Facebook Developers Console
-
Set up Messenger in your Facebook App:
- Configure webhooks
- Generate a Page Access Token
- Set up necessary permissions
-
Configure Webhook URL:
- URL:
https://your-domain.com/api/webhook
- Verify Token: Use the same token as in your FACEBOOK_VERIFICATION_TOKEN
- Subscribe to: messages, messaging_postbacks
- URL:
/app
: Contains the Next.js application and API routes/lib
: Core functionality split into logical modules:ai-common.ts
: Shared AI-related utilitiesanalyze-images.ts
: Image processing and analysisgenerate-message-response.ts
: AI response generationprocess-message.ts
: Main message handling logic
/types
: TypeScript type definitions for the project/prisma
: Database configuration and schema
- Admin Dashboard
- View / Reply to messages
- Prompt Settings
- Multi-Platform Support (WhatsApp & Instagram)