-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
62 lines (58 loc) · 2.09 KB
/
main.py
File metadata and controls
62 lines (58 loc) · 2.09 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
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from method import Method
import traceback, os
"""
Set the name of a group or contact for the bot to open the chat after responding to someone,
this is part of the polling
"""
contactorgroupname = "esperaumpouco"
def whatsapp(drive, api):
"""
This function will be the heart of the project,
it will be responsible for analyzing if there is a new chat waiting for an answer
and responding within the pre-defined functions.
"""
name = message = ''
wait = api.waittemp(temp=1)
unread = driver.find_elements_by_class_name(api.newmsg)
if len(unread) > 0:
__unread = unread[-1]
action = ActionChains(driver)
action.move_to_element_with_offset(__unread, 0, -20)
try:
action.click(); action.perform(); action.click(); action.perform()
except Exception as error:
pass
try:
"""
here the interaction between the bot and the user takes place,
the bot will read the predetermined settings, as in the examples below
"""
name = driver.find_element_by_class_name(api.nome).text
message_content = driver.find_elements_by_class_name(api.chatdados)[-1]
message = message_content.text.lower()
if len(message) > 0:
if (message == "/ping"):
api.sendResponse(response='PONG')
elif (message == "/img"):
api.sendMedia(file=os.path.dirname(os.path.realpath(__file__)) + '/img/Prev.png')
elif (message == "/doc"):
api.sendDocument(file=os.path.dirname(os.path.realpath(__file__)) + '/main.py')
except Exception as error:
traceback.print_exc()
pass
driver.find_element_by_xpath(f"//span[@title='{contactorgroupname}']").click()
api.waittemp(slp=1)
if __name__ == '__main__':
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir={}".format(os.path.join(os.getcwd(), "profile", "wpp")))
driver = webdriver.Chrome(executable_path='chromedriver', options=options)
meth = Method(driver)
meth.waittemp(temp=1)
driver.get("https://web.whatsapp.com/")
try:
while True:
whatsapp(driver, meth)
except KeyboardInterrupt:
print("\nClosing bot ...")