Behind AI #10: Automate 4 Boring Tasks in Python with 5 Lines of Code
Automation scripts with a few lines of Python code.
Think of the most boring task you’ve ever done at work. Chances are, you can automate it with Python.
You only need to open your favorite text editor and start writing code. However, you might struggle to automate some tasks more than others and end up wasting more time.
What most people don’t know, however, is that there are less-known libraries that take care of the hardest part of the automation, so you only need to write a few lines of code to get the job done.
Below are 4 tasks that you can automate with Python in 5 minutes or less. You’ll need to write 5 lines of code or so to automate each of them (seriously).
1. Sending Messages on WhatsApp
The easiest way to send messages on WhatsApp in Python is using a library called pywhatkit
.
First, we need to install it, so open up a terminal and run the following command (if possible, install it in a new virtual environment).
pip install pywhatkit
The installation might take a minute, but, don’t worry, writing the script to send the message would take less.
Note: You need to log in to your WhatsApp account through WhatsApp Web before working with this library. If you’re not logged in, you won’t be able to send messages with pywhatkit
. Also, I don’t recommend you to use this library in case you need to send a message that needs to remain private.
Now it’s time to send the message. We can send a message to a WhatsApp contact using the code below.
import pywhatkit
# syntax: phone number with country code, message, hour and minutes
pywhatkit.sendwhatmsg('+1xxxxxxxx', 'Message 1', 18, 52)
Keep in mind that the “18” and “52” are the hour/minute your message is being scheduled, so try to set it within 1 or 2 minutes to see the results faster.
Once you run the code, you’ll get this message:
In 5 seconds, web.WhatsApp.com will open, and after 15 seconds, a message will be delivered by Whatsapp.