How to Automate WhatsApp Messages
Tags: python, beginners, tutorial, programming
Published: 2021-10-16 11:56:53


WhatsApp is without a doubt the most popular smartphone application on our devices. With restrictions in place all around the world, it became one of our primary means of communication.

Are you the type of person who prefers not to be distracted from your work by the thought of wishing someone a happy birthday or reminding someone of anything at a specific time? If that's the case, this automation is for you!

You can automate this process by using python by writing just 2-3 lines of code. 

## Prerequisites 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wy0swgn2rm16oinpr5gm.png)
 
- Python3 
- Chrome browser*
- You must be logged into [WhatsApp web](https://web.whatsapp.com/) (does not matter if it is closed)

**If you do not have chrome browser, then you can follow the following steps:*

*1.  Download and extract the current stable release of chrome driver from  [https://chromedriver.chromium.org/](https://chromedriver.chromium.org/)*
    
*2.  Open the downloaded file and search for an application named chrome drive, copy its path, for windows, it should look like this - C:/Users/.../chromedriver.exe.*
    
*3.  Then call  `pywhatkit.add_driver_path(path)`  and pass the copied path as an argument, if the path is valid, a black window along with chrome will open and close.*
    
*4.  Now call  `pywhatkit.load_QRcode()`  function and scan the QR code.*
## Overview
![Overview of the process](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7ai55113r8y1257l4om.png)
To automate the transmission of messages, we'll write a Python script. Whatsapp web would open at the appointed time and deliver the message to the specified contact number.
## Install PyWhatkit
- Create a new python file, name it `wa_automation.py` or anything of your choice. 
- We would use a python library [PyWhatKit](https://pypi.org/project/pywhatkit/), which comes with various whatsapp features like send text messages, images, gifs to a personal contact or even a group chat. 
{% github https://github.com/Ankit404butfound/PyWhatKit %}
- This library takes care of everything behind the scenes, so all we have to do is write a few lines of Python code describing it what message to send and when to send it, along with some other parameters.

**Installation**
Install the library in your Python environment
```bash
pip3 install pywhatkit
```
*If you face errors:*
```bash
python -m pip install - upgrade pip

python -m pip install –upgrade Pillow 
```
Import the package
```py
# wa_automation.py
import pywhatkit
```
## Write Code

After we have installed the package, let us write our python code for automation.

We will use the function `sendwhatmsg()` to send our message to the contact.
The function asks for the following parameters: 
```py
sendwhatmsg(phone_no: str,
			 message: str,
			 time_hour: int,
			 time_min: int,
			 wait_time: int = 20,
			 tab_close: bool = False,
			 close_time: int = 3)

```
Mandatory Parameters: 
-   **phone_no**: 
    -   Enter phone number as a string
    - It is mandatory to add the country code, otherwise you would get error
    - Example: "+911234567891"
    
- **message**: 
  * Message to be sent as a string.
  * Ex: "One hour to go for the deadline"
		 
- **time_hour**: 
  * The hour when you want to send message
  * Please note that the time should be given in 24 hour format.
- **time_min**: 
  * The minutes when you want to send message.
```py
# wa_automation.py
import pywhatkit
pywhatkit.sendwhatmsg("+911234567891","Reminder to go to market" ,17, 00)
```

#### Customizations:
```py
# wa_automation.py
import pywhatkit
pywhatkit.sendwhatmsg("+911234567891","Reminder to go to market" ,17, 00, 20, True, 10)
```
- **wait_time:** After the appointed time, this is the time when your messages will be sent. It is set to 20 seconds by default, but you can alter it to suit your needs.
- **tab_close**: Specifies that whether the previous tab should be closed or not after sending the message. It will remain open after sending the message by default. You can change it by marking it `True`. 
- **close_time**: You can control the time after which the tab should be closed **after delivering the message** by specifying the `close_time` parameter **in seconds**. 
![full overview](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cd1r20k9k3jwiai64gwl.png)
 
 
## Other features

### Send messages to groups
- Using the `sendwhatmsg_to_group()` method we can send messages to group chats.
- Everything else is the same as the earlier case including the customizations, the only difference is that instead of `phone_num` there is `group_id`.
- You can identify the group_id of your group through the invite link. Highlighted text is your group id.

![WhatsApp Group ID. https://holithemes.com/whatsapp-chat/fin… | by venkat |  Click to Chat | Medium](https://miro.medium.com/max/413/1*Vq_3IAcaSeAgcky9ZEwRdg.png)

Example:
```py
pywhatkit.sendwhatmsg_to_group("AB123CDEFGHijklmn", "One hour to the deadline", 23, 00, 10, True, 10)
```

### Send Instant Messages
Here only difference is that there is no `time_hour` or `time_min` to be scheduled. 
```py
sendwhatmsg_instantly(phone_no: str,
					 message: str,
					 wait_time: int = 20,
					 tab_close: bool = False,
					 close_time: int = 3)
```
Example:
```py
pywhatkit.sendwhatmsg_instantly("+911234567891", "Can you call me? It is urgent!", 15, True, 4)
```
### Send Images/gifs
```py
sendwhats_image(phone_no: str,
					img_path: str,
					caption: str = " ",
					wait_time: int = 15,
					tab_close: bool = False,
					close_time: int = 3) -> None
```
Example:
```py
pywhatkit.sendwhats_image("+911234567891", "C:\\Image.png", "Question-1", 10, True, 5)
```
### Send multiple messages
The feature would be launched soon: 
{% github https://github.com/Ankit404butfound/PyWhatKit/issues/126 %}
### Possible pitfalls:
- Make sure your WhatsApp web is connected properly and you have a stable internet connection.
- When running the code, you should try to give at least 4-5 minutes from the present time; otherwise, if you set the time 1-2 minutes prior to the current time, the module may throw an error.
- Do not add 0 as a prefix when specifying time. 
- Windows users can send Images (all formats) and GIF's. For Linux based distributions, only `JPEG` and `PNG` are supported. For MacOS users, only `JPEG` is supported currently.

## Limitations
- This would only work when you are working on the machine, connected to a stable connection.
- So for example, you want to send your parents good morning every day at 7:00 AM while sleeping, this won't work. 

## Use cases
- If you are busy doing your work and you have to remind someone, or even schedule a text for meetings, announcements then this would do your work.
- Make aware your study group about the project deadline submission, wish someone a happy birthday on time while not being bothered about it doing work.  

## Alternative
If you want to send messages while being offline and away from your machine, then there is an alternative method for this using *Twilio, python and Heroku*. It is not that convenient and works on the basis of a hack, but it is for free and it works. 
Check out these resources to implement it:
{% youtube pQeFxdT3FGY %}
{% medium https://medium.com/programminghero/how-to-automate-whatsapp-with-15-lines-of-python-code-fa2d3565324d %}