How to Send Automatic whatsApp Messages in Python?

To Send automatic Whatsapp Messages in python we make use of pywhatkit library.

pywhatkit module in Python:

Pywhatkit is a popular Python library for automatically delivering messages to someone’s WhatsApp mobile number.

It sends the messages using WhatsApp web.

Pywhatkit is a Python 3.4+ package for WhatsApp Messenger. It’s simple, elegant, and completely pythonic.

Features

  • Automatically sending messages to contacts/people on Whatsapp
  • Send a message to a group automatically
  • Playing a YouTube video or a series of shorts
  • Conversion from Text(string) to handwriting is also possible.
  • Using HTML Code to Send Emails

Sending Automatic whatsApp Messages in Python

Before we work with this pywhatkit library we should first install it.

1)Installation:

pip install pywhatkit

2)Send a WhatsApp Message:

Whatsapp messages can be automatically sent to any Whatsapp number using pywhatkit.

To send whatsapp messages with pywhatkit, we require a browser and the user must be logged into the website web.whatsapp.com in that browser.

You must be logged in to Whatsapp in your browser, which means you must configure your WhatsApp Web account on your regular browser.

sendmsg() method:

The sendmsg() method of the pywhatkit module is used to send the automatic WhatsApp message. It includes various features that are given in the form of examples that explain how to send messages or images to a person or to a group.

Syntax:

pywhatkit.sendwhatmsg(“Receiver_mobile_number”, ”Message”, Hours, Minutes)

Parameters

Receiver_mobile_number: This is the number to which you want to send the message. The number should be in string format, with the country code appearing before the mobile number.

Message: The message that must be sent to that receiver contact.

Hours: It must be in the 24-hour time format. The message will be sent at this time.

Minutes: It must be between 00 to 59.

3)Code

Approach:

  • Import pywhatkit module using the import keyword.
  • Using the try-except blocks to handle the errors
  • Writing the code inside the try block
  • Pass some random receivers mobile number, message that has to be sent, hours, minutes as arguments to the sendwhatmsg() function of the pywhatkit() module.
  • Here the message is sent to the receiver number at the given time.
  • Print some random text for acknowledgment
  • Handling the error if occurred using the except block
  • Print some random text for acknowledgment if there is an error.
  • The Exit of the Program.

Below is the implementation:

# Import pywhatkit module using the import keyword.
import pywhatkit
# Using the try-except blocks to handle the errors
try:
    # Writing the code inside the try block
    # Pass some random receivers mobile number, message that has to be sent, hours, minutes as 
    # arguments to the sendwhatmsg() function of the pywhatkit() module.
    # Here the message is sent to the receiver number at the given time
    pywhatkit.sendwhatmsg("+91XXXXXXXXXX","Good morning btechgeeks", 15, 17)
    # Print some random text for acknowledgment
    print("The message sent successfully!!")
# Handling the error if occurred using the except block
except:
    # Print some random text for acknowledgment if there is an error.
    print("Sorry, there is an error while sending the message.")

Output:

sending whatsapp message using pywhatkit

This sendwhatmsg()method will send a message to the given receiver’s mobile number at the set time of 3:15 p.m.(24-hour time format), with the message “Good morning btechgeeks”

Note: By default, the method opens the browser 15 seconds before the given time to compensate for the time it takes to load the WhatsApp Web site on your usual browser.

  • We should keep the time at least 2-3 minutes after the current time, however if you keep it at 1-2 minutes after the current time, an error will appear.
  • Before executing the software, ensure that WhatsApp web is open in the browser. To log in, visit the WhatsApp link and scan the QR code provided.

Typical Unexpected Errors

Some of the most typical errors you may face, as well as their solutions:

1)“SyntaxError: leading zeros in decimal integer literals are not permitted; use an 0o prefix for octal integers”

Answer: Start a minute’s argument with any number other than 0.

2)”raise Warning(“INTERNET IS SLOW, information extraction may take longer”)”
“Warning: THE INTERNET IS SLOW; information extraction may take longer.”

Answer: Check that you have a strong internet connection.