Javatpoint Logo
Javatpoint Logo

Automate a WhatsApp message using Python

We can update everyone's birthdays on Google calendar and turn the notifications on. But what if there is an application that can send birthday wishes without us having to interfere at all? It is all about automation, and let us use Python's vast libraries and options to achieve this.

This tutorial explains how to automate WhatsApp messages in individual and group chats using Python's 'pywhatkit library'.

Library:

Python has a lot of libraries. To get the functionality we want, we can have different options/ libraries to use, out of which we can choose one based on certain needs of the project we develop. To get the WhatsApp Message Automation feature, these are some of the options:

  1. Browser Automation (selenium library)
  2. Pywhatkit library
  3. Pyautogui library

In this tutorial, we'll try automating messages using the Pywhatkit library.

Using the Pywhatkit library:

Pywhatkit is an inbuilt Python library. It provides features like:

  1. Sending an automated Whatsapp message at a certain time.
  2. Play a Youtube video
  3. Do a Google search or gather info about a particular topic.

Installing the library:

It is easy to use the library, but if you are working with other projects simultaneously, it is preferred to create a new virtual environment as the library has a lot of dependencies.

Creating a new virtual environment:

Generally, developers use separate virtual environments when working on different projects simultaneously to keep the projects isolated without dependency collisions.

Follow these steps to create a new Python virtual environment for your whatsapp project:

1. Open command prompt (cmd) from the start menu.

2. Use the command:

in order to change the directory to where you want the project file to be located.

E.g., cd Desktop

3. Now, type the command:

Note: If you have the latest version of Python, use py instead of python3

Automate a WhatsApp message using Python

4. The command creates a separate virtual environment with the name specified (Whatsapp here).

5. Use the command:

For mac:

For Windows:

to use/ activate the created virtual environment:

Automate a WhatsApp message using Python

Installing the library:

Now that a separate virtual environment is set up for the project, it is time to install the library. Just give the command:

It installs all the library modules, so it takes a bit of time. To check all the dependencies installed in the library: use the command:


Automate a WhatsApp message using Python

WhatsApp web:

Either use WhatsApp web or WhatsApp beta on your laptop/ computer to open whatsapp on your system.

If you don't know about whatsapp web:

  1. Open your browser and search for whatsapp web
  2. Open the official link.
  3. Take your mobile, open whatsapp, click on the three dots on the top-right corner and select linked devices.
  4. Select "Link a device" and scan the QR on your pc/ laptop with the scanner that opens up in your phone.
  5. Your whatsapp will be accessible in your system now. This is called the whatsapp web feature.

We'll now learn how to send a whatsapp message to individual contact and groups.

Individual contacts:

First, import the installed library using the import statement:

We need to use the .sendwhatmsg() method to get this functionality:

Syntax:

Here is an example:

Automate a WhatsApp message using Python

As you can observe, a message shows the number of seconds within which Python opens Whatsapp and sends the message to the given contact number.

Here are some important points:

  1. The function pywhatkit.sendwhatmsg() takes the time input in 24 hours format. Eg: 19: 35 -> 7: 35PM
  2. The time we want the message to be sent must be atleast 2 to 3 minutes from the current time. If you set the time to less than that, the module raises an error as it takes at least 2 seconds to open whatsapp web.
  3. Upgrade the pip version before installing the library if you already haven't.
  4. Avoid leading zeroes in when specifying minutes. Eg: For 6: 02 -> 18: 02 raises a syntax error. Either use 18:2 or use another number other than 0.
  5. Don't forget to include the country code of the phone number. An exception will be raised if not mentioned.

Group:

We learned how to send a message to an individual contact. Sending messages to a group is no different than sending to a single contact. The only thing here is that to send a message to a group, we need the group Id, which acts like the phone number.

  1. Open the group you want to send a message to on whatsapp.
  2. Click on the 'Invite via link' option and copy the link.
  3. The last/ suffix part in the link is the group Id we want.

For example, https://chat.whatsapp.com/.......................

The dotted line represents the Group Id.

The method we need to use here is: pywhatkit.sendwhatmsg_to_group()

Syntax:

Here is an example:

Automate a WhatsApp message using Python

Other parameters we can use in the methods:

  1. wait_time (int): The time the method waits before sending the given message to the contact/ group.
  2. tab_close (bool): When made true, after the message is sent, the tab will be closed after the specified number of seconds. Default: False
  3. close_time (int): The number of seconds we specify for the tab to be closed after the message is sent.

Two more methods are capable of sending messages instantly in the library:

Automate a WhatsApp message using Python





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA