-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbot.py
113 lines (82 loc) · 3.01 KB
/
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
110
111
112
113
#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-
# Author: Mustafa Durmuş [mustafa-durmuss@outlook.com]
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import json
import time
PATH_TO_DRIVER = "./chromedriver"
PATH_TO_PAGE = "https://web.whatsapp.com/"
def send_message(browser, message):
"""Put the given message to the message box and send.
Args:
browser {selenium.driver}: selenium driver
message {str}: message to send
"""
# get the input box and sending the text!
inp_xpath = '//div[@class="_3FRCZ copyable-text selectable-text"][@contenteditable="true"][@data-tab="1"]'
input_box = browser.find_element_by_xpath(inp_xpath)
time.sleep(1)
input_box.send_keys(message + Keys.ENTER)
time.sleep(2)
# find the button.
send_button = browser.find_element_by_class_name("_1U1xa")
send_button.click()
time.sleep(2)
print("Message has sent.")
def read_json_file():
"""Scan the file and send back related variables.
Returns:
{tuple}: message to send and contact name.
"""
with open('information.json') as json_file:
data = json.load(json_file)
response_text = data['response_text']
contact = data['contact']
return response_text, contact
def find_contact(browser, contact):
"""Click the chat search button and find the contact and open the chat page of it.
Args:
browser {selenium.driver}: selenium driver.
contact {str}: contact to send message.
Returns:
{bool}: True if the contact exits.
"""
# click to the new chat button.
new_chat_button = browser.find_elements_by_class_name("PVMjB")[1]
new_chat_button.click()
time.sleep(1)
# get the input box for searching given contact.
inp_xpath = '//div[@class="_3FRCZ copyable-text selectable-text"][@contenteditable="true"][@data-tab="3"]'
input_box = browser.find_element_by_xpath(inp_xpath)
time.sleep(1)
input_box.send_keys(contact)
time.sleep(1)
contact_button = browser.find_element_by_class_name("_2kHpK")
try:
contact_button.click()
except NoSuchElementException:
print("No contact found. Check the information file again. Process is killing.")
return False
print("Contact is selected.")
return True
def main():
# get the informations from json file.
response_text, contact = read_json_file()
# get the driver.
browser = webdriver.Chrome(PATH_TO_DRIVER)
browser.get(PATH_TO_PAGE)
browser.maximize_window()
# wait until the QR code is scanned.
if input("Please read the QR Code and then press any button."):
print("{}\nWe are in.".format("-" * 50))
time.sleep(2)
if not find_contact(browser=browser, contact=contact):
return
time.sleep(2)
send_message(browser=browser, message=response_text)
time.sleep(2)
# after all close the browser.
browser.close()
if __name__ == '__main__': main()