We can automate a Python script to send WhatsApp messages. In this article, we will learn the easiest ways using pywhatkit module that the website web.whatsapp.com uses to automate the sending of messages to any WhatsApp number.
Installing pywhatkit module:
pywhatkit is a python module for sending Whatsapp messages at a certain time. To install the pywhatkit module, Type the following command in your IDE/Compiler:
pip install pywhatkit
This command will download the pywhatkit module. It will cause some delay as it will download some related modules too.
Using pywhatkit module:
To use this python library to send messages automatically on WhatsApp at certain times, we need a chrome browser and you must have your WhatsApp logged into the web.whatsapp.com website.
If you do not have a chrome browser, then you can follow the following steps:
- Download and extract the current stable release of chrome driver from here(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 the function 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.
- Next call the function pywhatkit.load_QRcode() and scan the QR code.
Below is the implementation:
"Geeks For Geeks!"
- In the above script, we have specified the recipient phone number on which we want to send the message, then the message to be sent, and then the time at which the message has to be sent. This function follows the 24 hrs time format, hence the time 18:30 is 06:30 PM.
- Also, you should provide atleast 2-3 minutes future time from the current time while running the script, because if you will set the time 1-2 minutes from the current time, then the module will give an error.
- Before running the script make sure you are logged in to WhatsApp web in google chrome. To login into WhatsApp web go to this link and scan the QR Code using the mobile phone in which you are using WhatsApp.
Now just run the final script to send a WhatsApp message. After running the script, It will let you know that after how many seconds your WhatsApp going to open and what particular time your message would be sent. After that many seconds, this script will automatically open WhatsApp and it will send the message to the specified recipient.
In 51 seconds web.whatsapp.com will open and after 20 seconds message will be delivered
Some Error Messages You May Encounter during Execution:
1. While installing the Pywhatkit module, you may encounter this error message:
“The headers or library files could not be found for zlib, a required dependency when compiling Pillow from source.”
Solution: Upgrade pip and pillow. Run the codes below separately:
2. Specify the minutes’ argument by starting with 0. For instance 6:08 PM as 18, 08. You will get a syntax error:
“SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers.”
Solution: Start your minutes’ argument with a number other than 0.
3. If the recipient phone number you want to send a message to doesn’t have a country code. You will get this error:
“CountryCodeException(“Country code missing from phone no”)”
“pywhatkit.mainfunctions.CountryCodeException: Country code missing from phone no”
Solution: Include the country code for every phone number. For example, +234, +44, +1.
4. When you have slow internet, your message will not be sent. You will get this error message:
“raise Warning(“INTERNET IS SLOW, extraction of information might take longer time”)”
“Warning: INTERNET IS SLOW, extraction of information might take longer time”
Solution: Use a strong internet connection.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses
are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!