-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhat_bot.py
68 lines (51 loc) · 2.02 KB
/
what_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
import time
from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from config import USER_DATA_DIR, PROFILE_DIRECTORY
from dotenv import load_dotenv
import os
# Load .env file
load_dotenv()
# Set ChromeOptions to specify the user data directory
options = Options()
options.add_argument(f"user-data-dir={USER_DATA_DIR}")
options.add_argument(f"profile-directory={PROFILE_DIRECTORY}")
# options.add_argument('--restore-last-session')
# Initialize the WebDriver with the specified options
try:
driver = webdriver.Chrome(options=options)
except Exception as e:
print(f"Error initializing WebDriver: {e}")
driver.get("https://web.whatsapp.com")
time.sleep(10) # Wait for QR scan (for the first time)
def find_contact(phone_number):
new_chat_btn = driver.find_element(By.XPATH, "//div[@role='button' and @aria-label='New chat']")
new_chat_btn.click()
time.sleep(2)
driver.switch_to.active_element.send_keys(phone_number)
time.sleep(2)
driver.switch_to.active_element.send_keys(Keys.RETURN)
def send_message(message):
driver.switch_to.active_element.send_keys(message)
time.sleep(2)
driver.switch_to.active_element.send_keys(Keys.RETURN)
def main():
try:
phone_numbers = os.getenv("PHONE_NUMBERS").split(',')
message = "Trying to automate WhatsApp using Python! \n This is a test message."
for number in phone_numbers:
find_contact(number)
send_message(message)
time.sleep(5)
except Exception as e:
print(f"Error: {e}")
driver.quit()
if __name__ == "__main__":
main()