forked from shakedNissanov/WhatsappBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WhatsappBot.py
79 lines (61 loc) · 2.12 KB
/
WhatsappBot.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
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import NoSuchElementException, StaleElementReferenceException
from selenium.webdriver.chrome.options import Options
import threading
import time
driver = None
def whatsappWebConnection(chromeDriverPath):
global driver
chrome_options = Options() # Saving the last session
chrome_options.add_argument("user-data-dir=selenium")
driver = webdriver.Chrome(chromeDriverPath, chrome_options=chrome_options)
driver.get('https://web.whatsapp.com/')
while True:
time.sleep(1)
try:
appLoad = driver.find_element_by_class_name("m6ZEb") # The class only exists after the QR login page
if appLoad:
return # The login was successful
except NoSuchElementException:
pass
def sendMessage(targetName, msg):
global driver
target = driver.find_element_by_xpath('//span[@title= "{}"]'.format(targetName))
target.click()
msg_box = driver.find_element_by_class_name('_2S1VP')
msg_box.send_keys(msg)
button = driver.find_element_by_class_name('_2lkdt')
button.click()
def readAllMessages(targetName, textDirection):
global driver
target = driver.find_element_by_xpath('//span[@title= "{}"]'.format(targetName))
target.click()
messages = None
lastMessage = None
messages = driver.find_elements_by_class_name("_3zb-j")
lastMessage = messages[-1].text
lastMessage = lastMessage[::-1]
while(1):
try:
messages = driver.find_elements_by_class_name("_3zb-j")
if(textDirection == "rtl"):
for i in range(len(messages)):
messages[i] = messages[i][::-1]
return messages
except (NoSuchElementException, StaleElementReferenceException) as e:
pass
def getLastMessage(targetName, textDirection):
global driver
target = driver.find_element_by_xpath('//span[@title= "{}"]'.format(targetName))
target.click()
messages = None
while(1):
try:
messages = driver.find_elements_by_class_name("_3zb-j")
newMessage = messages[-1].text
if(textDirection == "rtl"):
newMessage = newMessage[::-1]
return newMessage
except (NoSuchElementException, StaleElementReferenceException) as e:
pass