WhatsApp API + ChatGPT: Buat Bot AI Sendiri

Tutorial membuat bot WhatsApp dengan ChatGPT/GPT-4. AI conversational bot yang cerdas. Step by step dengan code!

WhatsApp API + ChatGPT
WhatsApp API + ChatGPT

WhatsApp + ChatGPT = Bot AI yang bisa ngobrol natural!

Kombinasi ini memungkinkan bot yang mengerti konteks, bisa jawab pertanyaan kompleks, dan terasa seperti chat dengan manusia.


Architecture

┌─────────────┐     ┌─────────────┐     ┌─────────────┐
│  WhatsApp   │────►│  Your Bot   │────►│   OpenAI    │
│  Customer   │◄────│   Server    │◄────│   GPT API   │
└─────────────┘     └─────────────┘     └─────────────┘

Setup

1. Install Dependencies:

bash

npm install @whiskeysockets/baileys openai dotenv

2. Environment Variables:

env

OPENAI_API_KEY=sk-your-api-key-here

3. Basic Integration:

javascript

const { makeWASocket, useMultiFileAuthState } = require('@whiskeysockets/baileys');
const OpenAI = require('openai');
require('dotenv').config();

const openai = new OpenAI({
    apiKey: process.env.OPENAI_API_KEY
});

// Store conversation history per user
const conversations = new Map();

async function startBot() {
    const { state, saveCreds } = await useMultiFileAuthState('./auth_info');
    
    const sock = makeWASocket({
        auth: state,
        printQRInTerminal: true
    });
    
    sock.ev.on('creds.update', saveCreds);
    
    sock.ev.on('messages.upsert', async (m) => {
        const message = m.messages[0];
        if (!message.message || message.key.fromMe) return;
        
        const from = message.key.remoteJid;
        const text = message.message.conversation || 
                     message.message.extendedTextMessage?.text || '';
        
        if (!text) return;
        
        // Get AI response
        const response = await getAIResponse(from, text);
        
        // Send reply
        await sock.sendMessage(from, { text: response });
    });
    
    return sock;
}

async function getAIResponse(userId, userMessage) {
    // Get or create conversation history
    if (!conversations.has(userId)) {
        conversations.set(userId, []);
    }
    
    const history = conversations.get(userId);
    
    // Add user message to history
    history.push({ role: 'user', content: userMessage });
    
    // Keep only last 10 messages for context
    if (history.length > 10) {
        history.splice(0, history.length - 10);
    }
    
    try {
        const completion = await openai.chat.completions.create({
            model: 'gpt-4o-mini',
            messages: [
                {
                    role: 'system',
                    content: `Kamu adalah asisten customer service untuk Toko Online.
                    Jawab dengan ramah, singkat, dan helpful.
                    Gunakan bahasa Indonesia yang casual.
                    Jika tidak tahu, bilang akan dihubungkan ke admin.`
                },
                ...history
            ],
            max_tokens: 500,
            temperature: 0.7
        });
        
        const aiResponse = completion.choices[0].message.content;
        
        // Add AI response to history
        history.push({ role: 'assistant', content: aiResponse });
        
        return aiResponse;
    } catch (error) {
        console.error('OpenAI Error:', error);
        return 'Maaf, terjadi kesalahan. Coba lagi nanti ya!';
    }
}

startBot();

Custom System Prompt

E-commerce Bot:

javascript

const systemPrompt = `Kamu adalah CS bot untuk Toko Fashion Online "StyleKu".

INFORMASI TOKO:
- Produk: Baju, celana, aksesoris
- Harga range: Rp 50.000 - Rp 500.000
- Pengiriman: JNE, J&T, SiCepat
- Jam operasional: 09.00-21.00 WIB
- Pembayaran: Transfer bank, COD

TUGAS:
1. Jawab pertanyaan tentang produk
2. Bantu proses order
3. Info pengiriman dan pembayaran
4. Jika pertanyaan di luar kemampuan, arahkan ke admin

GAYA BAHASA:
- Ramah dan casual
- Gunakan emoji secukupnya
- Jawab singkat dan jelas
- Panggil customer dengan "Kak"`;

FAQ Bot:

javascript

const systemPrompt = `Kamu adalah bot FAQ untuk Klinik Sehat.

KNOWLEDGE BASE:
- Jam buka: Senin-Sabtu 08.00-20.00
- Dokter umum: dr. Andi, dr. Budi
- Spesialis: dr. Citra (anak), dr. Dian (kandungan)
- Pendaftaran: Via WA atau datang langsung
- BPJS: Diterima

RULES:
1. JANGAN memberikan diagnosis medis
2. JANGAN merekomendasikan obat
3. Untuk keluhan medis, sarankan konsultasi dokter
4. Jawab hanya seputar informasi klinik`;

Advanced Features

Context-Aware Response:

javascript

async function getContextualResponse(userId, userMessage, customerData) {
    const context = `
INFORMASI CUSTOMER:
- Nama: ${customerData.name || 'Tidak diketahui'}
- Order terakhir: ${customerData.lastOrder || 'Belum pernah order'}
- Status member: ${customerData.memberStatus || 'Regular'}
`;
    
    const completion = await openai.chat.completions.create({
        model: 'gpt-4o-mini',
        messages: [
            { role: 'system', content: systemPrompt + context },
            ...history,
            { role: 'user', content: userMessage }
        ]
    });
    
    return completion.choices[0].message.content;
}

Function Calling:

javascript

const tools = [
    {
        type: 'function',
        function: {
            name: 'check_order_status',
            description: 'Cek status order berdasarkan nomor order',
            parameters: {
                type: 'object',
                properties: {
                    order_number: {
                        type: 'string',
                        description: 'Nomor order, contoh: ORD-12345'
                    }
                },
                required: ['order_number']
            }
        }
    },
    {
        type: 'function',
        function: {
            name: 'get_product_info',
            description: 'Dapatkan informasi produk',
            parameters: {
                type: 'object',
                properties: {
                    product_name: {
                        type: 'string',
                        description: 'Nama produk yang dicari'
                    }
                },
                required: ['product_name']
            }
        }
    }
];

async function getAIResponseWithTools(userId, userMessage) {
    const completion = await openai.chat.completions.create({
        model: 'gpt-4o-mini',
        messages: [
            { role: 'system', content: systemPrompt },
            { role: 'user', content: userMessage }
        ],
        tools: tools,
        tool_choice: 'auto'
    });
    
    const responseMessage = completion.choices[0].message;
    
    // Check if AI wants to call a function
    if (responseMessage.tool_calls) {
        const toolCall = responseMessage.tool_calls[0];
        const functionName = toolCall.function.name;
        const args = JSON.parse(toolCall.function.arguments);
        
        let functionResult;
        
        switch (functionName) {
            case 'check_order_status':
                functionResult = await checkOrderStatus(args.order_number);
                break;
            case 'get_product_info':
                functionResult = await getProductInfo(args.product_name);
                break;
        }
        
        // Send function result back to GPT
        const secondCompletion = await openai.chat.completions.create({
            model: 'gpt-4o-mini',
            messages: [
                { role: 'system', content: systemPrompt },
                { role: 'user', content: userMessage },
                responseMessage,
                {
                    role: 'tool',
                    tool_call_id: toolCall.id,
                    content: JSON.stringify(functionResult)
                }
            ]
        });
        
        return secondCompletion.choices[0].message.content;
    }
    
    return responseMessage.content;
}

Cost Optimization

Use GPT-4o-mini:

javascript

// GPT-4o-mini: ~$0.15/1M input tokens
// GPT-4o: ~$2.50/1M input tokens

// Untuk kebanyakan use case, gpt-4o-mini sudah cukup!
model: 'gpt-4o-mini'

Limit Context:

javascript

// Batasi history untuk hemat token
const MAX_HISTORY = 6; // 3 exchanges

if (history.length > MAX_HISTORY) {
    history.splice(0, history.length - MAX_HISTORY);
}

Cache Common Responses:

javascript

const responseCache = new Map();

async function getCachedOrAIResponse(query) {
    // Check cache for common queries
    const cacheKey = query.toLowerCase().trim();
    
    if (responseCache.has(cacheKey)) {
        return responseCache.get(cacheKey);
    }
    
    const response = await getAIResponse(query);
    
    // Cache if it's a common question
    if (isCommonQuestion(query)) {
        responseCache.set(cacheKey, response);
    }
    
    return response;
}

Hybrid Approach

javascript

async function handleMessage(from, text) {
    // 1. Check exact keyword matches first (free!)
    const keywordResponse = checkKeywords(text);
    if (keywordResponse) {
        return keywordResponse;
    }
    
    // 2. Check FAQ database
    const faqResponse = await searchFAQ(text);
    if (faqResponse && faqResponse.confidence > 0.8) {
        return faqResponse.answer;
    }
    
    // 3. Fall back to AI for complex queries
    return await getAIResponse(from, text);
}

function checkKeywords(text) {
    const keywords = {
        'harga': 'Untuk daftar harga, ketik KATALOG',
        'jam buka': 'Jam operasional: 09.00-21.00 WIB',
        'rekening': 'Transfer ke BCA 1234567890 a.n. Toko'
    };
    
    for (const [keyword, response] of Object.entries(keywords)) {
        if (text.toLowerCase().includes(keyword)) {
            return response;
        }
    }
    
    return null;
}

Best Practices

1. Set Clear Boundaries

javascript

// Dalam system prompt
"JANGAN pernah:
- Memberikan informasi palsu
- Menjanjikan sesuatu di luar kemampuan
- Membahas topik sensitif (politik, SARA)
- Mengakui sebagai manusia"

2. Graceful Fallback

javascript

if (response.includes('tidak tahu') || response.includes('tidak yakin')) {
    return 'Untuk pertanyaan ini, lebih baik langsung chat admin ya. Ketik ADMIN untuk connect.';
}

3. Monitor Usage

javascript

// Track API usage
let tokenUsage = { prompt: 0, completion: 0 };

const completion = await openai.chat.completions.create({...});

tokenUsage.prompt += completion.usage.prompt_tokens;
tokenUsage.completion += completion.usage.completion_tokens;

console.log(`Daily tokens: ${tokenUsage.prompt + tokenUsage.completion}`);

FAQ

Berapa biaya per chat?

GPT-4o-mini: ~Rp 1-5 per conversation. GPT-4o: ~Rp 10-50 per conversation. Tergantung panjang chat.

Apakah AI bisa salah?

Ya! AI bisa hallucinate. Selalu set boundaries dan sediakan fallback ke human.

Model mana yang terbaik?

GPT-4o-mini untuk cost-effective. GPT-4o untuk accuracy tinggi. Claude untuk nuance.


Kesimpulan

WA + AI = Smart bot dengan budget terjangkau!

ApproachCostIntelligence
Keyword onlyFreeLow
HybridLowMedium
Full AIMediumHigh

Start hybrid, scale to full AI!

Build AI Bot →


Artikel Terkait