Python Pywhatkit – Send WhatsApp Messages Using Python

Python Send Automatic Message On Whatsapp

This post will introduce you to the python pywhatkit library and how to use it to send WhatsApp messages automatically using a few lines of Python code.

Python Pywhatkit

Pywhatkit is one of the popular python libraries for sending messages automatically to someone’s WhatsApp mobile number.

It uses the WhatsApp web to send these messages.

Pywhatkit is a python package for WhatsApp Messenger written in Python 3.4+. It’s simple, elegant, and 100% pythonic.

Features of pywhatkit library

  • Sending messages to contact/people on Whatsapp automatically
  • Automatically send message to group
  • Playing a YouTube Video or shorts
  • Also used to convert text(string) to handwriting
  • Sending Mails with HTML Code

Using Pywhatkit to Send WhatsApp messages using Python

Let’s now get into the steps to use the pywhatkit and send our first WhatsApp message using Python.

1. Installing the library

Since in Python3, pywhatkit is not pre-installed, it can be installed using the pip command:

pip install pywhatkit

2. Sending WhatsApp Message:

Using pywhatkit, Whatsapp messages can be automatically sent to any number on Whatsapp.

Note: You have to be logged in to Whatsapp in your browser, which means you’ll need to set up your WhatsApp Web account in your default browser.

The automatic WhatsApp is sent using sendmsg() method of the pywhatkit library. It has several features which are listed down in the form of examples that show how to send messages or images either to an individual or in a group.

Syntax: pywhatkit.sendmsg(“receiver’s mobile number”, “message to be sent”, hours, minutes)

Parameters of the method –

  • Receiver’s mobile number: Should be in string format and country code should be necessarily included, written before the mobile number.
  • Message to be sent: In string format.
  • Hours: the method follows a 24 hrs time format.
  • Minutes: It should be between 00-59.

3. Code to send message

import pywhatkit as pwk

# using Exception Handling to avoid unexpected errors
try:
     # sending message in Whatsapp in India so using Indian dial code (+91)
     pwk.sendwhatmsg("+91XXXXXX5980", "Hi, how are you?", 20, 34)

     print("Message Sent!") #Prints success message in console

     # error message
except: 
     print("Error in sending the message")

This program will send a message to the receiver(passed phone no.) at the specified time which is 8:34 pm and the message will be – “Hi, how are you?”

Note: The method by default opens the browser 15 seconds before the specified time to make up for the time of loading the WhatsApp Web site on your default browser.

Some more features of the library to send messages in WhatsApp

Closing the tab (WhatsApp Web tab) after sending the message.
pwk.sendwhatmsg("+91XXXXXX5980", "Hi", 18, 15, True, 5)

Here we are passing 5 seconds as the time to close the tab, also True represents the bool value of whether the tab needs to be closed or not, if it’s true it will close, or else if it’s false, it will not close

Sending an image to a Group along with the Caption as Hi

Here Media/image.png refers to the image to be sent

pwk.sendwhats_image("Group_Name", "Media/image.png", "Hi")

pwk.sendwhats_image("Name", "Media/images.png")
Sending a message to a Group:
pwk.sendwhatmsg_to_group("Group_Name", "Hey Guys! How's everybody?", 11, 0)

# it is similar to sending a message to a single person but here we are sending the message in a group
Instantly sending a message in Group

We use this to send messages instantly in a group as if we write 0 hours, 0 minutes then it will send the message at 12:00 AM

pwk.sendwhatmsg_to_group_instantly("Group_Name", "Hey Guys Again!")

Common Unexpected Errors

Some of the common errors which you may encounter and their solutions:

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

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

  • “raise Warning(“INTERNET IS SLOW, extraction of information might take longer time”)”
    “Warning: INTERNET IS SLOW, extraction of information might take longer time”

Solution:  Make sure you have a strong internet connection

Conclusion

That’s It for the tutorial! Hope you have learned well about pywhatkit and how to send WhatsApp messages automatically using the Pywhatkit library and are ready to implement it in your code. Stay tuned for more such tutorials on python.