You are on page 1of 2

import time

from selenium import webdriver


from selenium.webdriver.common.keys import Keys

def send_ultimate_messages(contact_name: str, messages: list, interval: int):


"""
Sends a series of ultimate messages to a single contact on WhatsApp.

This function uses the Selenium library to automate the process of sending
messages
on WhatsApp Web. It opens a Chrome browser, navigates to WhatsApp Web, searches
for
the specified contact, and sends the messages with the specified interval
between them.

Parameters:
- contact_name (str): The name of the contact to send the messages to.
- messages (list): A list of strings representing the messages to be sent.
- interval (int): The time interval (in seconds) between sending each message.

Raises:
- ValueError: If the 'messages' parameter is empty or if the 'interval'
parameter is less than or equal to zero.

Returns:
None
"""

# Validating the input parameters


if not messages:
raise ValueError("The 'messages' parameter cannot be empty.")
if interval <= 0:
raise ValueError("The 'interval' parameter must be greater than zero.")

# Initializing the Chrome WebDriver


driver = webdriver.Chrome()

try:
# Opening WhatsApp Web
driver.get("https://web.whatsapp.com")

# Waiting for the user to scan the QR code and log in


input("Press Enter after scanning the QR code and logging in to WhatsApp
Web...")

# Searching for the contact


search_box = driver.find_element_by_xpath("//div[@contenteditable='true']")
search_box.send_keys(contact_name)
time.sleep(2) # Wait for the search results to load
search_box.send_keys(Keys.ENTER)

# Sending the messages


for message in messages:
# Typing the message in the input field
input_box =
driver.find_element_by_xpath("//div[@contenteditable='true'][@data-tab='1']")
input_box.send_keys(message)

# Sending the message by pressing Enter


input_box.send_keys(Keys.ENTER)

# Waiting for the specified interval before sending the next message
time.sleep(interval)

finally:
# Closing the browser window
driver.quit()

# Example usage of the send_ultimate_messages function:

# Sending three messages to the contact named "John Doe" with a 2-second interval
between each message
contact = "John Doe"
messages = ["Hello!", "How are you?", "This is an ultimate message!"]
interval = 2

try:
send_ultimate_messages(contact, messages, interval)
print("Messages sent successfully!")
except ValueError as e:
print(f"Error while sending messages: {e}")

You might also like