Automating WhatsApp Web with Alright and Python

Written by kalebujordan | Published 2021/06/25
Tech Story Tags: python | python-programming | python-tutorials | python-programming-lists | selenium | python-dev-tips | python-developers | programming | web-monetization

TLDR Alright is a python wrapper that helps you automate WhatsApp web using python. It gives you the capability to send messages, images, video, and files to both saved and unsaved contacts without having to rescan the QR code every time. Using alright is Selenium, which does all the automation work by directly controlling the browser, so you need to have a selenium driver on your machine for alright to work.What you can do with alright: Send Messages (Sending a message) and send images (Images) to multiple users.via the TL;DR App

Alright is a python wrapper that helps you automate WhatsApp web using python, giving you the capability to send messages, images, video, and files to both saved and unsaved contacts without having to rescan the QR code every time.

Why Alright?

I was looking for a way to control and automate WhatsApp web with Python; I came across some very nice libraries and wrappers implementations, including:
  1. pywhatkit
  2. pywhatsapp
  3. PyWhatsapp
  4. WebWhatsapp-Wrapper
So I tried
pywhatkit
, a well crafted to be used, but its implementations require you to open a new browser tab and scan QR code every time you send a message, no matter if it's the same person, which was a deal-breaker for using it.
I then tried
pywhatsapp
,
which is based on
yowsup
and require you to do some registration with
yowsup
before using it of which after a bit of googling, I got scared of having my number blocked. So I went for the next option.
I then went for WebWhatsapp-Wrapper. It has some good documentation and recent commits so I had hoped it is going to work. But It didn’t for me, and after having a couple of errors, I abandoned it to look for the next alternative.
PyWhatsapp by shauryauppal, which was more of a CLI tool than a wrapper, surprisingly worked. Its approach allows you to dynamically send WhatsApp messages to unsaved contacts without rescanning QR-code every time.
So what I did is refactoring the implementation of that tool to be more of a wrapper to easily allow people to run different scripts on top of it. Instead of just using it as a tool, I then thought of sharing the codebase with people who might struggle to do this as I did.

Getting started

You need to do a little bit of work to get alright to running, but don’t worry, everything will work well if you just carefully follow through with the documentation.

Installation

We need to have alright installed on our machine to start using, either directly from GitHub or using pip.
Installing directly
You first need to clone or download the repo to your local directory and then move into the project directory as shown in the example and then run the below command:
git clone https://github.com/Kalebu/alright
cd alright
alright > python setup.py install 
....
Installing from pip
pip install alright

Setting up Selenium

Underneath alright is Selenium, which does all the automation work by directly controlling the browser, so you need to have a selenium driver on your machine for alright to work.
So primarily, I developed alright and tested on a Chrome browser, and therefore it's gonna require you to have Chrome and chromedriver.
You need to make sure you download the chrome driver compatible with the Chrome version you’re using; otherwise, it won’t work, and also, don’t forget to extract the zip version of a driver.
Here is a guide to check the version of chrome you’re using

Adding selenium driver to the path

Add the selenium driver location to the path to be discovered by alright, which varies depending on the operating system you’re using.
For instance, let's say the current location our driver is in
/home/kalebu/chrome-driver
 
(You can view the full path to your driver by running the
PWD
command).
Linux
For Linux to permanently add the path to the browser, do this:
nano ~/.bashrc
and then add the command to export the folder at the very bottom of the file & then Ctrl+X to save it
export PATH="$PATH:/home/kalebu/chrome-driver"
Windows
For window users, follow this guide.
Now, after that, we’re ready to automate and control WhatsApp web using alright.
What you can do with alright:
  1. Send Messages
  2. Send Images
  3. Send Videos
  4. Send Documents
When you’re running your program made with alright, you can only have one controlled browser window at a time. If you run while another window is live, it raises an error, so make sure to close the controlled window before running another one.

Sending Messages

To send a message with alright, you first need to target a specific user by using the
find_user()
method, and then you can start sending messages to the target user using the
send_message()
method as shown in the example below:
>>> from alright import WhatsApp
>>> messenger = WhatsApp()
>>> messenger.find_user('2557xxxxxz')
>>> messages = ['Morning my love', 'I wish you a good night!']
>>> for message in messages:  
        messenger.send_message(message)

Multiple numbers

Here how to send a message to multiple users. Let’s say we want to wish merry-x mass to all our contacts. Our code is going to look like this:
>>> from alright import WhatsApp
>>> messenger = WhatsApp()
>>> numbers = ['2557xxxxxx', '2557xxxxxx', '....']
>>> for number in numbers:
        messenger.find_user(number)
        messenger.send_message("I wish you a Merry X-mass and Happy new year ")
You have to include the country code in your number for this library to work, but don’t include the (+) symbol.

Sending Images

Sending a message is nothing new - it's just the fact you have to include a path to your image instead, or raw string characters, and also you have to use s
end_image()
, Here is an example:
>>> form alright import WhatsApp
>>> messenger = WhatsApp()
>>> messenger.find_user('mobile')
>>> messenger.send_image('path-to-image')

Sending Videos

Similarly, for videos, just use
send_videos()
method;
>>> from alright import WhatsApp
>>> messenger = WhatsApp()
>>> messenger.find_user('mobile')
>>> messenger.send_video('path-to-video)

Sending Documents

The rest of the documents, such as Docx, pdf, audio, etc. falls into the category of documents, and you can use
send_files()
to do that.
>>> from alright import WhatsApp
>>> messenger = WhatsApp()
>>> messenger.find_user('mobile')
>>> messenger.send_file('path-to-file'))
Well, that's all for now for the package. To request a new feature makes an issue to the official repository.

Contributions

Alright is an open-source package under MIT license, so contributions are warmly welcomed. When contributing to code, please create an issue before making your changes to discuss implementation.

Credits

All the credits to:
  1. kalebu
  2. shauryauppal
  3. and all the contributors

Written by kalebujordan | I'm a Mechatronics engineer | Pro Python Developer | AI Enthusiast
Published by HackerNoon on 2021/06/25