-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwhatsapp-bot.py
72 lines (59 loc) · 2.23 KB
/
whatsapp-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
import functools
import codecs
import time
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
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 import webdriver
open_file = functools.partial(open, encoding='utf8')
def format():
message = open_file('message.txt', 'r').readlines()
formatted = ''
for i in range(len(message)):
formatted += message[i].rstrip('\n') + " " + "\\n"
with open_file("message_formatted.txt", "w") as final:
final.writelines(formatted)
print("Message formating complete")
print("*** WhatsApp Bot started ***")
format()
driver = webdriver.Firefox()
time.sleep(3)
driver.get("https://web.whatsapp.com")
with open("numbers.txt", "r") as numbers:
for number in numbers:
number = number.strip()
if len(number) < 7 or len(number) > 13:
print("Invalid Number")
else:
driver.get("https://web.whatsapp.com/send?phone=" + number)
element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located(
(By.XPATH,
'//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
)
)
message_box = driver.find_element_by_xpath(
'//*[@id="main"]/footer/div[1]/div[2]/div/div[2]'
)
for line in open_file(
"message_formatted.txt",
"r",
).read().split(r'\n'):
try:
message_box.send_keys(line)
ActionChains(driver) \
.key_down(Keys.SHIFT) \
.key_down(Keys.ENTER) \
.key_up(Keys.SHIFT) \
.key_up(Keys.ENTER) \
.perform()
except:
print("Message format Error")
quit()
driver.find_element_by_xpath(
'//*[@id="main"]/footer/div[1]/div[3]/button'
).click()
print("Message sent to " + number)
time.sleep(2)