Annoying friend? Develop a whatsapp chatbot

Seemant Singh
4 min readMar 27, 2021

--

Photo by Tech Daily on Unsplash

Sunday afternoon, sat down with snacks and a full season of Attack on titan. But as soon as the title song hits you hear your Whatsapp notification. It’s that annoying friend who might call if you don’t read and reply the message. So what do you do? Create a Whatsapp chatbot!

But before you start creating, try it out —

pip install whatsappchatbot #to install the packagepython #start python interpreterimport whatsappchatbot #import packagewhatsappchatbot.start_chat("Annoying-friends-name-as-in-the-chatfile", "Your-name-as-in-the-chatfile", "location-of-chatfile")

check full code at https://github.com/maxsanwal/whatsappChatbot

Requirements

  1. Python 3

Overview

  1. We’ll download the previous chat history as a text file.
  2. We’ll do some preprocessing to get 2 different lists of messages — Messages you sent and messages you received.
  3. We’ll create a function that takes new received message as an input, finds the most similar received message from chat history list and output the old reply to that similar message from sent message list.
  4. Then we’ll automate the process of reading a new received message, feeding it to above function and send the output as a reply.

Downloading previous chats

  1. Open Whatsapp on mobile.
  2. Select that annoying person’s chat.
  3. Click on options (three vertical dots) on top right corner of the app and select “More” options.
  4. Select “Export chat” and click on “WITHOUT MEDIA”. From here you can share you chat “.txt” file to your google drive so that it could be downloaded in your computer where we’ll do rest of the development.

Seperating Messages

Let’s start by reading the file as a string into “chat_string”. Then we’ll split the lines in the chat file using a regular expression “re_date_time”. Check the chat file to understand how the regex is formated. To learn more about regex checkout https://regexr.com/.

After spliting the lines we’ll have a list of chats as elements of a list “chat_list”. Now we just have to seperate the messages into two list as “sent” and “received”. That’s what you can see in the try except block — we are checking if an element consist of sender name or a receiver name and then append them in the respective list. We have a while loop because we are appending the continuous messages by the same sender into one line.

At the end we are just removing removing extra elements from either of the list to maintain same amount of sent and received message.

One thing that you might notice here is we are using a function “remove_emoji_newline”. Its takes a string input and replaces emojis with empty string.

Predicting Reply

Before prediction we’ll do some cleaning. We’ll remove all the punctuations and lemmatize all the words.

Lemmatization is the process of grouping together different inflected forms of a word so they can be analysed as a single item. Read more here https://www.geeksforgeeks.org/python-lemmatization-with-nltk/

In the “response” function you can see we are using TfidfVectorizer and cosine_similarity to find the most similar sentence to the currently received message based on word occurence in both the sentence. These are the steps:

  1. We are first appending the new message in the old messages list.
  2. Then vectorizing all the sentences so that we can find the cosine similarity between each element and the latest element(that we just appended).
  3. Then find the index of the highest value of cosine_similarity using argsort.
  4. This is the index of the message in “all_received_messages” which is most similar to the new message, therefore if we check the “sent_messages” list this should be the index of the message that we sent as a reply for the same.

Automating the Chat

We are using Selenium to automate our task. First we’ll open whatsapp web on chrome browser. Then the “automation” function will open the particular contact’s chat that we want to chat with and return the class for the input box where we need to enter the message.

Finally the “reply” function will be used to reply for incoming messages —

  1. This function will keep looking for new messages in a loop.
  2. As soon as a new message comes in, it will pass this new message to “response” function which will give a reply message as an output.
  3. We’ll type this message in the “input_box” class and send.

Now you can just run your program and enjoy aot, because it will take care of any messages from your annoying friend

I know its not always going to give the best reply, but we can make improvements. This is the github link where you can suggest new features improvement and report all the 🐞

--

--

Seemant Singh
Seemant Singh

Written by Seemant Singh

Writer, developer, developing writer.

No responses yet