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:
- Use the
sendwhatmsg() function to send a WhatsApp message:
pywhatkit.sendwhatmsg("+91xxxxxxxxxx", "Hello, this is an automated message sent using Python!", 14, 30)
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
from selenium import webdriver
- Create a new instance of the Chrome browser using the ChromeDriver executable:
driver = webdriver.Chrome("path/to/chromedriver")
- Navigate to the WhatsApp web page:
- 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!")
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.