-
Notifications
You must be signed in to change notification settings - Fork 0
/
wa-bot.py
109 lines (93 loc) · 3.62 KB
/
wa-bot.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
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options as ChromeOptions
# options
# Path = "G:\chromedriver\chromedriver.exe"
chrome_options = ChromeOptions()
chrome_options.add_experimental_option("detach", True) # to keep the chrome open
chrome_options.add_experimental_option("excludeSwitches", ["enable-logging"]) # to hide the warning
# open chromedriver v.109 and go to whatsapp web page
# executable_path can be passed as an argument to the webdriver.Chrome() function
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://web.whatsapp.com/")
# search target
def search_target(driver, target):
driver.implicitly_wait(1)
search_box = driver.find_element(
By.XPATH, '//div[@class="Er7QU copyable-text selectable-text"]')
search_box.send_keys(target + Keys.ENTER)
# get target
driver.implicitly_wait(1)
driver.find_element(By.XPATH, '//span[@class="matched-text _11JPr"]').click()
# set what to send
def chat(driver, message):
driver.implicitly_wait(1)
# message_box = driver.find_element(By.XPATH, '//div[@class="fd365im1 to2l77zo bbv8nyr4 mwp4sxku gfz4du6o ag5g9lrv bze30y65 bdf91cm1"]')
message_box = driver.find_element(By.XPATH, '//div[@title="Ketik pesan"]')
message_box.send_keys(message)
message_box.send_keys(Keys.ENTER)
# spam message
def spam_message(driver, message, spamMessage):
for i in range(int(spamMessage)):
driver.implicitly_wait(1)
chat(driver, message)
# do send message
def send_message(driver):
message = input("Enter message: ")
spamMessage = input("Enter numbers to spam message: ")
spam_message(driver, message, spamMessage)
# program finished
print("Program finished\n")
# ask to repeat the message, and ask to change the target or not
repeat_message(driver, message, spamMessage)
# ask to repeat the message, and ask to change the target or not
def repeat_message(driver, message, spamMessage):
repeat = input("Do you want to repeat the message? (y/n): ")
if repeat == "y":
# CHANGE THE TARGET
change_target = input("Do you want to change the target? (y/n): ")
if change_target == "y":
target = input("Enter target name: ")
search_target(driver, target)
# CHANGE THE MESSAGE
send_message(driver)
else:
# ask to change the message
change_message = input("Do you want to change the message? (y/n): ")
if change_message == "y":
# change the message
send_message(driver)
else:
spam_message(driver, message, spamMessage)
else:
print("Program finished\n")
return False
# ask to repeat the message, and ask to change the target or not
run_again(driver)
# run the program again
def run_again(driver):
run = input("Do you want to run the program again? (y/n): ")
if run == "y":
main(driver)
else:
print("Program finished\n")
driver.close()
# main
def main(driver):
# wait for user to scan qr code
input("Press any key after scanning qr code\n")
# get target
target = input("Enter target name: ")
search_target(driver, target)
# send message
send_message(driver)
# run the program
if __name__ == "__main__":
try:
main(driver)
except Exception as e:
print("There is an error, please run the program again\n")
print(e) # to show the error when debugging
# ask to run the program again
run_again(driver)