Skip to main content

📝 Latest Blog Post

Python is for "Lazy" People: How to Automate WhatsApp Messages with 2 Lines of Code

Python is for "Lazy" People: How to Automate WhatsApp Messages

Python is for "Lazy" People: How to Automate WhatsApp Messages

Bill Gates once said, "I choose a lazy person to do a hard job. Because a lazy person will find an easy way to do it." Today, that easy way is Python.

There is a rule in programming called the DRY Principle: Don't Repeat Yourself. Usually, this applies to writing code, but it should apply to your life too.

If you find yourself waking up every morning and typing "Good morning" to your significant other, or sending "Meeting in 10 minutes" to your team, or reminding clients "Your invoice is due," you are wasting your life. You are acting like a robot. And if you act like a robot, you should be replaced by one.

Today, we are going to build a simple Python script that takes control of your browser and sends WhatsApp messages for you. It allows you to schedule messages for next hour, next week, or next year.

The Goal: Write a script that automatically opens WhatsApp Web, finds a contact, types a message, and hits send at a specific time—all without you touching the keyboard.

The Secret Weapon: PyWhatKit

You could write a complex script using Selenium to hack the browser, but why reinvent the wheel? There is an amazing open-source library called pywhatkit. It creates a bridge between your Python script and WhatsApp Web.

Prerequisites

Before we start, you need three things:

  1. Python installed on your computer.
  2. A code editor (VS Code, PyCharm, or even Notepad).
  3. An active WhatsApp account logged into WhatsApp Web on your default browser.

Step 1: Installation

First, open your terminal or command prompt. We need to install the library. It’s a simple pip command:

pip install pywhatkit

Note: This library relies on other automation tools, so it might take a minute to download dependencies.

Step 2: The One-Liner Code

I promised you this was for lazy people. The entire script can be written in two lines. Create a new file called whatsapp_bot.py and type this:

import pywhatkit # Syntax: phone_number, message, hour (24h), minute pywhatkit.sendwhatmsg("+1234567890", "Hello! This is an automated message.", 14, 30)

That is it. Seriously.

Let's break down the arguments in the sendwhatmsg function:

  • Phone Number: Must include the country code (e.g., +1 for USA, +91 for India). It must be a string.
  • Message: The text you want to send.
  • Hour: The hour in 24-hour format (e.g., 14 is 2:00 PM).
  • Minute: The specific minute you want the message to send.

Step 3: How It Works (The Ghost in the Machine)

When you run this script, Python will pause. It calculates how much time is left until the scheduled time.

Approximately 60 to 90 seconds before the scheduled time, the script will trigger. It will automatically open your default web browser (Chrome, Edge, etc.) and navigate to web.whatsapp.com.

Important: You must already be logged into WhatsApp Web on that browser. If you aren't, the script will get stuck at the QR code login screen.

Once the page loads, the script waits for the chat to open. Then, like a ghost invisible to the naked eye, it will programmatically type the message into the text box and simulate a generic "Enter" key press to send it.

Advanced Automation: Sending to a Group

What if you want to annoy your entire family or work team at once? You can send messages to groups using the Group ID.

You can find the Group ID in the URL of the group invite link. Once you have it, the code changes slightly:

# Send to a Group group_id = "AbC123XyZ" # The end part of the invite link pywhatkit.sendwhatmsg_to_group(group_id, "Meeting starts in 5 minutes!", 9, 0)

Common Errors & Troubleshooting

Automation isn't always perfect. Here are common issues you might face:

1. Screen Resolution Issues

PyWhatKit uses simulated mouse clicks. If your browser opens but the message doesn't send, your internet might be slow, or the "Send" button might be in a different place due to screen resolution. You can increase the wait time:

# Add a wait_time argument (seconds) pywhatkit.sendwhatmsg("+1234567890", "Hi", 14, 30, wait_time=20)

2. Browser Not Opening

Ensure you are not running the script as Administrator/Root, as this sometimes blocks the browser from launching due to permission conflicts.

Conclusion

Python is powerful not because it can calculate complex math, but because it can interact with the real world. By mastering libraries like pywhatkit, you stop being a user of technology and start being a master of it.

Use this power wisely. Don't spam people. But definitely use it to wish your best friend a Happy Birthday at exactly 12:00 AM while you are sound asleep.

Download the Source Code

Comments

🔗 Related Blog Post

🌟 Popular Blog Post