Having a functioning relationship is hard, especially when you’re a full-time coder. You have to take care of her and her needs all the time, and most importantly, if she is clingy, you have to send “Good night” texts every single night.
But I sit down to code after dinner and honestly, opening WhatsApp to text someone after that is very distracting. I mean, I gotta take care of hundreds of methods and classes in 3 different windows. WhatsApp web works without having the internet-connected nowadays, but one more tab on Chrome is a big jab at my poor RAM.
So, I thought about automating the whole process to give myself some relief. Yeah, I know, you’re going to call me a bad boyfriend, but I just made sending a boring good-night text so damn cool. I coded a bot to do it for me!
So, let’s see how I did it. Thank me later (after reading this article, by buying me a coffee maybe).
Backstory:
I needed to send some promotional messages to some of my colleagues some months back. But sending the same text to everyone by copy-pasting in everyone’s chat is so tedious and time-consuming. Plus doing it for a thousand people is just obnoxiously repetitive.
So, I coded a python package to automate the whole thing.
Yes, there are other python packages to do similar things, but honestly, they suck. Every one of those packages opens up an ugly Chrome window and then does the job. If I minimize the window, the process fails. I wanted something that works in the background.
So, I coded AutoWhatsPy, a package to automate your WhatsApp, that works in the background (opens no ugly window).
You can just assign a task to a program written using the package, and it will work in the background. Technically it does open a browser window but it doesn’t open in the foreground. You can keep doing other things on your PC while it works in the background. Plus a headless firefox browser window
Plus, it is completely safe to use. I made the whole project open-source so that you can see what the code does by yourself. The package is completely built with python using selenium and Firefox gechodriver.
Installing AutoWhatsPy
Environment Setup
AutoWhatsPy requires Python 3.5 (Python 3.9 will be the best option) or above to run. I have tested it with Python 3.5. Make sure that you have Python added to your PATH. When you install Python in your Windows system, make sure to check ‘Add Python 3.x to PATH’. If you forget to do it, see this tutorial to know how to add Python to your PATH for Windows.
Install Python
For Windows and Mac
Download Python 3.9 from here. Use the installer to install Python in your System. Download ‘macOS 64-bit universal2 installer’ for Mac OS. Download ‘Windows x86–64 executable installer’ for your Windows 64 Bit system and ‘Windows x86 executable installer’ for Windows 32 bit system.
For Linux
Use the following command to install Python 3.9 on your Linux system.
apt-get install python3.9
Check pip
Make sure you have pip installed in your system. Use the following command to check if you have pip installed.
pip --version
If you see a message like ‘pip 21.2.2’ then you have pip installed on your system. Otherwise, follow this tutorial to install pip in your system. Generally, Python comes with an ensurepip module, which can install pip in a Python environment.
python -m ensurepip --upgrade
Download Package
Open up your terminal/cmd and install it using the pip command.
pip install autowhatspy
Programming
Now comes the programming part. To send a single text or image (or both) message to single/multiple contacts through WhatsApp, we have to use the message_to_contacts function from the package. Here’s the function and the arguments needed.
message_to_contacts(msg, contacts, gechodriver, gechodriver_log, user_profile, image)
Arguments
msg — Path to the text file containing the message text.
I created a file names goodnight.txt that contains just one line saying “Good night, babe”. The content of the file looks like this:
Good night, honey
contacts — Path to the text file containing the list of contacts.
In this case, it just contained the name of her contact name. The content of the file contacts.txt looks like this:
Bae II
The names in the text file should be exactly the same as they are saved on your phone. You must be already talking to them on WhatsApp.
gechodriver — Path to the gechodriver.exe file. Download the gechodriver.exe (link here) and send the path as an argument. You may use the absolute path or the relative path based on how your interpreter works. Make sure gechodriver.exe is in the same project folder you are working on. Or add the folder where gechodriver.exe is located in the PATH. Otherwise, an error might occur. If you still get an error, downgrade selenium to version 2.53.6.
gechodriver_log — Path to the text file where the logs of gechodriver will be saved. Make sure gechodriver.log is in the same project folder you are working on. Otherwise, an error might occur.
user_profile — Path to the saved firefox user profile.
Open firefox. Go to about:profiles and make a new profile.
Save it in your project directory with whatever name firefox assigns it.
Open that profile, open web.whatsapp.com and scan the QR.
Send the path of the profile as the argument to the function.
image — (Optional) Path to the image/video you want to send. Make sure that it is supported by WhatsApp and is of the permissible length.
Before I show you the code, I have a little request. Please become a member of Medium. Just for 5$ per month, you can read any article on Medium (not just my articles, any article). Click the link below. If you join from my referral link, I will get a little commission which will be very helpful for me in paying my bills.
Code
from datetime import datetime
import autowhatspy
import os
import time
path = os.path.dirname(os.path.realpath(__file__)) + "\\"
msg = path + "msg.txt"
contacts = path + "contacts.txt"
gechodriver = path + "gechodriver.exe"
gechodriver_log = path + "gecholog.txt"
user_profile = path + "firefoxprofile"
while True:
now = datetime.now()
current_time = now.strftime("%H%M")
if current_time == "2315":
autowhatspy.message_to_contacts(msg, contacts, gechodriver, gechodriver_log, user_profile)
time.sleep(60)
else:
time.sleep(30)
This sends her a “Good night, honey” text precisely at 11:15 PM. The code checks the current time every 30 seconds and when the time reaches 11:15 PM, it sends the text. If your PC runs 24/7, you can just keep this code running forever and ever and ever and ever and ever… (until she leaves you)
Conclusion
I dunno if she found out that I was using a bot to send her texts or not, but, (SPOILER ALERT), she broke up. So, now I have to code a bot to block her on WhatsApp, cause I am too weak to do it myself.
Then I also have to make a bot to flirt with another girl I like. That’s a lot of bots. Let’s use them as much as we can before the machine revolution happens.
Follow me to get notified when I code those bots as well.
I’d love to hear your thoughts about this, so feel free to reach out to me in the comments below!
— If this article helped you in any way, consider sharing it with 2 friends you care about.
Till then stay alive.
Original post -medium
Top comments (0)