DEV Community

Joey Umanito
Joey Umanito

Posted on

How to Build a WhatsApp Bot with Python in 5 Minutes

WhatsApp bots are one of the hottest automation tools for businesses right now. Whether you want to automate customer support, send notifications, or build an AI assistant, Python makes it surprisingly simple.

In this tutorial I'll show you how to build a basic WhatsApp bot using Python and the Twilio API (free tier available).

What You'll Need

  • Python 3.8+
  • A free Twilio account
  • Flask (Python web framework)
  • ngrok (for local testing)

Step 1: Install Dependencies

Open your terminal and run:

pip install flask twilio


## Step 2: Set Up Twilio WhatsApp Sandbox

1. Go to twilio.com and create a free account
2. Navigate to Messaging > Try it out > Send a WhatsApp message
3. Follow sandbox setup - send a WhatsApp message to activate
4. Note your Account SID and Auth Token

## Step 3: Create Your Flask App

Create a file called whatsapp_bot.py:

Enter fullscreen mode Exit fullscreen mode

from flask import Flask, requestfrom twilio.twiml.messaging_response import MessagingResponseapp = Flask(name)@app.route('/bot', methods=['POST'])def bot(): incoming_msg = request.values.get('Body', '').lower() resp = MessagingResponse() msg = resp.message() if 'hello' in incoming_msg: msg.body('Hello! I am your WhatsApp bot. How can I help?') elif 'price' in incoming_msg: msg.body('Our pricing starts at $20. Visit our website for more info!') else: msg.body('I did not understand that. Try saying hello!') return str(resp)if name == 'main': app.run(debug=True)

Enter fullscreen mode Exit fullscreen mode

Step 4: Deploy with ngrok

Run your Flask app and expose it with ngrok for free:

ngrok http 5000

Copy the HTTPS URL from ngrok and paste it into your Twilio WhatsApp webhook settings.

Conclusion

You now have a working WhatsApp bot! This is just the beginning. You can extend it to handle orders, FAQs, customer support, and more.

Need a custom WhatsApp or Telegram bot for your business? Check out my Fiverr gig where I build professional bots starting at $20: https://www.fiverr.com/joeyumanito

Drop your questions in the comments below!

Top comments (0)