Automate WhatsApp Messages With Python using Pywhatkit module


To automate WhatsApp messages with Python, we can use the Pywhatkit module. Pywhatkit is a Python library for sending WhatsApp messages at a certain time, automating WhatsApp web, and much more.

Here are the steps to automate WhatsApp messages with Python using Pywhatkit module:

  • Install Pywhatkit module using pip command:
pip install pywhatkit
  • Import the Pywhatkit module in your Python script:
import pywhatkit
  • Use the sendwhatmsg() function to send a WhatsApp message:
pywhatkit.sendwhatmsg("+91xxxxxxxxxx", "Hello, this is an automated message sent using Python!", 14, 30)

The sendwhatmsg() function takes four arguments: - phone_no: The phone number of the recipient, including the country code (e.g. +91 for India). - message: The message you want to send. - time_hour: The hour at which you want to send the message (in 24-hour format). - time_min: The minute at which you want to send the message.

In the above example, the message will be sent to the phone number +91xxxxxxxxxx at 2:30 PM.

  • Run the Python script and wait for the message to be sent.

Note: To use this method, you need to have WhatsApp web open on your computer and logged in to your account.

Another method to automate WhatsApp messages with Python is by using the Selenium module. Here are the steps:

  • Install Selenium module using pip command:
pip install selenium
  • Download the ChromeDriver executable from the official website and place it in a directory on your computer.

  • Import the necessary modules in your Python script:

from selenium import webdriver
import time
  • Create a new instance of the Chrome browser using the ChromeDriver executable:
driver = webdriver.Chrome("path/to/chromedriver")
  • Navigate to the WhatsApp web page:
driver.get("https://web.whatsapp.com/")
  • Wait for the user to scan the QR code and log in to their account:
input("Scan the QR code and press Enter to continue...")
  • Find the chat box and send the message:
chat_box = driver.find_element_by_xpath('//div[@class="_3uMse"][@contenteditable="true"][@data-tab="1"]')
chat_box.send_keys("Hello, this is an automated message sent using Python!")
chat_box.send_keys(Keys.ENTER)
  • Close the browser:
driver.quit()

In the above example, the message will be sent to the last chat that was opened on WhatsApp web.

Note: This method requires you to have the Chrome browser installed on your computer and logged in to your WhatsApp account.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.