Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added whatsapp_automation.tar.gz
Binary file not shown.
1 change: 1 addition & 0 deletions whatsapp_automation/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.venv/\n__pycache__/\n*.log
53 changes: 53 additions & 0 deletions whatsapp_automation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# WhatsApp Automation

This script automates sending personalized WhatsApp messages from an Excel file.

## Disclaimer

**Please be aware that automating WhatsApp messages may violate their Terms of Service and could result in your account being banned. Use this script at your own risk.**

## Setup

1. **Install Python:** If you don't have Python installed, download and install it from [python.org](https://python.org).

2. **Install Google Chrome:** This script uses the Chrome browser, so you'll need to have it installed.

3. **Install Dependencies:**
* Open a terminal or command prompt.
* Navigate to the project directory (`whatsapp_automation`).
* Run the following command to install the required Python libraries:
```bash
pip install -r requirements.txt
```
* The script will automatically download and manage the correct version of ChromeDriver for you.

## How to Run

1. **Prepare your Excel file:**
* Create an Excel file with the following columns:
* `First name`
* `Last name`
* `Telephone number` (including the country code, e.g., +1234567890)
* `Message` (you can use `{first_name}` and `{last_name}` as placeholders for personalization)

2. **Run the script:**
* Open a terminal or command prompt.
* Navigate to the project directory.
* Run the following command:
```bash
python main.py
```

3. **Log in to WhatsApp:**
* The script will open a new Chrome window with WhatsApp Web.
* Use your phone to scan the QR code and log in.

4. **Select your Excel file:**
* A file dialog will open. Select the Excel file you prepared.

5. **Let it run:**
* The script will start sending the messages one by one.
* Do not close the Chrome window until the script has finished.

6. **Check the log:**
* Once the script is done, a `message_log.csv` file will be created in the project directory, showing the status of each message.
133 changes: 133 additions & 0 deletions whatsapp_automation/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import pandas as pd
import tkinter as tk
from tkinter import filedialog
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
import time
import random
import urllib.parse

def get_excel_file():
"""Opens a file dialog to select an Excel file."""
root = tk.Tk()
root.withdraw() # Hide the main window
file_path = filedialog.askopenfilename(
title="Select the Excel file with contacts",
filetypes=[("Excel Files", "*.xlsx *.xls")]
)
return file_path

def read_contacts(file_path):
"""Reads contacts from the specified Excel file."""
if not file_path:
print("No file selected. Exiting.")
return None
try:
df = pd.read_excel(file_path)
print("Successfully loaded contacts:")
print(df)
return df
except FileNotFoundError:
print(f"Error: The file '{file_path}' was not found.")
return None
except Exception as e:
print(f"An error occurred while reading the Excel file: {e}")
return None

def initialize_driver():
"""Initializes the Chrome WebDriver using webdriver-manager."""
try:
print("Setting up ChromeDriver...")
service = ChromeService(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service)
driver.get("https://web.whatsapp.com")
print("Please scan the QR code to log in to WhatsApp Web.")
return driver
except Exception as e:
print(f"Error initializing WebDriver: {e}")
print("Please make sure you have Google Chrome installed.")
return None

def send_whatsapp_message(driver, phone_number, message):
"""Sends a WhatsApp message to a given phone number."""
try:
# Wait for the user to log in by looking for the search bar
WebDriverWait(driver, 60).until(
EC.presence_of_element_located((By.XPATH, '//div[@data-testid="chat-list-search"]')))

# Format the phone number
if not str(phone_number).startswith('+'):
phone_number = '+' + str(phone_number)

# URL-encode the message
encoded_message = urllib.parse.quote(message)

# Create the URL to open a chat
url = f"https://web.whatsapp.com/send?phone={phone_number}&text={encoded_message}"
driver.get(url)

# Wait for the message box to be ready
message_box = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.XPATH, '//div[@role="textbox"]')))

# Click the send button
send_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.XPATH, '//button[@data-testid="send"]')))
send_button.click()

time.sleep(random.uniform(1, 3)) # Random delay
return True
except TimeoutException:
print(f"Timed out waiting for element for {phone_number}.")
return False
except NoSuchElementException:
print(f"Could not find an element for {phone_number}.")
return False
except Exception as e:
print(f"An error occurred while sending a message to {phone_number}: {e}")
return False

def main():
"""Main function to run the application."""
excel_file = get_excel_file()
contacts_df = read_contacts(excel_file)
if contacts_df is not None:
driver = initialize_driver()
if driver:
log = []
for index, row in contacts_df.iterrows():
first_name = row.get('First name', '')
last_name = row.get('Last name', '')
phone_number = row.get('Telephone number')
message = row.get('Message', '')

if pd.isna(phone_number) or not message:
print(f"Skipping row {index + 2}: Missing phone number or message.")
log.append({'phone_number': phone_number, 'status': 'Skipped - Missing data'})
continue

# Personalize the message
personalized_message = message.replace('{first_name}', str(first_name)).replace('{last_name}', str(last_name))

print(f"Sending message to {first_name} {last_name} ({phone_number})...")
success = send_whatsapp_message(driver, phone_number, personalized_message)

if success:
print("Message sent successfully.")
log.append({'phone_number': phone_number, 'status': 'Success'})
else:
print("Failed to send message.")
log.append({'phone_number': phone_number, 'status': 'Failed'})

driver.quit()
log_df = pd.DataFrame(log)
log_df.to_csv("message_log.csv", index=False)
print("\nFinished sending messages. A log file 'message_log.csv' has been created.")

if __name__ == "__main__":
main()
4 changes: 4 additions & 0 deletions whatsapp_automation/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pandas
openpyxl
selenium
webdriver-manager