DEV Community

Cover image for Automating WhatsApp web with alright
Jordan Kalebu
Jordan Kalebu

Posted on • Updated on

Automating WhatsApp web with alright

alright?

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

This article is originally found in these places;

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;

So I tried pywhatkit, really cool one well crafted to be used by others but its implementations require you to open a new browser tap and scan QR code every time you send a message no matter if its the same person, which was deal-breaker for me.

I then tried pywhatsapp which is based on yowsup and thus requiring you to do some registration with yowsup before using it of which after bit of googling I got scared of having my number blocked when I do that so I went for the next option

I then went for WebWhatsapp-Wrapper, it has some good documentation and recent commits so I had hopes it gonna work but It didn't for me, and after having couples of errors I abandoned it to look for the next alternative.

Which is PyWhatsapp by shauryauppal, which was more of cli tool than a wrapper which suprisingly worked and it's approach allows you to dynamically send whatsapp message to unsaved contacts without rescanning QR-code everytime.

So what I did is more of a refactoring of the implementation of that tool to be more of wrapper to easily allow people to run different scripts on top of it instead of just using as a tool I then thought of sharing the codebase to people who might struggled 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 I gotcha you, everything will work well if you just carefully follow through the documentation.

Installation

We need to have alright installed on our machine to start using which can either be done 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 
....
Enter fullscreen mode Exit fullscreen mode

installing from pip

pip install alright 
Enter fullscreen mode Exit fullscreen mode

Setting up Selenium

Underneath alright is Selenium which is one 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 gonna require you to have Chrome and chromedriver other browser support coming soon.

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 a guide to check the version of chrome you're using

Adding selenium driver to the path

One more final step to set up is to add the selenium driver location to path so as it can be discovered by alright, which varies depending on the operating system you're using.

For instance, let's say example 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), Here how you would do that.

Linux

For linux to permanently add path to browser do this;

nano ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode
Window

For window users, you follow this guide to actually do that.

Now after that, we're now ready to automating and controlling WhatsApp web using alright

What you can do with alright?

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 raise an error so make sure to close the controlled window before running another one

Unsaved contact vs saved contacts

Alright allows you to send the messages and media to both unsaved contacts as explained earlier but there is a tiny distinction on how you do that, you will observe this clearly as use the package.

The first step before sending anything to the user is first to locate the user and then you can start sending the informations thats where the main difference lies btn saved and unsaved contacts.

Saved contacts

To saved contact use method find_by_username() to locate saved user,you can also use the same method to locate WhatsApp groups, The parameter can be either be;

  • saved username
  • mobile number
  • group name

Here an Example on how to do that

>>> from alright import WhatsApp
>>> messenger = WhatsApp()
>>> messenger.find_by_username('saved-name or number or group')
Enter fullscreen mode Exit fullscreen mode

Unsaved contacts

In sending message to unsaved whatsapp contacts use find_user() method to locate the user and The parameter can only be users number with country code with (+) omitted as shown below;

>>> from alright import WhatsApp
>>> messenger = WhatsApp()
>>> messenger.find_user('255-74848xxxx')
Enter fullscreen mode Exit fullscreen mode

Now Let's dive in on how we can get started on sending messages and medias

Sending Messages

To send a message with alright, you first need to target a specific user by using the find_user() method, and then after that 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)    
Enter fullscreen mode Exit fullscreen mode

Multiple numbers

Here how to send a message to multiple users, Let's say we wanta 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 ")
Enter fullscreen mode Exit fullscreen mode

You have to include the country code in your number for this library to work but don't include the (+) symbol

Sending Images

Sending message is nothing new, its just the fact you have to include a path to your image instead or raw string characters and also you have use send_image(), Here an example;

>>> form alright import WhatsApp
>>> messenger = WhatsApp()
>>> messenger.find_user('mobile')
>>> messenger.send_image('path-to-image')
Enter fullscreen mode Exit fullscreen mode

Sending Videos

Samewise to videos just send_videos() method;

>>> from alright import WhatsApp
>>> messenger = WhatsApp()
>>> messenger.find_user('mobile')
>>> messenger.send_video('path-to-video)

Enter fullscreen mode Exit fullscreen mode

Sending Documents

The rest of the documents such as docx, pdf, audio, you name it falls into the category of documents and you can send_files() to that.

>>> from alright import WhatsApp
>>> messenger = WhatsApp()
>>> messenger.find_user('mobile')
>>> messenger.send_file('path-to-file')
Enter fullscreen mode Exit fullscreen mode

Well thats all for now for the package, to request new feature make an issue to the official repository.

Contributions

alright is an open-source package under MIT license, so contributions are warmly welcome whether that be a code, docs or typo just fork it.

when contributing to code please make an issue for that before going making your changes so that we can have a prior discussion on implementation

Issues

If you're facing any issue or difficulty with the usage of the package just raise one so as we can fix it as soon as possible.

Give it a star

Was this useful to you? then give it a star so that more people can know about this.

Credits

All the credits to;

Latest comments (6)

Collapse
 
vineshgada profile image
VineshGada • Edited

'alright' makes it realy easy to send WhatsApp messages via python. However, I run into the chromedriver issues when my Chrome updates. I changed the

        browser = webdriver.Chrome(
            ChromeDriverManager().install(),
            options=self.chrome_options,
        )
Enter fullscreen mode Exit fullscreen mode

to

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromiumService
from webdriver_manager.chrome import ChromeDriverManager
from webdriver_manager.core.utils import ChromeType

browser  = webdriver.Chrome(service=ChromiumService(ChromeDriverManager(chrome_type=ChromeType.CHROMIUM).install()))
Enter fullscreen mode Exit fullscreen mode

in init.py of the wrapper. It works fine for me now.

Probably, one can extend browser support also :

pypi.org/project/webdriver-manager/

Collapse
 
rajat0410 profile image
Rajat-0410

While using Sending Images Function in multiple number function it throws error

messenger.send_image('testimage.jpeg')
AttributeError: 'WhatsApp' object has no attribute 'send_image'
Enter fullscreen mode Exit fullscreen mode
Collapse
 
rezer68 profile image
rezer68

hi...
its really cool feature
how to use : WhatsApp.username_exists ?

Collapse
 
hq063 profile image
Gonzalo HQ063

Really cool feature, any way to use it for sending messages to a group chat?
Thanks

Collapse
 
kalebu profile image
Jordan Kalebu

Jus updated the package so firstly just upgrade the package

pip install --upgrade alright 
Enter fullscreen mode Exit fullscreen mode

and then instead of using find_user() user find_by_username(group-name)

>>> from alright import WhatsApp
>>> messenger = WhatsApp()
>>> messenger.find_by_username('saved-name or number or group')
Enter fullscreen mode Exit fullscreen mode

Let me know how it goes

Collapse
 
kalebu profile image
Jordan Kalebu

Not yet, working on it