DEV Community

Cover image for How to Automate WhatsApp Messages
vibalijoshi
vibalijoshi

Posted on • Edited on

How to Automate WhatsApp Messages

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

  • Python3
  • Chrome browser*
  • You must be logged into WhatsApp web (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/

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
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, which comes with various whatsapp features like send text messages, images, gifs to a personal contact or even a group chat.

    GitHub logo Ankit404butfound / PyWhatKit

    Send WhatsApp message at certain time and many other things.

    image

    image image image image image image image image

    PyWhatKit is a Python library with various helpful features. It's easy-to-use and does not require you to do any additional setup. Currently, it has about 200k+ downloads and counting. New updates are released frequently with new features and bug fixes.

    Links

    • Have some query or suggestions or want to become a beta tester, join our discord server - Discord invite link

    • Help us test an upcoming feature - here

    • Documentation - Wiki

    Contributing

    Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. For more detailed information see this.

    License

    MIT. For more information see this



  • 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

pip3 install pywhatkit
Enter fullscreen mode Exit fullscreen mode

If you face errors:

python -m pip install - upgrade pip

python -m pip install –upgrade Pillow 
Enter fullscreen mode Exit fullscreen mode

Import the package

# wa_automation.py
import pywhatkit
Enter fullscreen mode Exit fullscreen mode

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:

sendwhatmsg(phone_no: str,
             message: str,
             time_hour: int,
             time_min: int,
             wait_time: int = 20,
             tab_close: bool = False,
             close_time: int = 3)

Enter fullscreen mode Exit fullscreen mode

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.
# wa_automation.py
import pywhatkit
pywhatkit.sendwhatmsg("+911234567891","Reminder to go to market" ,17, 00)
Enter fullscreen mode Exit fullscreen mode

Customizations:

# wa_automation.py
import pywhatkit
pywhatkit.sendwhatmsg("+911234567891","Reminder to go to market" ,17, 00, 20, True, 10)
Enter fullscreen mode Exit fullscreen mode
  • 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

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

Example:

pywhatkit.sendwhatmsg_to_group("AB123CDEFGHijklmn", "One hour to the deadline", 23, 00, 10, True, 10)
Enter fullscreen mode Exit fullscreen mode

Send Instant Messages

Here only difference is that there is no time_hour or time_min to be scheduled.

sendwhatmsg_instantly(phone_no: str,
                     message: str,
                     wait_time: int = 20,
                     tab_close: bool = False,
                     close_time: int = 3)
Enter fullscreen mode Exit fullscreen mode

Example:

pywhatkit.sendwhatmsg_instantly("+911234567891", "Can you call me? It is urgent!", 15, True, 4)
Enter fullscreen mode Exit fullscreen mode

Send Images/gifs

sendwhats_image(phone_no: str,
                    img_path: str,
                    caption: str = " ",
                    wait_time: int = 15,
                    tab_close: bool = False,
                    close_time: int = 3) -> None
Enter fullscreen mode Exit fullscreen mode

Example:

pywhatkit.sendwhats_image("+911234567891", "C:\\Image.png", "Question-1", 10, True, 5)
Enter fullscreen mode Exit fullscreen mode

Send multiple messages

The feature would be launched soon:

Sending multiple messages to multiple numbers #126

I think you are already working on sending multiple messages to the same person (one after another), if not, this feature is needed. Also I would like to send a message (or multiple) to multiple numbers.

Thanks

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:

Top comments (10)

Collapse
 
ifierygod profile image
Goran Kortjie

Thank you for this now I can automate talking to my family 👪 ❤

Collapse
 
vibalijoshi profile image
vibalijoshi

Great 😎😎

Collapse
 
matsumaki profile image
matsumaki

hi, i'm having a problem with the whatsapp here's the recording that shows what kind of problem that i'm having (youtu.be/c69MV2rnLiQ) i've tried changing the browser and changing the wait time but i still stuck at that problem,i hope that you have an answer to my problem, Thanks!

Collapse
 
jobobnenner profile image
JoBobNenner

I have two numbers blocked from Whatsapp while playing with WhatsApp-python librarys... Be carefull!

Collapse
 
vibalijoshi profile image
vibalijoshi

Wow you did sacrifice a lot in the name of science 😂
To people using this, make sure you do not violate WhatsApp terms and conditions.
faq.whatsapp.com/general/account-a...

Collapse
 
parijatsutradh2 profile image
Parijat Sutradhar

I am getting this error message, please help me with it

C:\Users\parij\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/parij/PycharmProjects/pythonProject/wa_automation.py
Traceback (most recent call last):
File "C:\Users\parij\PycharmProjects\pythonProject\wa_automation.py", line 2, in
import pywhatkit
File "C:\Users\parij\PycharmProjects\pythonProject\venv\lib\site-packages\pywhatkit_init_.py", line 29, in
from pywhatkit.remotekit import start_server
File "C:\Users\parij\PycharmProjects\pythonProject\venv\lib\site-packages\pywhatkit\remotekit.py", line 6, in
from flask import Flask, request
ModuleNotFoundError: No module named 'flask'

Process finished with exit code 1

Collapse
 
aboss123 profile image
Ashish Bailkeri • Edited

The visuals are really good on this, way to go! Instructions are also clear, and help guide the reader through your process.

Collapse
 
vibalijoshi profile image
vibalijoshi

Thank you so much Ashish, glad it was easy to understand!

Collapse
 
santosh profile image
Santosh Kumar • Edited

Yeah, sounds something related to Selenium? chromedriver and stuff.

But does this complies with WhatsApp's terms of service?

Collapse
 
saifprogrammer profile image
saif-programmer

Is it possible to deploy this app
And how?

Some comments may only be visible to logged-in visitors. Sign in to view all comments.