Automating WhatsApp messages with Python is one of the coolest projects I have ever created. The beauty of this project is it only takes two lines of code to send a message to someone or even a group using your Python script. Yeah! Good things always come in small packages. 😉
Disclaimer: This is a fun Python project and has been strictly created for educational purposes only. Personally, I do not recommend you send private messages using this technique. But you can definitely send messages like normal good morning texts and notifications using the technique described in this tutorial.
Thus, in this fun project, I will be demonstrating how you can automate and send WhatsApp messages/notifications to a user or a group using the PyWhatkit library. Let’s have a look at the two-step process that will allow you to send WhatsApp texts using Python.
Before you begin, ensure that you are logged in to WhatsApp Web. Also, ensure that your phone is not too far away from your laptop/system. Another important criteria is that your phone must be on the same network as your computer. Otherwise, this technique won’t work. So, yes! There are a few limitations.
Step 1: Installing PyWhatkit
PyWhatKit is a fabulous Python library with amazing features. Presently, it is one of the most popularly used libraries in Python that is used for WhatsApp and YouTube automation. The beauty about this library is it’s easy of use and it does not require you to do any additional setup.
pywhatkit in your system, open your terminal and run the command below:
pip install pywhatkit
Note that the installation might take some time. So, please be patient!
Step 2: Sending The Message to a WhatsApp Contact
PyWhatkit facilitates you with a method known as
.sendwhatmsg which will allow you to send a message to a certain contact. Here’s how you can send the message using your script –
import pywhatkit as pwk # syntax: phone number with country code, message, hour and minutes pwk.sendwhatmsg('+91xxxxxxxx', 'Your Message!', 12, 25)
Explanation: (i) Import the
pywhatkit library. (ii) Use the
sendwhatmsg to send a message (in this case ‘
Your Message!‘) to the specified number
Once you execute the above code you may get a message like this:
And there we go! After a few seconds your browser will open up and the message will be automatically delivered to the contact.
Note that you can also send a message instantly to a contact using the
sendwhatsmsg method as shown below:
import pywhatkit as pwk pwk.sendwhatmsg_instantly("+91XXXXXXXXXX", 'Hello!')
Sending WhatsApp Messages to a Group
You can also send WhatsApp message to a group instead of just sending the message to a user. This can be really handy if you want to send notifications to a group of users. The process to do so is quite similar. However, in this case you need to grab the group id.
You can find the group id using the following steps:
- Open the WhatsApp group where you want to send the message and click on “Group Info” section.
- Scroll down to look for ‘Invite via link’ option and click on it.
- You will then see a link. The suffix in that link is the id of the group.
Once you have the group id, use the following code to send a message to the group:
import pywhatkit as pwk pwk.sendwhatmsg_to_group_instantly("H3JukSmlGGUJzPZPkiw4oB", 'Hello!')
That’s it! Now execute the script and the message will be sent to the group.
There we go! We have successfully created a script that automates WhatsApp messages for us. I hope this project added some value and helped you in your coding quest. Stay tuned and subscribe for more interesting projects and tutorials.
Useful Read: Block Websites Using Python in Windows