Send WhatsApp messages to contacts/groups using this Python script
Python is probably one of the coolest languages. And this fascinating about it is that you can automate day-to-day tasks.
One of such tasks we do every day is sending messages on WhatsApp.
Although this task is as simple as picking up the phone, typing, and tapping on the “send” button, but we sometimes forget to send a wishing messages on special occasions like birthdays, anniversaries, etc.
So you must be wondering can we schedule a WhatsApp message?
Definitely you can by using a library called pywhatkit. We can schedule messages to send to any of our contacts or to a group.
Here are the 3 steps you need to follow to send WhatsApp messages to contacts and groups using Python.
Disclaimer: This article is for educational purposes only. You might use this library to schedule birthday/anniversary messages while learning Python. 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 library
Pywhatkit is a Python library used for different purposes and one of those is sending WhatsApp messages.
This is an easy-to-use library that doesn’t need any additional setup. That said, it has a lot of dependencies, so I highly recommend you install it in a new virtual environment (learn how to set up a virtual environment here).
Step 1: Install pywhatkit
Open up terminal and run the following command.
pip install pywhatkit
You can check out all the dependencies this library installed with the command
Step 2: Send a message to someone on WhatsApp
Log in to your WhatsApp account through WhatsApp Web.
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).
import pywhatkit # syntax: phone number with country code, message, hour and minutes pywhatkit.sendwhatmsg('+91xxxxxxxx', 'Your Message', 12, 01)
This code means it will send ‘Your Message’ to contact X at “12:01”. Now run this code and you will see a message like this —
In 10 seconds, web.WhatsApp.com will open, and after 15 seconds, a message will be delivered by Whatsapp.
Great!! You have successfuly sent a message using python.
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 link.
Once you have the link of the group, 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 link.
import pywhatkit #syntax: group link, message, hour and minutes pywhatkit.sendwhatmsg_to_group("group-link", "Your Message", 12, 01)
In this article you have successfully learnt how to send an automated message using python library “pywhatkit”.
I hope you find this article helpful and was worth it. Feel free to share this article with your Programmer Friends.