Step 1: Install pywhatkit
To install the last version of pywhatkit, open up a terminal and run the following command.
pip install pywhatkit |
The installation might take seconds or even a minute so be patient as it involves a lot of dependencies. Once the library is installed, you can check out all the dependencies this library installed with the command pip list.
Step 2: Send a message to a contact on WhatsApp
Before we start sending messages, you have to log in to your WhatsApp account through WhatsApp Web. If you’re not logged in, your phone isn’t on the same WiFi as your computer, or the phone is far away from your computer, this method won’t work.
To send a message to a WhatsApp contact with Python and pywhatkit, we use the .sendwhatmsg method as shown in the code below (insert your contact number instead).
Code (Copy and Paste it in your favorite IDE) |
import pywhatkit |
After running the code above you’ll get a message like this:
In 10 seconds, web.WhatsApp.com will open, and after 15 seconds, a message will be delivered by Whatsapp.(Default time for sending is 15 seconds you can also change it.)
That’s it! After some seconds your browser will open and the message will be sent to your contact.
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).
Great! Now we can also customize other actions in the browser.
Say we want to close the tab 2 seconds after sending the message. We do so, running the code below.
import pywhatkit |
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.
Step 3: Send a message to a group on WhatsApp
We can also send messages to a specific group on WhatsApp, but first, we have to get the group id.
To find the id, follow these steps:
Open any group you want and click on the “Group Info” section
Scroll all the way down and look for the “Invite via link” option. Tap on that option
There you’ll see a link. Copy the link. The suffix part of the link is the id of the group.
Once you have the id of the group that you’ll use for this test, we have to use the .sendwhatmsg_to_group method. This method is similar to the one we used in step 2, but now we insert a group id.
import pywhatkit |
That’s it! Note that there are other methods pywhatkit has. You can also send messages instantly using .sendwhatmsg_instantly and .sendwhatmsg_to_group_instantly but they didn’t work for me.
Just in case, below are the parameters we used in this tutorial for .sendwhatmsg and .sendwhatmsg_to_group.
phone_no (str): The receiver’s mobile number group_id (str): The id of the group message (str): Message to be sent time_hour (int, 0–24), time_min (int): Hour and minutes of the scheduled time wait_time (int = 15): The time it will wait until the message is delivered (set to 15 seconds by default) tab_close (bool = False): If you set this to True the tab will close after X seconds once the message is delivered close_time (int = 3): Represents the number of seconds the tab will close after the message is delivered |
Subscribe to our Blogs and News
"Believe in yourself and all that you are. Know that there is something inside you that is greater than any obstacle."
WhatsApp message & Python