Repository navigation
Expand file tree
/
Copy pathsendWhatsappWeb.py
More file actions
118 lines (93 loc) · 4.57 KB
/
Copy pathsendWhatsappWeb.py
File metadata and controls
118 lines (93 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import pandas as pd
# ---- Setup Firefox WebDriver with Profile ----
options = webdriver.FirefoxOptions()
options.set_preference("dom.webnotifications.enabled", False)
i = 0
# CSV file path
file_numbers = "/home/mohammed28/Downloads/Welcome Day Registeration - Sheet1.csv"
# Add your Firefox profile path
profile_path = "/home/mohammed28/.mozilla/firefox/o1v9f9ua.default-release"
options.add_argument("-profile")
options.add_argument(profile_path)
driver = webdriver.Firefox(options=options)
try:
# ---- Read phone numbers from CSV ----
print("📂 Reading CSV file...")
df = pd.read_csv(file_numbers)
# Extract phone numbers from the 'Phone Number' column
# Strip whitespace and convert to string
phone_numbers = df['Phone Number'].astype(str).str.strip().tolist()
# Add +20 prefix to each number
numbers = [f"+20{num}" for num in phone_numbers]
print(f"✅ Loaded {len(numbers)} phone numbers from CSV")
# ---- Open WhatsApp Web ----
driver.get("https://web.whatsapp.com")
# Wait for WhatsApp to load (should already be logged in)
print("⏳ Waiting for WhatsApp to load...")
time.sleep(8) # Give it time to load with your existing session
# ---- Messages to send ----
message1 = """السلام عليكم \u2764\uFE0F
ازيك ايه الاخبار
مبسوطين جدا بمشاركتك معانا في ال welcome Day
دلوقتي جه وقت الخطوة اللي بعدها :
ده لينك فورم معاد ال Interview
من فضلك املي الفورم واختار المعاد اللي يناسبك
الفورم دي للناس اللي سجلت يوم ال Welcome Day بس فياريت بلاش تتشارك أو تتبعت لحد تاني
ولو سجلت فيها خلاص متسجلش تاني في ال form اللي هتنزل علي Facebook
الديدلاين بتاعها هيكون بكره الساعة 4 العصر ان شاء الله
مستنيين نشوفك في ال interview\U0001F4AA\U0001F3FB
#RoboTech'26\u2764\uFE0F"""
message2 = """ملحوظة: واتساب بيظهر اللينك كـ Text ، بسبب أني مش متسجل عندك
انسخ اللينك او اعمل trust للرقم وهيشتغل عادي"""
message3 = """https://forms.gle/bp2dX9vJFbjq6bg46"""
# Store all messages in a list
messages = [message1, message2, message3]
# ---- Loop through each number ----
for number in numbers:
print(f"🔄 Processing {number}...")
driver.get(f"https://web.whatsapp.com/send?phone={number}")
try:
# Send each message separately
for idx, msg in enumerate(messages, 1):
# Wait for the message box to be present and visible
wait = WebDriverWait(driver, 20)
msg_box = wait.until(
EC.presence_of_element_located((By.XPATH, '//div[@contenteditable="true"][@data-tab="10"]'))
)
# Additional wait to ensure chat is fully loaded
time.sleep(2)
# Click the message box to focus it
msg_box.click()
time.sleep(0.5)
# Type character by character (more reliable)
for char in msg:
msg_box.send_keys(char)
time.sleep(0.02) # Small delay between characters
time.sleep(1)
# Send the message
msg_box.send_keys(Keys.ENTER)
# Wait between messages
time.sleep(2)
print(f" ✅ Message {idx}/3 sent to {number}")
i+=1
print(f"✅ All messages sent to {number}, count is: {i}")
except Exception as e:
print(f"❌ Failed to send to {number}: {e}")
# Try to capture screenshot for debugging
try:
driver.save_screenshot(f"error_{number.replace('+', '')}.png")
print(f"📸 Screenshot saved for debugging")
except:
pass
# Wait before next contact (avoid rate limiting and suspicion)
time.sleep(5)
finally:
print("\n🏁 Finished! Closing browser in 3 seconds...")
time.sleep(3)
driver.quit()