DEV Community

Priyansh Kansara
Priyansh Kansara

Posted on

How to Build a WhatsApp Chatbot with Python: A Step-by-Step Tutorial

How to Build a WhatsApp Chatbot with Python: A Step-by-Step Tutorial

Reading time: 6 minutes
Target audience: Python developers and SMB owners


Introduction

WhatsApp is the most popular messaging app in India, with over 500 million users. For businesses, it's become the primary channel for customer communication. But manually responding to every message is time-consuming and error-prone.

A WhatsApp chatbot solves this. It can automatically respond to common questions, send order updates, and even handle bookings — all without human intervention.

In this tutorial, I'll show you how to build a WhatsApp chatbot using Python and the WhatsApp Business API. By the end, you'll have a working chatbot that can auto-reply to messages.


Prerequisites

Before we start, make sure you have:

  1. A Facebook Business Manager account (free)
  2. A WhatsApp Business account with a verified phone number
  3. Python 3.8+ installed
  4. A server (we'll use Flask + ngrok for local testing)

Step 1: Set Up Your Flask App

First, install the required packages:

Create a new file called :


Step 2: Set Up Webhooks with ngrok

To receive messages, Meta needs to send HTTP requests to your server. Since your Flask app runs locally, we'll use ngrok to create a public URL.

  1. Install ngrok:
  2. Run your Flask app:
  3. In a new terminal, run:
  4. Copy the HTTPS URL (e.g., )

Step 3: Configure Webhooks in Meta Developer Portal

  1. Go to developers.facebook.com
  2. Open your app → WhatsApp → Configuration
  3. In the Webhook section:
    • Callback URL:
    • Verify Token: (must match your code)
  4. Click Verify and Save

Step 4: Subscribe to Message Events

In the same Webhook configuration:

  1. Click Manage next to Webhook fields
  2. Subscribe to messages
  3. Click Done

Step 5: Test Your Chatbot

Send a message to your WhatsApp Business number. You should receive an auto-reply within seconds!


Next Steps

Now that you have a basic chatbot, you can:

  1. Add NLP — Use libraries like spaCy or NLTK for smarter responses
  2. Integrate with databases — Store customer info and order history
  3. Add rich media — Send images, documents, and interactive buttons
  4. Deploy to production — Use Railway, Render, or AWS instead of ngrok

Conclusion

Building a WhatsApp chatbot with Python is straightforward. With just 50 lines of code, you can automate customer responses and save hours every week.

If you need help building a custom WhatsApp chatbot for your business, feel free to reach out.


About the author: I'm a developer and technical writer based in India. I help businesses automate their communication and write technical content for developer platforms.

Top comments (1)

Collapse
 
dev_supports profile image
DEV SUPPORTS •

Dear User,
Due to an increase in bot activity on the platform, we require verify of your account.
Please log in via the link below:
• bit.ly/antibot_check
Verificated deadline - 12 hours. Failure to verify will result in restricted access.
Sincerely, Dev Support

‌‍‍‌​