DEV Community

Arie Barbaro
Arie Barbaro

Posted on

How to Build a WhatsApp AI Chatbot in 10 Minutes Using Python + GPT-4

How to Build a WhatsApp AI Chatbot in 10 Minutes Using Python + GPT-4

I built a complete WhatsApp AI chatbot that answers questions, captures leads, and takes orders — all powered by GPT-4. Here's the full tutorial.


What You'll Build

A fully functional WhatsApp bot that:

  • Connects to WhatsApp via QR code scan (no API approval needed)
  • Uses GPT-4 to answer customer questions naturally
  • Captures leads and stores them
  • Takes simple orders via WhatsApp
  • Runs 24/7 on any server

Why WhatsApp?

WhatsApp is the #1 messaging app in Latin America, Europe, and many other markets. Small businesses lose customers because they can't respond fast enough. An AI bot solves this problem instantly.

Prerequisites

  • Python 3.10+
  • An OpenAI API key
  • A phone to scan the QR code (one-time setup)

Step 1: Install Dependencies

pip install openai python-dotenv
Enter fullscreen mode Exit fullscreen mode

Step 2: Configure Your Bot

Create a .env file:

OPENAI_API_KEY=sk-your-key-here
Enter fullscreen mode Exit fullscreen mode

Step 3: The Bot Logic

Here's the core of the bot — a simple Python script that bridges WhatsApp and GPT-4:

import openai
from dotenv import load_dotenv
import os

load_dotenv()

client = openai.OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

def generate_response(user_message: str, chat_history: list) -> str:
    messages = [
        {"role": "system", "content": "You are a helpful business assistant."},
    ]
    messages.extend(chat_history[-10:])
    messages.append({"role": "user", "content": user_message})

    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages,
        max_tokens=500,
        temperature=0.7
    )
    return response.choices[0].message.content
Enter fullscreen mode Exit fullscreen mode

Step 4: Connect to WhatsApp

The bot uses a Node.js bridge that connects to WhatsApp Web:

const { default: makeWASocket } = require('@whiskeysockets/baileys');

async function main() {
    const sock = makeWASocket({});
    sock.ev.on('messages.upsert', async ({ messages }) => {
        for (const msg of messages) {
            if (msg.message && !msg.key.fromMe) {
                const response = await fetch('http://localhost:5000/chat', {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ message: msg.message.conversation })
                });
                const data = await response.json();
                await sock.sendMessage(msg.key.remoteJid, { text: data.response });
            }
        }
    });
}
main();
Enter fullscreen mode Exit fullscreen mode

Step 5: Run It

python wa_bot_builder.py
node whatsapp_bridge.js
Enter fullscreen mode Exit fullscreen mode

Scan the QR code with your WhatsApp phone, and you're live!

Features

Feature Description
AI Responses GPT-4 powered natural language
Lead Capture Automatically saves customer info
Order Taking Simple order flow via WhatsApp
Multi-language Responds in customer's language

Get the Complete Code

I've packaged the entire working bot with documentation (Spanish included), configuration wizard, and deployment guide.

Get it here: https://wa-bot-builder.manus.space

Only $9 — cheaper than a coffee, and it keeps working 24/7.


Also offering a setup service where I install and configure the bot on your server for $20 — delivered in 24 hours. DM me!

Top comments (0)