Save Time with Python: Automating WhatsApp Messages in Just 3 Steps

Save Time with Python: Automating WhatsApp Messages in Just 3 Steps

The Dynamic Duo of Time-Saving Awesomeness!

Are you tired of sending the same old boring WhatsApp messages? Do you want to impress your friends with a hand-written note without actually writing it? Well, my dear readers, have no fear because pywhatkit is here! I use it to send monthly reminders to my NETFLIX group. 🚀


Pywhatkit: Your Personal Python Assistant! 🤖

Pywhatkit is a Python library that lets you do all sorts of cool things like converting text to hand-written text, sending WhatsApp messages, playing YouTube videos, and more. It's like having your own personal assistant at your fingertips, except it's made of code and doesn't require a salary.

But wait, there's more! Pywhatkit is not just for fun and games. It can also be used for practical purposes like generating QR codes, searching for information on Google, and finding out the weather forecast of any city.

So whether you're looking to spice up your social life or make your work easier, pywhatkit has got you covered. Give it a try and unleash the power of Python in your daily life!

Disclaimer: This guide is for educational purposes only. You might use this library to schedule birthday/anniversary messages while learning Python, but I don’t recommend you to use it in case you need to send a message that needs to remain private.


Sending messages to WhatsApp with pywhatkit 💬

If you're looking to automate sending messages on WhatsApp, pywhatkit is a great Python package to use. It's easy to install and can send messages to both individual contacts and groups.

Step 1: Install pywhatkit

Before we can start sending messages, we need to install pywhatkit. To do so, simply open your command prompt or terminal and enter the following command:

pip install pywhatkit

Once the installation is complete, check out the dependencies it installed with the command pip list and we're ready to move on to the next step.

Step 2: Send a message to a contact on WhatsApp 🤳🏻

Before we can start sending messages using Python and pywhatkit, there's an important prerequisite we need to cover. To use pywhatkit to send messages on WhatsApp, we need to be logged in to our WhatsApp account through WhatsApp Web on the computer we'll be running our Python code on.

Similarly, if your phone isn't connected to the same Wi-Fi network as your computer or your phone is too far away from your computer, the code may not work properly.

Assuming you're logged in and ready to go, let's take a look at how we can send a message to a WhatsApp contact using Python and pywhatkit.

To send a message to a contact, we use the .sendwhatmsg method provided by pywhatkit.

Here's an example of how to do so:

  • Enter the phone number of the contact you want to send the message to, along with the message itself

    
      # importing the module
      import pywhatkit
    
      # using Exception Handling to avoid
      # unprecedented errors
      try:
          # sending message to receiver
          # using pywhatkit
          pywhatkit.sendwhatmsg("+91XXXXXXXXXX", "Hello, this is an automated message sent with pywhatkit!")
          print("Successfully Sent!")
    
      except:
        # handling exception
        # and printing error message
        print("An Unexpected Error!")
    

In the example above, replace +91XXXXXXXXXX with the phone number of the contact you want to message.

NOTE: The phone number should include the country code (e.g. "+91" for India).

When you run the script, you should see a new window open with WhatsApp Web.

In 10 seconds, web.WhatsApp.com will open, and after 15 seconds, a message will be delivered by Whatsapp.

Important Note: When your browser opens, a message to grant access will pop up. You have to accept this message (if you don’t give permission the library won’t be able to control your browser). ✅

You can also do this manually On macOS, you have to go to “System Preferences”, click on “Security & Privacy”, select “Accesabitly”, and check the “Terminal” box.

Great! Now we can also customize other actions in the browser.

  • Send a WhatsApp Message to a Contact at 1:30 PM

      pywhatkit.sendwhatmsg("+910123456789", "Hi", 13, 30)
    
  • Same as above but Closes the Tab in 2 Seconds after Sending the Message

    
      pywhatkit.sendwhatmsg("+910123456789", "Hi", 13, 30, 15, True, 2)
    

    Here the “True” indicates that we want to close the tab after “X” seconds once the message is delivered. The last argument “2” represents this “X” number of seconds.

  • Send an Image to a Contact with the no Caption

      pywhatkit.sendwhats_image("+910123456789", "Images/Hello.png")
    

Step 3: Send a message to a group on WhatsApp 🚀

Sending a message to a group on WhatsApp is just as easy as sending a message to an individual contact. Here's an example of how to do so:

import pywhatkit

# Enter the name of the group you want to send the message to, along with the message itself
pywhatkit.sendwhatmsg_to_group("Group Name", "Hello, this is an automated message sent with pywhatkit!")

In the example above, replace "Group Name" with the name of the group you want to message.

NOTE: The group name should be entered exactly as it appears in your WhatsApp contacts.

When you run the script, you should see a new window open with WhatsApp Web. The message will be sent automatically after a few seconds to the entire group.

Awesome! Let's check out some other examples:

  • Send an Image to a Group with the Caption as Hello

      pywhatkit.sendwhats_image("Group Name", "Images/Hello.png", "Hello")
    
  • Send a WhatsApp Message to a Group at 12:00 AM

      pywhatkit.sendwhatmsg_to_group("Group Name", "Hey All!", 0, 0)
    

Some Other Features ✨

  1. Play a YouTube video:

    
     # importing the module
     import pywhatkit
    
     # using Exception Handling
     # to avoid exceptions
     try:
       # it plays a random YouTube video based on the parameter
       pywhatkit.playonyt("URL/Topic Name")
       print("Playing...")
    
     except:
       # printing the error message
       print("Network Error Occurred")
    

    Parameters:

    • URL/Topic Name: Mention the topic name or URL of the YouTube video
  2. Perform Google Search:

    
     # importing the module
     import pywhatkit
    
     # use Try Except to
     # handle the Exceptions
     try:
       # it will perform the Google search
       pywhatkit.search("Keyword")
       print("Searching...")
    
     except:
       # Printing Error Message
       print("An unknown error occurred")
    

    Parameters:

    • Keyword: It opens your browser and performs a search operation.
  3. Get information on particular topic:

     # importing the module
     import pywhatkit
    
     # using Exception Handling for
     # handling unprecedented errors
     try:
     # it will perform google search
     pywhatkit.info("topic",lines=4)
     print("\nSuccessfully Searched")
    
     except:
    
     # printing error message
     print("An Unknown Error Occurred")
    

    Parameters:

    • Topic: Gives the output on the topic mentioned.

    • lines: It prints the searched topic in the specified number of lines.


Conclusion 💡

And that's it! With just a few lines of code, you can easily send personalized messages to a large group of people, or even automate your personal messages to save time and effort. Happy messaging! ✨