Hi! Let’s automate WhatsApp with the pywhatkit library. We will be using Python 3.8.10. Let’s go! 🔥⚡
The pywhatkit library is a brilliant little tool that allows us to automate a number of WhatsApp functions such as:
- Sending messages
- Sending images/files
- Sending files/messages to a group
- Play a Youtube Video
The first step is to get WhatsApp activated and working on your PC or Workstation where your Python script will be executed. You will need a WhatsApp account for this, obviously, and you will need to visit the WhatsApp Web website HERE. Follow the instructions on that page to get WhatsApp working on your computer. We suggest you use the Chrome browser for this example. Get Chrome HERE.
Once you get WhatsApp up and working on your computer, you need to install a few Python Libraries with the following commands:
pip install Flask
pip install pywhatkit
Run those two commands in the order presented: Flask first, then pywhatkit second. Read the output and ensure that both are installed successfully. When that is done, you can write the following code:
import pywhatkit as py
py.sendwhatmsg("+1*******************", "Code Everyday!", 16, 15)
py.sendwhats_image("+1*******************", "HappyBirthday.png")
py.playonyt("Python")
Let’s explain what is happening here:
- First we include the pywhatkit library.
- The method py.sendwhatmsg() accepts 6 parameters but here we are only giving 4:
- First parameter, phone_num is required and is the phone number that the message will be sent to. The recipient must also have WhatsApp. Be sure to include the full number with the country code in the format: “+1xxxxxxxxxxx”
- Second parameter, message is required and is the text of the message to be sent.
- Third, time_hour which is the hour of the day to schedule the message for. This MUST be in 24HR clock format.
- Fourth, time_min which is the minute of the day to schedule the message for.
- The phone number used in this example is a dummy value. Make sure you change the code to include a real and valid WhatsApp recipient phone number BEFORE you run the code.
- The method py.sendwhats_image() will send an image to the phone_num specified in the first parameter. The second parameter is where you specify the name and path of the image. In this case, our image was in the same path as the Python script.
- Finally, the method py.playonyt() will take the topic supplied as the parameter and play the first video in search results for that keyword.
When the code executes you will see the following output:
#output
#In 37 Seconds WhatsApp will open and after 15 Seconds Message will be Delivered!
After the time shown in that message elapses, three things will happen:
- If you used Chrome and are signed into WhatApp Web, the first statement will execute and a new tab will automatically open at the exact time specified in the py.sendwhatmsg() method. The message will appear in the send text box and after a brief pause it will be sent to the WhatsApp recipient specified.
- After a little while, the second statement will execute and yet another tab will open automatically and the image specified will be sent to the specified WhatsApp recipient.
- Finally, when the third statement executes yet another tab will open and it will automatically play the first Youtube video returned in the search results for the topic specified.
Great right? This is a simple script so a few things may go wrong such as the message failing to send, or WhatsApp Web freezing. If any of this happens, make sure that you specify the correct phone number to send the message to in the code and make sure it includes the country code. Also make sure the time you set for the message to be sent isn’t too far in the future.
Second, ensure that WhatApp Web is up and working when the script runs. I have found that the code works better when the app is open and working on your phone. Ensure that your internet connection is working too. If all else fails, reinstall the packages.
We hope this helped! Automation is the FUTURE and it saves time 😊. Find out more about pywhatkit HERE. Good luck and be good! 👌👌👌