We recently saw how we can automate the use of WhatsApp Web with Python to check the status of a user.
The proposed solution would allow us, after appropriate modifications, to also send automatic messages, but there is a problem: only numbers already in the address book can be used.
How can we do to send messages to unsaved numbers?
WhatsApp Web and Unsaved Numbers
Let’s start with a quick test to better understand the problem.
Let’s say we have WhatsApp Web enabled on our home computer and we want to send a message to a number in our contact list.
The first thing that comes to mind is to open a new chat:
and enter the phone number or contact name in the relevant search bar:
If the contact is actually present in the address book, it will appear under the contact list and you can select it to send a message.
Now, let’s rewind the tape of the operations we just did and imagine that we want to do the same thing but entering a phone number of an unsaved contact. The result is that no item will appear in the contact list and therefore it will not be possible to send any message.
The Solution
Don’t worry, there is always (or almost) a solution. In this regard, WhatsApp exposes APIs that allow you to get around the problem.
First we’ll try to use this method manually, then we’ll move on to write a script in Python to automate the procedure.
So, going back to our previous example, let’s admit that we want to send a message to the number +39 327 1234567.
Open a window in your browser and type in the following URL:
As you will see, entering a valid phone number will open the WhatsApp Web dashboard with the chat ready to send the message to the specified number.
The basic elements of this URL are:
- send: command that allows us to tell the WhatsApp Web service that we want to send a message;
- phone: attribute that allows us to specify a phone number, not necessarily present in the address book;
- text: the text of the message we want to send;
- app_absent: attribute to speed up the process of loading the web application. If set equal to 1, it loads the WhatsApp Web dashboard directly.
Let’s automate with Python
As we’ve already discussed in a few articles, Selenium is a platform for testing web applications and lends itself well to automate operations that involve the use of a browser.
Let’s write a simple script that allows us to send a message to a phone number not present in our address book.
Keep in mind that I’m assuming that you have a valid Python installation and that you have also installed the Selenium library and the webdriver related to the browser of your interest.
If you need help, refer to this article, particularly the paragraphs Selenium for Python, Installing WebDriver, and Hello World!.
To begin with, we import the necessary libraries and, we go to generate a driver to interact with the browser.
We build the URL with the data related to the message to be sent and then we ask Selenium to load the web page.
Once the loading is finished, we simulate a keyboard send command.
Conclusions
Today we discussed how to be able to send a message to a number not in the address book with WhatsApp Web, either manually or automatically. Specifically, in this second declination, we used Python and its web application testing library, Selenium. Thanks to this framework, it is possible to develop complex applications that automate the operations done through the browser.
Hi, many thanks! This is what I have been looking for.
BTW, do you have the workaround for sending GIF files from WhatsApp web?
Hi Kelvin,
thanks for you comment!
Unfortunately I haven’t tryed to find a solution yet, but stay tuned!
Have a nice continuation,
Macheronte
Hi ,I wants to send message to multiple numbers ,what should I do to solve this problem ?
Why does it give me so much time to open WhatsApp?
In 82898 Seconds WhatsApp will open and after
thanks