How to Create a WhatsApp Bot with Python in Minutes

Published on: May 11, 2026
Reading time: 9 minutes

Have you ever thought about how useful it would be to have a virtual assistant responding to your customers or contacts around the clock? Learning how to create a WhatsApp bot with Python is one of the most sought-after skills for anyone who wants to enter the world of Python automation. In this complete guide, you will discover that you do not need to be an expert programmer to get this project running. Using accessible tools and straightforward logic, your first messaging bot can be working in just a few minutes.

WhatsApp is one of the most widely used messaging platforms in the world, and integrating it with Python opens up a wide range of opportunities, from sending automatic notifications to building complex customer support systems. This tutorial focuses on the PyWhatKit library, which is ideal for beginners because it does not require complex official API configurations to get started. Get your code editor ready and let us turn a few lines of text into a functional automated assistant.

Why Use Python to Build Your WhatsApp Bot?

Python stands out as the preferred language for automation tasks because of its clean syntax and its vast ecosystem of Python libraries. Choosing Python for bot development means faster development cycles and easier code maintenance over time. The community is enormous, which means that if you run into a problem, someone has almost certainly documented the solution already.

Versatility is another major advantage. With the same script that sends messages, you can integrate other capabilities, such as generating a QR code with Python to make it easier for new users to connect with your bot, or reading data from a spreadsheet to personalize messages for each recipient. The possibilities are limited only by your imagination and the time you invest.

Setting Up Your Environment

Before writing the bot logic, you need to make sure your computer is properly configured. Start by confirming that Python is installed and up to date. With that in place, choose a code editor. VS Code is one of the best IDEs for Python available today, offering real-time error highlighting and a large library of useful extensions.

Once your editor is open, you need to install the library that connects Python to WhatsApp Web. Open your terminal and run the following command:

Bash
pip install pywhatkit

PyWhatKit works by controlling WhatsApp Web through your default browser. Before running any script, make sure you are already logged in to WhatsApp Web in that browser. The library automates the browser interaction, so you should avoid touching your mouse or keyboard while the script is executing to prevent the bot from clicking in the wrong place.

How the Bot Logic Works

A basic WhatsApp bot follows a simple sequence of events: open the browser, find the target contact, type the message, and press send. Python automates all of those clicks and keystrokes. This is a direct application of programming logic to save time on repetitive daily tasks. The PyWhatKit library wraps all of that browser automation into simple function calls, so you can focus on the message content and timing rather than the low-level mechanics.

Step 1: Importing the Library and Setting Variables

The first step is importing the module and defining where the message will go and what it will say. The phone number must include the international country code, for example +1 for the United States or +44 for the United Kingdom:

Python
import pywhatkit as kit

destination_number = "+15551234567"
message = "Hello! This is a bot created with Python. How can I help you?"

Step 2: Scheduling Message Delivery

The main function you will use is sendwhatmsg. It requires the phone number, the message text, and the time (hour and minute in 24-hour format) when you want the message to be sent. This is perfect for creating automatic reminders or scheduled notifications without needing to be at your computer at the exact moment:

Python
# Schedule the message for 3:30 PM
kit.sendwhatmsg(destination_number, message, 15, 30)

When this runs, Python will open WhatsApp Web in your browser, wait for it to load, navigate to the contact, and send the message at the scheduled time.

Step 3: Sending Immediately and Closing the Tab

If you want to send the message right away instead of scheduling it for a specific time, PyWhatKit offers an instant send function with an additional option to close the browser tab after sending. This is especially useful when running a batch of messages, since it prevents dozens of tabs from accumulating in your browser:

Python
kit.sendwhatmsg_instantly(
    phone_no=destination_number,
    message=message,
    wait_time=15,
    tab_close=True
)

The wait_time parameter sets how many seconds the script waits for WhatsApp Web to fully load before attempting to interact with it. If your internet connection is slow, increase this value to avoid synchronization errors where the script tries to type before the page is ready.

Handling Common Automation Errors

When running your first bot, you may encounter a few obstacles. A common issue is a permission error or the script failing to locate the browser window. On Windows or Linux, knowing how to run terminal commands with Python can help you diagnose whether the libraries were installed in the correct environment and whether your browser is the default one Python expects to open.

Another common issue involves character encoding. If your message contains special characters, accented letters, or emojis and Python throws an encoding error, you need to ensure your script file is saved in UTF-8 format. Always test with simple ASCII messages before moving to complex text with special formatting or emoji sequences.

Security and Responsible Use

Building bots must be done responsibly and in line with the platform’s terms of service. Sending large volumes of unsolicited messages to people who have not opted in to receive them can result in your phone number being permanently banned by WhatsApp. Use your bot for productive purposes such as order notifications, customer support for registered users, or personal task automation.

To keep your script secure, never expose sensitive credentials or tokens directly in the source code. The best practice is to read environment variables in Python to store sensitive information separately from the main script. If your bot needs to handle security tokens or generate random session identifiers, consider using the Python secrets module for secure random generation instead of the standard random library.

Expanding Your Bot’s Capabilities

Once you have mastered text messaging, you can expand the bot to send files and images. PyWhatKit supports sending images with captions automatically. This makes it possible to automate the delivery of daily PDF reports, metric screenshots, or promotional images to your work group without any manual effort. You simply provide the file path on your local machine.

If you want a bot that truly has a conversation, meaning one that understands questions and responds intelligently, you will need to combine this WhatsApp automation with a natural language processing layer. The ChatGPT Python integration guide walks through exactly how to connect the OpenAI API to your Python script. Combining that with PyWhatKit gives you a bot that can answer complex user questions in real time through WhatsApp.

Complete Project Code

Here is the full, unified script with proper error handling, a clean function structure, and comments explaining each step. Replace the phone number with your own test number and make sure WhatsApp Web is already logged in before running:

Python
import pywhatkit as kit
import time

def run_whatsapp_bot():
    try:
        # Bot settings
        phone = "+15551234567"  # Use international format
        text = "Hello! This is my first WhatsApp bot running with pure Python."

        print("Starting the send process...")

        # Send the message instantly
        # wait_time: seconds to wait for WhatsApp Web to load
        # tab_close: closes the browser tab after sending
        kit.sendwhatmsg_instantly(
            phone_no=phone,
            message=text,
            wait_time=20,
            tab_close=True
        )

        print("Message sent successfully!")

    except Exception as e:
        print(f"An unexpected error occurred: {e}")

if __name__ == "__main__":
    run_whatsapp_bot()

Taking Your Automation Further

Mastering WhatsApp automation is just one step in a larger automation journey. If you enjoyed controlling browser behavior through code, you will likely find Selenium in Python very interesting. It enables far more powerful browser automation across any website, simulating clicks, form filling, and complex navigation sequences.

Another natural next step is building a graphical interface around your bot. Instead of running it from the terminal, you could create a graphical interface with Tkinter so that anyone in your team could type a message and click a button to trigger the bot without ever seeing a single line of code.

For developers who want to build production-grade WhatsApp integrations at scale, the WhatsApp Cloud API documentation by Meta provides the official API approach, which supports webhook-based message receiving and is the right tool for business applications that need to handle large message volumes reliably.

Frequently Asked Questions

Does the bot work with the computer turned off?

No. Because PyWhatKit controls your actual browser session on your machine, the script must be running and the computer must be on and connected to the internet for any actions to occur.

Can I send messages to groups?

Yes. The library includes a sendwhatmsg_to_group function that uses the unique group ID found in the group invite link to send messages directly to a group chat.

Can I send images and PDFs?

Yes. The sendwhats_image function lets you provide the local file path of an image along with an optional caption. For PDF files, you can attach them through the standard file attachment flow that PyWhatKit automates.

Can my number get banned for using this bot?

Yes, if you send unsolicited messages in bulk or contact large numbers of people who have not saved your number, there is a real risk of being flagged and banned. Always use the bot for legitimate, consensual communication.

Is PyWhatKit free to use?

Yes, PyWhatKit is an open-source library available at no cost. You only need Python installed and an active internet connection to use it.

Does the bot work with WhatsApp Business?

Yes, it works with both personal and Business accounts as long as the account is connected to WhatsApp Web in your browser at the time the script runs.

How do I make the bot reply to incoming messages?

PyWhatKit is not designed for reading and responding to incoming messages. For that functionality, you would need browser automation with Selenium or the official WhatsApp Cloud API, which supports webhooks that notify your server when a new message arrives.

Can I run this bot on a mobile device?

This script is designed for desktop operating systems. Running it on a mobile device through environments like Termux is technically possible but the browser automation behavior works very differently and is much harder to configure reliably.

Share:

Facebook
WhatsApp
Twitter
LinkedIn

Article content

    Related articles

    Automation and Scripts
    Foto do Leandro Hirt

    How to Generate an Android .apk Executable with Python

    Learn how to generate an Android APK with Python using Kivy and Buildozer. This complete guide covers environment setup, building

    Ler mais

    Tempo de leitura: 9 minutos
    11/05/2026
    Automation and Scripts
    Foto do Leandro Hirt

    How to Organize Your PC Files Automatically with Python in 5 Minutes

    Learn how to automatically organize your PC files with Python in minutes. This guide covers mapping extensions, creating folders, moving

    Ler mais

    Tempo de leitura: 8 minutos
    11/05/2026
    Automation and Scripts
    Foto do Leandro Hirt

    How to Integrate ChatGPT into Your Python Code: Practical Guide

    Learn how to integrate ChatGPT into your Python code using the OpenAI API. This practical guide covers setup, reusable functions,

    Ler mais

    Tempo de leitura: 10 minutos
    09/05/2026
    Ícone da linguagem de programação Python com um código ao lado
    Automation and Scripts
    Foto do Leandro Hirt

    10 Essential Python Commands for Beginners

    If you are starting your programming journey, learning a few essential Python commands can make a huge difference. Python is

    Ler mais

    Tempo de leitura: 6 minutos
    08/05/2026
    Ilustração de automação usando Python
    Automation and Scripts
    Foto do Leandro Hirt

    20 Ready-to-Use Python Scripts for Automation

    If you spend time doing the same tasks on your computer every day, Python scripts for automation can save you

    Ler mais

    Tempo de leitura: 13 minutos
    06/05/2026
    Automation and Scripts
    Foto do Leandro Hirt

    How to compare two lists in Python and find the differences

    Comparing data is one of the most common and essential tasks in a programmer’s day-to-day work. Whether you are synchronizing

    Ler mais

    Tempo de leitura: 10 minutos
    06/05/2026