Improve Your Personal Relationships — With a Touch of Automation and Python

Rajat Mehta
The Startup
Published in
5 min readDec 3, 2020
Photo by 🇨🇭 Claudio Schwarz | @purzlbaum on Unsplash

Maintaining personal relationships has become vital these days. Nurturing the network is as important as building the network. We can’t just keep on adding connections on LinkedIn and Facebook without ever talking to them. That is not how an organic network is built. Lots of self-help books that teach building friendships in and out of work always highlight the fact that we need to keep the connection alive by having some conversations now and then.

Now the question arises how exactly should we start the conversation with the network? For introverts trying to open up - this question can be terrifying.

The easiest way to begin a conversation or to continue an old chat is to wish them on any festival.

Wishing someone on an occasion like Christmas or New Year is a foolproof icebreaker. It works like a charm with anyone like a new gym partner, current manager or ex-colleagues. Nobody is too busy to return a simple “thank you”. Everyone feels good that you remembered them on the occasion and wished them. Now that the conversation has started, you can take it in any direction you want.

Recently, on Diwali (Indian festival of lights) I opened up Whatsapp and started sending messages to my network - family, friends, current, and ex-colleagues. I drafted a simple message and forwarded it to all the chats. Since I sent the message to people whom I usually chat with, I found their chats at the top of my list. Whatsapp allows forwarding to 10 people at once so after two rounds of forwarding I was DONE. Here’s the catch - the top 20 people whom I sent the message to were already the ones I was in contact with. I should’ve sent the message to people who were lost in the long list of my chats.

That’s when the programmer in me shouted VOILA!

As a Data Scientist, I faced a new but an interesting challenge of automating tasks that a group of business analysts performed daily. I was able to accomplish this using Python and Selenium. So, now I have used those skills to automate the basic task of sending messages on Whatsapp without any APIs.

Get your coffee ready, it’s time to convert caffeine into code.

Step 1. Preparing the data

The data is kept straightforward. Only a CSV with 2 columns - the recipient’s details (mobile number or group name) and the message - is needed. Here is the sample dataset -

Step 2. Installing Selenium

Use pip install selenium to install the library. Selenium requires a driver to interact with the browser of your choice. In this example, we’ll use Firefox. You can find URLs to download drivers here — https://pypi.org/project/selenium/

Geckodriver is the driver for Firefox which can be downloaded here-https://github.com/mozilla/geckodriver/releases. Download the latest version and add Geckodriver to PATH variable of the OS.

P.S. — Ensure that you have the latest Firefox and geckodriver is in PATH variable to prevent any errors at a later stage.

Step 3. Understand a few Selenium basics

Selenium automates browsers. Any action like click, scroll or type etc can be performed using Selenium which makes it perfect for our needs. Before we dive into the coding lets understand the basic working of Selenium.

Since we automate the actions on the browser, first we need to identify which element of the webpage we need to act on. There are multiple ways of finding the element in Selenium. A thorough guide can be found here.

For automating Whatsapp we’ll use XPath. In simple words, XPath is used to identify the element on the basis of its tag name and its associated attributes. Its syntax is something like this:

xpath = //tag_name[@attribute_name = “attribute_value”]

Let’s take a very simple HTML example:

<div class = “highlighted text” id = “block 1”>

If we need to find this element on the page and we know that id attribute uniquely identifies it then a simple XPath expression to find would be:

xpath = //div[@id=“block 1”]

Isn’t that pretty neat?

Let’s test this knowledge on web.whatsapp.com. Open the website and scan the QR code. Once you see your chats press Ctrl+Shift+I to inspect the webpage. It should look something like this -

Image taken by author.

We need the XPath for 2 elements - the search bar to type the recipient’s name and the message bar to type the message. On selecting both these bars in inspection mode, there’s one common attribute -

contenteditable = “true”

Using this attribute we can get the XPath for them and perform our actions.

Step 4. THE CODE

The code is rather straightforward. After importing the libraries and loading the CSV, we open the browser and let it go to web.whatsapp.com. Point to note here is that we need to scan the QR code every time we go to web.whatsapp.com. So, Selenium is paused for 10 seconds until we scan the QR code using the time module’stime.sleep(10). Line 22 applies a function send_messageto all rows from the CSV.

send_message imitates how we would send messages. On receiving the recipient and the message, it first finds the search bar to input the message. Since contenteditable = “true” can retrieve the search bar, we use browser.find_elements_by_xpath() function. The first element of the resulting list will be the search bar and the second element will be the message bar.

On getting the search_bar the recipient’s name is typed using send_keys and Enter key is pressed using the same function. time.sleep function allows the page to load the results after which the process is repeated to type the message in the message bar. Pressing Enter again sends the message. The process repeats for all the contacts. The code below cleans up the memory by closing the browser.

With Christmas and New Year coming up, I hope you find this useful.

Make use of it in any way possible and keep up with those conversations. Do let me know how you use it and your thoughts in the comments or connect with me over LinkedIn -

--

--

The Startup
The Startup

Published in The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +772K followers.

Rajat Mehta
Rajat Mehta

Written by Rajat Mehta

AI Engineer, athlete and a gardener who loves to read and write about data, automation and life. https://www.linkedin.com/in/rajat-mehta-/

No responses yet