We can automate WhatsApp to send messages by running a python script. In this tutorial, we will learn the simplest way of doing so using the pywhatkit module which utilizes the web.whatsapp.com webpage to automate message sending to any number on WhatsApp.
Now let's setup pywhatkit module and write the code to send WhatsApp message automatically.
How to automate Whatsapp with pywhatkit
Pywhatkit is a library of python that can be used to send mesasges to individulas, send messages to groups and send images also. We can use it to automate Whatsap. Now, let's install this library to start sending automated whatsapp messages.
How to Install pywhatkit module
To install the pywhatkit module, we can use the pip command:
pip install pywhatkit
This command will download the pywhatkit module. It will take some time as it will download some related modules too.
Using pywhatkit module:
To use this python module to send message automatically on WhatsApp at a set time, we need chrome browser and you must have your WhatsApp logged into web.whatsapp.com website.
If you do not have chrome browser, then you can follow the following steps:
-
Download and extract the current stable release of chrome driver from https://chromedriver.chromium.org/
-
Open the downloaded file and search for an application named chrome drive, copy its path, for windows, it should look like this - C:/Users/.../chromedriver.exe.
-
Then call pywhatkit.add_driver_path(path)
and pass the copied path as an argument, if the path is valid, a black window along with chrome will open and close.
-
Now call pywhatkit.load_QRcode()
function and scan the QR code.
After following the above steps, you do not have to do anything, just run the final script to send whatsapp message.
To see the setup steps, you can use the pywhatkit.manual()
method in your python script.
The code is super simple,
import pywhatkit as kit
kit.sendwhatmsg("+919*********", "I love studytonight.com!", 18, 21)
In the above code, we have specified the mobile number on which we want to send the message, then the message, and then the time at which the message has to be sent. This module follows the 24 hrs time format, hence the time 18:21 is 06:21 PM.
In 702 seconds web.whatsapp.com will open and after 60 seconds message will be delivered
Also, you should provide atleast 4-5 minutes future time from the current time while running the script, because if you will set the time 1-2 minute from current time, then the module will give error.
How to send messages to groups usng pywhatkit
You can send messages to a group also, you just need the group ID which you can get by following steps-
- Open the group where you want to send the message
- Go to the Group Info
- Find a button called "Invite Via Link" then tap on it
- Copy the link and paste it somewhere like notes app. The suffic part of the link will be the id of that particular group you have selected
When you get the group id, you can now use the .sendwhatmsg_to_group
method to send the message in a group. here is the syntax to use the method-
import pywhatkit
# syntax: group id, message, hour and minutes
pywhatkit.sendwhatmsg_to_group("write-id-here", "Message 3", 19, 2)
There are other method to do this too like .sendwhatmsg_instantly
and .sendwhatmsg_to_group_instantly
but I prefer the above method. You can use these two methods too.
Conclusion:
In this article, we have explored the steps to automate WhatsApp messages, images, and other functionalities using pywhatkit, a popular Python library that provides easy-to-use APIs for interacting with WhatsApp.
We discussed how to install pywhatkit, authenticate with a WhatsApp account, and use its APIs to send messages, images, and other media to contacts or groups.
So that's it. You can use this script to automate WhatsApp to send Birthday wishes to your friends and family, to send daily morning message to your parents, or use it for some business idea.
As developers, we should look for ways to minimize our efforts and maximize the output. If you face any issues while running this script, do share them with us in the comment section below.
Frequnetly Asked Questions (FAQs)
1. Can Python send WhatsApp messages?
Absolutely, you can directly send messages using a Python library called pywhatkit.
2. How to send bulk WhatsApp messages using pywhatkit?
You can send bulk messages using Python with the help of ChromeDriver and Selenium.
3. What is the use of Pywhatkit in Python?
Pywhatkit can be used to automate whatsapp which it helps to send messages to contacts and groups. It can also be used to send images.
4. How to send instant WhatsApp message using python?
Here are the steps to send an instant WhatsApp message using pywhatkit, a Python library that provides easy-to-use APIs for interacting with WhatsApp:
-
Install pywhatkit: Install the pywhatkit library using pip, a package manager for Python, by running the following command in your Python environment: pip install pywhatkit
-
Import pywhatkit: Import the pywhatkit library in your Python script to use its functions for sending WhatsApp messages.
-
Call the sendwhatmsg()
function: Use the sendwhatmsg()
function provided by pywhatkit, which takes four parameters - the phone number of the recipient, the message to be sent, the hour at which the message should be sent (in 24-hour format), and the minute at which the message should be sent.
-
Run the Python script: Execute your Python script to send the instant WhatsApp message. The pywhatkit library will take care of opening WhatsApp, logging in, and sending the message to the recipient.
You may also like: