Skip to content

πŸ€– πŸ’¬ Node.js integration for WhatsApp Business API and AI21 Studio, enabling real-time AI responses with Docker and ngrok support via Express.js

Notifications You must be signed in to change notification settings

sergio-santiago/ai-whatsapp-integration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

15 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

AI21 WhatsApp Chatbot Integration

A Node.js application that integrates AI21 Studio with WhatsApp Business API to provide AI-powered responses to WhatsApp messages.

πŸ”— Includes ngrok integration for exposing your local webhook to the internet.

Requirements

  • Docker & Docker Compose
  • Node.js 18+ (for local development without Docker)
  • WhatsApp Business API account
  • AI21 Studio API key

Setup

  1. Clone this repository
  2. Copy the .env.example file to .env:
    cp .env.example .env
    
  3. Edit the .env file with your credentials provided in the next step

Environment Variables Setup

AI21 Studio API Key (AI21_API_KEY)

  1. Create an account at AI21 Studio
  2. Go to your Workspace API keys
  3. Copy your API key from the "API Key" section

WhatsApp Business API Configuration

To get the PHONE_NUMBER_ID and WHATSAPP_TOKEN:

  1. Go to Meta for Developers
  2. Create or select your app (Choose "Business" type)
  3. Add WhatsApp product to your app
  4. Go to WhatsApp > Getting Started
  5. Set up your WhatsApp business account if you haven't already
  6. Generate and copy your WHATSAPP_TOKEN from the "Temporary access token" section
  7. Find your PHONE_NUMBER_ID in the "From" field (Number ID, not the number itself)
  8. Configure your webhook URL using the ngrok URL (see "Using ngrok for Webhooks" section below)

Verification Token (VERIFY_TOKEN)

This is a custom string that you create yourself. It should be:

  • Random and unique
  • At least 32 characters long
  • Kept secret and secure

Example of generating a secure token:

# Using OpenSSL (recommended)
openssl rand -hex 32

# Or using Node.js
node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"

When configuring your webhook in the Meta for Developers dashboard, you'll need to provide this same VERIFY_TOKEN value.

Ngrok Authentication Token (NGROK_AUTHTOKEN)

  1. Sign up or log in to your ngrok dashboard
  2. Navigate to the "Your Authtoken" section
  3. Copy your authentication token
  4. Add it to your .env file as NGROK_AUTHTOKEN

Running with Docker

This project includes Docker configuration for easy deployment.

Using Make Commands

The project includes a Makefile with common commands:

# Build the Docker image
make build

# Run the application
make run

# View logs
make logs

# Stop the application
make stop

# For development with live reloading
make dev

# See all available commands
make help

Using Docker Compose Directly

# Build and start the application
docker-compose up -d

# Stop the application
docker-compose down

Running Locally (Without Docker)

# Install dependencies
npm install

# Start development server with hot reloading
npm run dev

# Start production server
node index.js

Using ngrok for Webhooks

The project includes ngrok as a Docker service to expose your local webhook to the internet.

  1. Make sure you have added your ngrok authentication token to the .env file:

    NGROK_AUTHTOKEN=your_ngrok_auth_token
    
  2. Start the application with ngrok:

    make init
  3. Get your public webhook URL:

    make ngrok-url
  4. Use the URL displayed (ending with /webhook) to configure your WhatsApp Business API webhook.

  5. View ngrok logs to debug connections:

    make ngrok-logs

Project Structure

β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ config/          # Configuration settings
β”‚   β”œβ”€β”€ routes/          # API route definitions
β”‚   └── services/        # Business logic
β”œβ”€β”€ .env                 # Environment variables (create from .env.example)
β”œβ”€β”€ .env.example        # Example environment file
β”œβ”€β”€ Dockerfile          # Docker configuration
β”œβ”€β”€ docker-compose.yml  # Docker Compose configuration (with ngrok service)
β”œβ”€β”€ Makefile            # Make commands
└── index.js            # Application entry point

About

πŸ€– πŸ’¬ Node.js integration for WhatsApp Business API and AI21 Studio, enabling real-time AI responses with Docker and ngrok support via Express.js

Topics

Resources

Stars

Watchers

Forks