Build a WhatsApp AI Chatbot with ChatGPT & APIs

Learn how to connect ChatGPT with WhatsApp to build an AI-powered chatbot for customer service. Explore both code-based and no-code integration methods that follow WhatsApp’s official API guidelines.

Diagram showing the components of building a WhatsApp AI Chatbot
How to Build a Custom WhatsApp AI Chatbot with the WA-MExpress API

Modern customers expect instant answers, 24/7. But how can you provide real-time, intelligent support without overwhelming your team? The answer is a custom AI chatbot that lives on WhatsApp.

While our no-code AI Agent feature is perfect for most businesses, developers often need more power and flexibility. This guide will walk you through the core concepts of using the WA-MExpress platform to build a fully custom WhatsApp chatbot connected to any AI model, like OpenAI's GPT-4.

The Two Core Components

A custom chatbot has two main jobs: listening for incoming messages and sending replies. WA-MExpress provides powerful, developer-friendly tools for both.

1. The "Ears": Receiving Messages with Webhooks

You can't build a bot if you don't know when a user messages you. A Webhook is the solution. When a user sends a message to your WhatsApp number, WA-MExpress instantly sends a notification with the message content to a URL you provide. This is the real-time trigger for your application.

2. The "Mouth": Sending Replies with the REST API

Once your AI has generated a response, you need a way to send it back to the user on WhatsApp. Our REST API provides a simple, powerful endpoint to send text, media, and template messages programmatically. This is how your bot talks.

The 5 Steps to Building Your Bot
  1. Set Up Your Server Endpoint
    Create a simple web application using a framework like Node.js (with Express) or Python (with Flask). This application needs a public URL that can receive `POST` requests. This URL will become your webhook receiver.
  2. Configure Your WA-MExpress Webhook
    Log in to your WA-MExpress dashboard, navigate to your device settings, and paste your application's public URL into the webhook field. Now, all incoming messages will be sent directly to your server.
  3. Process the Incoming Message
    In your application's code, parse the JSON data that WA-MExpress sends. Extract the `sender` phone number and the `conversation` text. This is the information you'll pass to your AI.
  4. Generate an AI Response
    Send the user's message text to your preferred AI model's API (e.g., OpenAI, Anthropic, or a self-hosted model). The AI will process the text and return a generated response.
  5. Send the Reply via the WA-MExpress API
    Take the response from your AI and use it as the `message` parameter in a call to the WA-MExpress `create-message` API endpoint. Use the `sender` number from the webhook as the `to` parameter. This completes the loop and sends the reply to the user.
Example Code (Python + Flask)

Here’s a simplified Python example showing how these pieces fit together.

from flask import Flask, request, jsonify import requests import openai app = Flask(__name__) # Your WA-MExpress API Credentials MExpress_APP_KEY = "YOUR_APP_KEY" MExpress_AUTH_KEY = "YOUR_AUTH_KEY" MExpress_API_URL = "https://wa.mexpress.pk/api/create-message" # Your OpenAI API Key openai.api_key = "YOUR_OPENAI_API_KEY" def get_ai_response(user_message): # This is where you call the ChatGPT API response = openai.Completion.create(engine="text-davinci-003", prompt=user_message, max_tokens=150) return response.choices[0].text.strip() def send_whatsapp_reply(recipient, message): payload = { 'appkey': MExpress_APP_KEY, 'authkey': MExpress_AUTH_KEY, 'to': recipient, 'message': message } requests.post(MExpress_API_URL, data=payload) @app.route('/whatsapp-webhook', methods=['POST']) def whatsapp_webhook(): data = request.json sender_number = data.get('sender') user_message = data.get('payload', {}).get('conversation') if sender_number and user_message: # 1. Get AI response ai_reply = get_ai_response(user_message) # 2. Send reply via WA-MExpress API send_whatsapp_reply(sender_number, ai_reply) return jsonify({"status": "success"}), 200 if __name__ == '__main__': app.run(debug=True)

Ready to Build Your Custom AI Agent?

Get your API keys and start building powerful, custom WhatsApp automations today.

Get Started for Free

When building custom integrations, you are responsible for securing your application and API keys. Always ensure you have proper user consent and comply with all WhatsApp Commerce and Business policies.