This post will introduce you to the python pywhatkit library and how to use it to send WhatsApp messages automatically using a few lines of Python code.
Also read: Python Twilio – Automate WhatsApp Business API Messages
Exploring Pywhatkit: A Python Library for WhatsApp Automation
Pywhatkit is a Python library that enables automation of WhatsApp messages. It’s simple to use and offers features such as sending messages or images to individuals or groups, playing YouTube videos, and even converting text to handwriting. To use Pywhatkit, you need to install it using the pip command and set up your WhatsApp Web account in your default browser
Pywhatkit utilizes WhatsApp Web to send messages automatically. This means, you need your computer to stay running and WhatsApp web to be logged in for this to work.
Pywhatkit: Key Features
- Sending messages to contact/people on Whatsapp automatically
- Automatically send message to group
- Playing a YouTube Video or shorts
- Also used to convert text(string) to handwriting
- Sending Mails with HTML Code
Step-by-Step Guide: Sending WhatsApp Messages with Pywhatkit
Let’s now get into the steps to use the pywhatkit and send our first WhatsApp message using Python.
1. Installing the library
Since Pywhatkit is not pre-installed in Python3, you can install it using the pip command:
pip install pywhatkit
2. Sending WhatsApp Message
Using pywhatkit, Whatsapp messages can be automatically sent to any number on Whatsapp.
Note: You have to be logged in to Whatsapp in your browser, which means you’ll need to set up your WhatsApp Web account in your default browser.
The automatic WhatsApp message is sent using the sendmsg() method of the Pywhatkit library. It has several features which are listed down in the form of examples that show how to send messages or images either to an individual or in a group.
Syntax: pywhatkit.sendmsg(“receiver’s mobile number”, “message to be sent”, hours, minutes)
Parameters of the method –
- Receiver’s mobile number: Should be in string format and country code should be necessarily included, written before the mobile number.
- Message to be sent: In string format.
- Hours: the method follows a 24 hrs time format.
- Minutes: It should be between 00-59.
3. Complete code to send WhatsApp message automatically using Python
import pywhatkit as pwk
# using Exception Handling to avoid unexpected errors
try:
# sending message in Whatsapp in India so using Indian dial code (+91)
pwk.sendwhatmsg("+91XXXXXXXXXX", "Hi, how are you?", 20, 34)
print("Message Sent!") #Prints success message in console
# error message
except:
print("Error in sending the message")
This program will send a message ‘Hi, how are you?‘ to the specified receiver’s phone number at the specified time (20:34 or 8:34 PM).
Note: The method by default opens the browser 15 seconds before the specified time to make up for the time of loading the WhatsApp Web site on your default browser.
Some more features of the library to send messages in WhatsApp
Closing the tab (WhatsApp Web tab) after sending the message.
pwk.sendwhatmsg("+91XXXXXX5980", "Hi", 18, 15, True, 5)
Here we are passing 5 seconds as the time to close the tab, also True represents the bool value of whether the tab needs to be closed or not, if it’s true it will close, or else if it’s false, it will not close
Sending an image to a Group along with the Caption as Hi
Here Media/image.png refers to the image to be sent
pwk.sendwhats_image("Group_Name", "Media/image.png", "Hi")
pwk.sendwhats_image("Name", "Media/images.png")
Sending a message to a Group:
pwk.sendwhatmsg_to_group("Group_Name", "Hey Guys! How's everybody?", 11, 0)
# it is similar to sending a message to a single person but here we are sending the message in a group
Instantly sending a message in Group
We use this to send messages instantly in a group as if we write 0 hours, 0 minutes then it will send the message at 12:00 AM
pwk.sendwhatmsg_to_group_instantly("Group_Name", "Hey Guys Again!")
Also read: Using Selenium in Python to Automate Facebook Login
Troubleshooting Common Errors in Pywhatkit
Some of the common errors which you may encounter and their solutions:
“SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers”
Solution: Start a minute’s argument with any number other than 0.
“raise Warning(“INTERNET IS SLOW, extraction of information might take longer time”)”
“Warning: INTERNET IS SLOW, extraction of information might take longer time”
Solution: Make sure you have a strong internet connection
Conclusion
That wraps up our exploration of Pywhatkit! Now that you’re equipped with the knowledge to automate WhatsApp messages using Python, how will you use this powerful tool in your next project?