-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
645 lines (337 loc) · 14.1 KB
/
main.py
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
'''
AutoWhatsApp
Repo Owner :- https://github.com/prajwalmali/prajwalmali
ALL-IN-ONE link :- https://linktr.ee/prajwalmali
'''
# Features
'''
Automatically Sends Same Or Different Dynamic Messages Or Attachments Or Both To List Of Peoples Or Groups Or Spam
Detailed Documentation on GitHub
'''
# Driver Setup
'''
(You Need To Download Your Browsers Driver From Site Given Below) (*** IMPORTANT *** - Check Version Carefully)
(Link For Chrome Driver - https://chromedriver.chromium.org/downloads)
(Link For Firefox Driver - https://github.com/mozilla/geckodriver/releases)
(Link For Edge Driver - https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/)
(Link For Safari Driver - https://webkit.org/blog/6900/webdriver-support-in-safari-10/)
'''
# Requirements
'''
Install By Pip If Not Done Yet
Paste Below Commands In Command Prompt For Windows :-
(For Linux, Do I Need To Tell You ?)
pip install -r requirements.txt
OR
pip install selenium
pip install PyAutoIt
pip install pandas
pip install pyperclip
pip install openpyxl
pip install tqdm
pip install termcolor
(Time Is In-Built Module)
(OS Is In-Built Module)
'''
# Imports
# For Browser Automation
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
# For Coloured CLI
from tqdm import tqdm
from termcolor import colored,cprint
# For Sending Attachments
import autoit
# For Reading Excel
import pandas
# For Adding Buffer/Wait Time In Program
import time
# For Copying The Message
import pyperclip
# For Clearing CLI
import os
clear = lambda: os.system('cls')
space_count = 80 * ' '
# Program Logo
logo = ''' _ _ _ _
|_| _|_ _ \ /|_ _ _|_ _ |_||_)|_)
| ||_| |_(_) \/\/ | |(_| |__> | || |
'''
# Load Driver (Replace With Your Path)
chromepath = r"C:\\Program Files\\Google\\Chrome Driver\\chromedriver.exe"
options = webdriver.ChromeOptions()
# Save The Session Data (THIS IS NOT SAFE) (Need To Scan QR Code Only Once For The First Time) (Replace With Your Path)
options.add_argument("user-data-dir=C:\\Prajwal\\Python\\AutoWhatsApp\\User Data") # (For Security You Can Comment Out This Line)
while True:
count = 0
# Main Menu
cprint(logo,'green',attrs=['bold'])
cprint('\n{}*** Programmed By Prajwal Vijaykumar Mali ***'.format(space_count),'green',attrs=['bold'])
cprint('\n\t1. Press 1 If You Want To Send A Message','cyan', attrs=['bold'])
cprint('\n\t2. Press 2 If You Want To Send An Attachment','yellow', attrs=['bold'])
cprint('\n\t3. Press 3 If You Want To Send Both A Message And An Attachment','magenta', attrs=['bold'])
cprint('\n\t4. Press 4 If You Want To Exit','red', attrs=['bold'])
cprint('\n\t~Ultimate WhatsApp Automation : ','green', attrs=['bold'])
choice = int(input('\n\t'))
clear()
# For Sending Message
if choice == 1:
# To Open Automated Browser Window
driver = webdriver.Chrome(executable_path = chromepath, options = options)
# Wait For 1 Second
time.sleep(1)
# To Maximize Automated Browser Window
driver.maximize_window()
# Open WhatsApp URL In Chrome Browser
driver.get("https://web.whatsapp.com/")
wait = WebDriverWait(driver, 60)
# Read Data From Excel (Replace With Your Path)
excel_data = pandas.read_excel("C:\\Prajwal\\Python\\AutoWhatsApp\\demo.xlsx")
# Iterate Excel Rows From Start To End
for column in excel_data['Name'].tolist():
# Assign Customized Message
'''
1st Line Of Code Is To Send One Message Dynamically To List Of Contacts
2nd Line Of Code Is To Send Separate Dynamic Message To Respective Contact In The Name Column
*** IMPORTANT *** - Comment Out Anyone Of The Line
'''
message = excel_data['Message'][0]
# message = excel_data['Message'][count]
# Locate Search Box Through x_path
search_box = '//*[@id="side"]/div[1]/div/label/div/div[2]'
person_title = wait.until(lambda driver:driver.find_element_by_xpath(search_box))
# Clear Search Box If Any Contact Number Is Written In It
person_title.clear()
# Wait For 1 Second
time.sleep(1)
# Send Contact Number In Search Box
person_title.send_keys(Keys.HOME)
person_title.send_keys(str(excel_data['Contact'][count]))
count += 1
# Wait For 1 Second To Search Contact Number
time.sleep(1)
try:
'''
Load Error Message If Contact Number Is Unavailable/Wrong
(Currently Only Saved Contact Numbers Are Supported BUT,
You Can Use This Program To AUTOMATE Message For Those Unsaved Contacts
To Whom You Have Texted Atleast One Message Like HI Or Anything Else)
'''
element = driver.find_element_by_xpath('//*[@id="pane-side"]/div[1]/div/span')
except NoSuchElementException:
'''
Format The Message From Excel
(Make The Message Dynamic Or Specific To The Receiver By Adding His/Her/Others Name.
You Can Also Make It Specific In Your Own Way By Adding Other Data Instead Of Name
Such As Company Name, College Name, Location, Etc. MORE DATA MORE SPECIFIC)
'''
message = message.replace('{name}', column)
# Copy Message To Clipboard
pyperclip.copy(message)
person_title.send_keys(Keys.ENTER)
actions = ActionChains(driver)
# Paste Message (CTRL + V)
actions.key_down(Keys.CONTROL).send_keys('v')
actions.perform()
# Wait For 1 Second
time.sleep(1)
# Locate Send Button Through x_path
driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[3]/button').click()
# Wait For 1 Second
time.sleep(1)
# To Clear CLI
clear()
# Close Chrome Driver
driver.quit()
cprint('\n\tDo You Want To Run The Program Again ? (y/n) : ','red',attrs=['bold'])
again_choice = input('\n\t')
if (again_choice.lower() == 'y'):
clear()
continue
else:
exit()
# For Sending Attachment
elif choice == 2:
cprint(logo,'green',attrs=['bold'])
cprint('\n{}*** Programmed By Prajwal Vijaykumar Mali ***'.format(space_count),'green',attrs=['bold'])
cprint('\n\tEnter the Attachment File Path You Want To Send : ','cyan', attrs=['bold'])
cprint('\n\t~Ultimate WhatsApp Automation : ','green', attrs=['bold'])
# Provide Attachment Path
attachment_path = input('\n\t')
clear()
# To Open Automated Browser Window
driver = webdriver.Chrome(executable_path = chromepath, options = options)
# Wait For 1 Second
time.sleep(1)
# To Maximize Automated Browser Window
driver.maximize_window()
# Open WhatsApp URL In Chrome Browser
driver.get("https://web.whatsapp.com/")
wait = WebDriverWait(driver, 60)
# Read Data From Excel (Replace With Your Path)
excel_data = pandas.read_excel("C:\\Prajwal\\Python\\AutoWhatsApp\\demo.xlsx")
# Iterate Excel Rows From Start To End
for column in excel_data['Name'].tolist():
# Locate Search Box Through x_path
search_box = '//*[@id="side"]/div[1]/div/label/div/div[2]'
person_title = wait.until(lambda driver:driver.find_element_by_xpath(search_box))
# Clear Search Box If Any Contact Number Is Written In It
person_title.clear()
# Wait For 1 Second
time.sleep(1)
# Send Contact Number In Search Box
person_title.send_keys(Keys.HOME)
person_title.send_keys(str(excel_data['Contact'][count]))
count += 1
# Wait For 1 Second To Search Contact Number
time.sleep(1)
try:
# Load Error Message If Contact Number Is Unavailable/Wrong
element = driver.find_element_by_xpath('//*[@id="pane-side"]/div[1]/div/span')
except NoSuchElementException:
person_title.send_keys(Keys.ENTER)
actions = ActionChains(driver)
# Locate Clip Button Through x_path
driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[1]/div[2]/div/div/span').click()
# Wait For 1 Second
time.sleep(1)
# Locate Button To Send Attachment Through x_path
driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[1]/div[2]/div/span/div/div/ul/li[1]/button').click()
# Wait For 1 Second
time.sleep(1)
# To Send Attachment
autoit.control_focus("Open", "Edit1")
autoit.control_set_text("Open", "Edit1", attachment_path)
autoit.control_click("Open", "Button1")
# Wait For 1 Second
time.sleep(1)
# Locate Send Button Through x_path
driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[2]/span/div/span/div/div/div[2]/span/div/div').click()
# Wait For 5 Seconds To Deliver The Attachment
time.sleep(5)
# To Clear CLI
clear()
# Close Chrome Driver
driver.quit()
cprint('\n\tDo You Want To Run The Program Again ? (y/n) : ','red',attrs=['bold'])
again_choice = input('\n\t')
if (again_choice.lower() == 'y'):
clear()
continue
else:
exit()
# For Sending Both Message And Attachment
elif choice == 3:
cprint(logo,'green',attrs=['bold'])
cprint('\n{}*** Programmed By Prajwal Vijaykumar Mali ***'.format(space_count),'green',attrs=['bold'])
cprint('\n\tEnter the Attachment File Path You Want To Send : ','cyan', attrs=['bold'])
cprint('\n\t~Ultimate WhatsApp Automation : ','green', attrs=['bold'])
# Provide Attachment Path
attachment_path = input('\n\t')
clear()
# To Open Automated Browser Window
driver = webdriver.Chrome(executable_path = chromepath, options = options)
# Wait For 1 Second
time.sleep(1)
# To Maximize Automated Browser Window
driver.maximize_window()
# Open WhatsApp URL In Chrome Browser
driver.get("https://web.whatsapp.com/")
wait = WebDriverWait(driver, 60)
# Read Data From Excel (Replace With Your Path)
excel_data = pandas.read_excel("C:\\Prajwal\\Python\\AutoWhatsApp\\demo.xlsx")
# Iterate Excel Rows From Start To End
for column in excel_data['Name'].tolist():
# Assign Customized Message
'''
1st Line Of Code Is To Send One Message Dynamically To List Of Contacts
2nd Line Of Code Is To Send Separate Dynamic Message To Respective Contact In The Name Column
*** IMPORTANT *** - Comment Out Anyone Of The Line
'''
message = excel_data['Message'][0]
# message = excel_data['Message'][count]
# Locate Search Box Through x_path
search_box = '//*[@id="side"]/div[1]/div/label/div/div[2]'
person_title = wait.until(lambda driver:driver.find_element_by_xpath(search_box))
# Clear Search Box If Any Contact Number Is Written In It
person_title.clear()
# Wait For 1 Second
time.sleep(1)
# Send Contact Number In Search Box
person_title.send_keys(Keys.HOME)
person_title.send_keys(str(excel_data['Contact'][count]))
count += 1
# Wait For 1 Second To Search Contact Number
time.sleep(1)
try:
'''
Load Error Message If Contact Number Is Unavailable/Wrong
(Currently Only Saved Contact Numbers Are Supported BUT,
You Can Use This Program To AUTOMATE Message For Those Unsaved Contacts
To Whom You Have Texted Atleast One Message Like HI Or Anything Else)
'''
element = driver.find_element_by_xpath('//*[@id="pane-side"]/div[1]/div/span')
except NoSuchElementException:
'''
Format The Message From Excel
(Make The Message Dynamic Or Specific To The Receiver By Adding His/Her/Others Name.
You Can Also Make It Specific In Your Own Way By Adding Other Data Instead Of Name
Such As Company Name, College Name, Location, Etc. MORE DATA MORE SPECIFIC)
'''
message = message.replace('{name}', column)
# Copy Message To Clipboard
pyperclip.copy(message)
person_title.send_keys(Keys.ENTER)
actions = ActionChains(driver)
# Paste Message (CTRL + V)
actions.key_down(Keys.CONTROL).send_keys('v')
actions.perform()
# Wait For 1 Second
time.sleep(1)
# Locate Send Button Through x_path
driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[3]/button').click()
# Wait For 1 Second
time.sleep(1)
# Locate Clip Button Through x_path
driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[1]/div[2]/div/div/span').click()
# Wait For 1 Second
time.sleep(1)
# Locate Button To Send Attachment Through x_path
driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[1]/div[2]/div/span/div/div/ul/li[1]/button').click()
# Wait For 1 Second
time.sleep(1)
autoit.control_focus("Open", "Edit1")
autoit.control_set_text("Open", "Edit1", attachment_path)
autoit.control_click("Open", "Button1")
# Wait For 1 Second
time.sleep(1)
# Locate Send Button Through x_path
driver.find_element_by_xpath('//*[@id="app"]/div/div/div[2]/div[2]/span/div/span/div/div/div[2]/span/div/div').click()
# Wait For 5 Seconds To Deliver The Attachment
time.sleep(5)
# To Clear CLI
clear()
# Close Chrome Driver
driver.quit()
cprint('\n\tDo You Want To Run The Program Again ? (y/n) : ','red',attrs=['bold'])
again_choice = input('\n\t')
if (again_choice.lower() == 'y'):
clear()
continue
else:
exit()
# To Close The Program
elif choice == 4:
exit()
# When User Input Has An Error
else:
cprint('\n\tPlease select a valid option.....!','red',attrs=['bold'])
continue
# END