For My Dad, I Automated Bulk WhatsApp Messaging using Python - Here's How

Written by DiveshLunker | Published 2020/09/07
Tech Story Tags: whatsapp-automation | whatsapp | whatsapp-web-hacks | whatsapp-web | automation | whatsapp-chatbot | we-building | hackernoon-top-story

TLDR For My Dad, I Automated Bulk WhatsApp Messaging using Python - Here's How to run the code. The full code can be seen here and the full of it can be used to send messages to anyone in the world if he/she exists on whatsapp:https://://wa.me/<number>?text=<text message> My dad even after it makes it easier to send two messages, just explaining it to him how to run it. The code snippet looks like this: driver get (https://web.whatsapp.com/send?phone=" +str(i) ") input_box = driver.find_element_by_xpath( '//[@id=”main/div[1]/div/divvia the TL;DR App

So, this Whatsapp just irritates my dad.
Starting with my dad who is a business man and who gives loan to few people. Now, for asking them to pay EMI, he does this long process:
Save PhoneNumber -> Open Whatsapp -> Check if phone Number exist on Whatsapp -> Open Number -> Paste the already copied message -> Click send button.


Total time wasted for each message — approx 40–50 seconds.
Now, there exist a WhatsApp API which made me think if this whole messaging could be automated???
Lets jump to the chase:
This is the exact link which can be used to send number to anyone in the world if he/she exists on whatsapp:
https://wa.me/<number>?text=<text message>
Yehhh, I just found a way to automate, I will just loop up the link with a new number each time.
Now comes a burst of problems doing this…
  1. I had to open this link in google using Python.
  2. wa.me blocks you after certain usages. Atleast for me in my browser it stopped working after messaging like 50–70 people.
  3. Even if that many people is fine, it has two pages before the whatsapp opens, where some buttons need to be clicked in order to proceed further. Surprise is one of the button doesn’t get clicked by selenium.
  4. Adding the message in the link itself and messaging 100s of people would make WhatsApp suspicious of spam and that would result in blocking on phone Number. And believe me it does does ban the phone numbers, my friends got banned while I was experimenting.
  5. Since I was using WhatsApp Web, it should not ask for scanning QR code for each number.
So, these challenges had to be sorted out before launching this script.
Let us solve the problems now one by one, one by one…
  1. Opening google could be solved by doing two tasks: Downloading google driver them adding selenium library in the script.
  2. wa.me is of no use, I just took off the exact WhatsApp web link which loads when we open a new number which looks something like this: https://web.whatsapp.com/send?phone=<number>
  3. Since, I just shifted from wa.me, the problem of clicking the button solved.
  4. Now, if we add the message in the link it self, WhatsApp will identify us, hence I used selenium to enter the text in the message box. Only thing I had to do is find the X-Path of the text box and that is: “//*[@id=”main”]/footer/div[1]/div[2]/div/div[2]”
  5. So, now I am just able to pass the keys to this text box like we do in other forms if you have ever used selenium. The code snippet looks like this:
  6. driver.get("https://web.whatsapp.com/send?phone="+str(i)") 
                 
    input_box = driver.find_element_by_xpath('//[@id="main"]/footer/div[1]/div[2]/div/div[2]')        
        
    for j in messages:
          input_box.send_keys(j)
    Ok, now I have successfully entered the message in the text box, but clicking the button using selenium wasn’t happening, atleast I failed to do so. If any of the readers are able to, just comment below.
    Hence, I used a simple technique which is clicking enter after the message is entered in the text box and the modified code looked something like this:
    Yehhhh, now the message is sent successfully to the number specified. The only thing now left to do is loop withing the list of Phone Numbers and I think showing how to write a for loop will be waste of time and space.
    Lucky us, once we scan the QR code, when the next link opens in a new window it won’t ask for it again. Hopefully WhatsApp doesn’t fix that.
    I think this is it, messaging bulk on whatsapp just became easier and hence, I thought of releasing a library and it is called Whatsall and the full code can be seen here.
    I think now anyone can use this to send messages in bulk. Huhh problem solved, but…
    Again a list of problems come up:
    1. Images can’t be sent.
    2. New lines could not be added using the link I am using above. Each time a new line “\n” is used in the message, it just makes it two messages.
    3. My dad even after 100 tries of explaining him how to run the code, he still can’t😪😪😪.
    And all I wanted to do was to make my dad’s work easier but it still remains the same. Hence, I decided to make an app
    This app should be in such a way that it solves the above three problems. And well well well, it just addressed all the above problems and the code of the app is another time’s story. Until then just use my app.
    Link for the app: https://play.google.com/store/apps/details?id=com.divesh.whatsall.automessenger_free
    Please copy the link - most likely the link get altered here...
    I guess that’s it for this particular blog. If you liked it, give it a thumbs up and in the next blog we will see how to make the whatsapp automation app.
    Thank you…

Written by DiveshLunker | Engineer By Choice...
Published by HackerNoon on 2020/09/07