You are on page 1of 7

18.09.

22, 14:39 I Made a Python Bot to Send “Good Night” Texts to My GF | by Pythonians | Medium

To make Medium work, we log user data. Get started


Open in app
By using Medium, you agree to our
Privacy Policy, including cookie policy.

Pythonians Follow

Jun 13 · 5 min read · Listen

Save

I Made a Python Bot to Send “Good Night”


Texts to My GF
Here’s how you too can build it very easily.

https://pythonians.medium.com/i-made-a-python-bot-to-send-good-night-texts-to-my-gf-7c290273a01a 1/7
18.09.22, 14:39 I Made a Python Bot to Send “Good Night” Texts to My GF | by Pythonians | Medium

Photo by Eyestetix Studio on Unsplash


To make Medium work, we log user data. Get started
Open in app
By using Medium, you agree to our
Having a functioning relationship is hard,
Privacy Policy, especially
including when you’re a full-time coder. You
cookie policy.

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

https://pythonians.medium.com/i-made-a-python-bot-to-send-good-night-texts-to-my-gf-7c290273a01a 2/7
18.09.22, 14:39 I Made a Python Bot to Send “Good Night” Texts to My GF | by Pythonians | Medium

Plus, it is completely safe to use. I made the whole project open-source so that you can
To make Medium work, we log user data.
Open in app Get started
see what the code does by yourself. The package
By using Medium, isour
you agree to completely built with python using
Privacy Policy, including cookie policy.
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
https://pythonians.medium.com/i-made-a-python-bot-to-send-good-night-texts-to-my-gf-7c290273a01a 3/7
18.09.22, 14:39 I Made a Python Bot to Send “Good Night” Texts to My GF | by Pythonians | Medium

with an ensurepip module,Towhich can install pip in a Python environment.


make Medium work, we log user data.
Open in app Get started
By using Medium, you agree to our
Privacy Policy, including cookie policy.
python -m ensurepip --upgrade

Download Package
Open up your terminal/cmd and install it1Kusing the37pip 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

https://pythonians.medium.com/i-made-a-python-bot-to-send-good-night-texts-to-my-gf-7c290273a01a 4/7
18.09.22, 14:39 I Made a Python Bot to Send “Good Night” Texts to My GF | by Pythonians | Medium

The names in the text file should be exactly the same as they are saved on your phone. You must be
To make Medium work, we log user data.
Open in app Get started
already talking to them on WhatsApp.
By using Medium, you agree to our
Privacy Policy, including cookie policy.

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.

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)

https://pythonians.medium.com/i-made-a-python-bot-to-send-good-night-texts-to-my-gf-7c290273a01a 5/7
18.09.22, 14:39 I Made a Python Bot to Send “Good Night” Texts to My GF | by Pythonians | Medium

time.sleep(60)

else:
To make Medium work, we log user data. Get started
Open in app
By using Medium, you agree to our
time.sleep(30)
Privacy Policy, including cookie policy.

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.

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.

THANK YOU

https://pythonians.medium.com/i-made-a-python-bot-to-send-good-night-texts-to-my-gf-7c290273a01a 6/7
18.09.22, 14:39 I Made a Python Bot to Send “Good Night” Texts to My GF | by Pythonians | Medium

Be One Of Us- A GREAT PYTHONIAN.


To make Medium work, we log user data. Get started
Open in app
Connect Yourself with us. By using Medium, you agree to our
Privacy Policy, including cookie policy.

Your email

Subscribe

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our
privacy practices.

About Help Terms Privacy

Get the Medium app

https://pythonians.medium.com/i-made-a-python-bot-to-send-good-night-texts-to-my-gf-7c290273a01a 7/7

You might also like