24 Jun Automate Your WhatsApp Messages with Python in Minutes
In this tutorial, we’ll learn how to automate WhatsApp messages with Python programming language.
Python, as we know, is a very flexible language utilized by Developers daily in performing different tasks ranging from website and software development to task automation, data analysis, data visualization, game development, embedded applications, and many more.
We want to automate our tasks; implementing this will make life easier. We don’t have to be bothered about forgetting to send messages to our loved ones, especially on their Birthdays or any other urgent messages.
With Python, we can easily automate our message to be sent at our desired time.
The Python library we’ll use in this project is Pywhatkit, a library used for sending WhatsApp messages. It has a lot of dependencies, and various WhatsApp features like sending images and gifs to personal contact or even a group chat.
Now, let’s see how we can use this library. This implementation will be done in a step-by-step process.
Step 1: Set up a Virtual Environment
The first thing we need to do is to set up a virtual environment for our project. We do that to keep our dependencies isolated, whereby we don’t break our system tools.
Note: A windows machine is used in implementing this project.
1 #If you don’t have virtualenv you can install with the command 2 pip install virtualenv 3 4 #Create the virtaul environment 5 virtualenv mypython 6 7 #Activate the Virtual Environemt 8 mypthon\Scripts\activate
Step 2: Install Pywhatkit
After you’ve activated your virtual environment, what we do next is install the Pywhatkit Library and flask by using the command below;
1 pip install pywhatkit 2 pip install flask
Step 3: Send Message to WhatsApp Contact
Here, we’ll demonstrate how to send a message to a particular contact on our WhatsApp App. Before going into that, you must log in to your WhatsApp account using WhatsApp Web. You can use this address WhatsApp Web on your Chrome Desktop.
Here is a code to demonstrate how to send our message in seconds. We used the .sendwhatmsg endpoint to pass in our arguments.
The first argument, “+xxxxxxxx “is telling us what contact number we want to send our message to, while the “This is our xxxxxx “is the message body we want to send. This “13” is the time in hours, while the next number is time in minutes, and the next one is the time in seconds.
The “True” argument indicates we want our WhatsApp tab to be closed automatically in our browser after the message has been delivered. The last argument is the time we want the tab closed in seconds.
1 #import the library 2 import pywhatkit 3 #pass in your arguments here 4 pywhatkit.sendwhatmsg("+xxxxxxxxx", "This is our first demo text message with python", 13, 15, 10, True, 2
Step 4: Send Message to WhatsApp Group
We can also send messages to a group chat in Python; to do that, we need the “id” of the group. This will be used in place of a number. You can get the id of your group by clicking the group info → scroll down and click on the invite via link → click on the copy link. Now you will extract the suffix part of the link, which will be the id to use in the code.
1 #import the library 2 import pywhatkit 3 #pass in your arguments here 4 pywhatkit.sendwhatmsg_to_group("your group id", "This is our first demo text message with python", 13, 20, 10, True, 2)
Step 5: Send Images To WhatsApp Contact & Groups
You may also want to share images with your contact or group. We can do that using the code below.
1 #import the library 2 import pywhatkit 3 # Send an Image to a Group with the Caption "Check out this image" 4 pywhatkit.sendwhats_image("your group id", "Images/Hello.png", "Check out this image") 5 6 # Send an Image to your contact with the no Caption 7 pywhatkit.sendwhats_image("+xxxxxxxxxxx", "Images/Hello.png")
Wow! You created your own powerful, database-driven WhatsApp automation tool. To really appreciate Python’s simplicity, you must now give it a try.
Hope you enjoyed this post. Thanks for reading.
No Comments