Last active
July 24, 2023 17:04
-
-
Save ankushKun/fa51f0a9271edcc6e2256e4ca49d7798 to your computer and use it in GitHub Desktop.
Python Selenium script that talks with your girlfriend(s) in your place
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Install requirements | |
# pip3 install -U selenium cahtterbot | |
import time | |
import random | |
from selenium import webdriver | |
from selenium.webdriver.chrome.service import Service | |
from selenium.webdriver.common.by import By | |
from chatterbot import ChatBot | |
from chatterbot.trainers import ChatterBotCorpusTrainer | |
# Name of the Person you want to automate repleies for (as saved in your contacts) | |
NAME = "Manas(CU)" | |
# Can use this list of messages to send text randomly (optional, since the script currently uses chatterbot) | |
# MESSAGES = ["Hello", "Hi", "hellooooo", "aachaa", "ok", "thik", "ooo", "๐ณ", "๐ฎ", "๐", "๐", "wow", "๐คฃ"] | |
chatbot = ChatBot('Ron Obvious') | |
# Create a new trainer for the chatbot | |
trainer = ChatterBotCorpusTrainer(chatbot) | |
# Train the chatbot based on the english corpus | |
trainer.train("chatterbot.corpus.english") | |
options = webdriver.ChromeOptions() | |
options.add_argument("user-data-dir=<Your_browser_user_profile>") | |
driver = webdriver.Chrome(service=Service("./chromedriver"), options=options) | |
# driver.maximize_window() | |
print("Opening Whatsapp") | |
driver.get("https://web.whatsapp.com") | |
input("Press enter when whatsapp has loaded ") | |
def send_message(text): | |
print(f"Opening chat for {NAME}") | |
user = driver.find_element(by=By.XPATH,value=f'//span[@title="{NAME}"]') | |
user.click() | |
time.sleep(2) | |
messages = driver.find_elements(by=By.XPATH, value=f'//div[@class="_22Msk"]') | |
print(messages[-1]) | |
print("Clicking text box") | |
box = driver.find_element(by=By.XPATH, value=f'//div[@title="Type a message"]') | |
box.click() | |
# msg = random.choice(MESSAGES) | |
msg = str(text) | |
print(f"Typing {msg}") | |
box.send_keys(msg) | |
print("Clicking send") | |
time.sleep(2) | |
send_btn = driver.find_element(by=By.XPATH, value=f'//button[@data-testid="compose-btn-send"]') | |
send_btn.click() | |
time.sleep(3) | |
def has_unread(): | |
all_chats = driver.find_elements(by=By.XPATH, value=f'//div[@class="_3OvU8"]') | |
for chat in all_chats: | |
try: | |
our_target = chat.find_element(by=By.XPATH, value=f'.//span[@title="{NAME}"]') | |
title = our_target.get_attribute("title") | |
if title == NAME: | |
print("Found "+title) | |
try: | |
chat.find_element(by=By.XPATH, value=f'.//div[@class="_1pJ9J"]') | |
print("Found Unread message(s) from "+NAME) | |
return True | |
except: | |
print("No Unread messages from "+NAME) | |
break | |
except: | |
our_target = chat.find_element(by=By.XPATH, value=f'.//span[@class="ggj6brxn gfz4du6o r7fjleex g0rxnol2 lhj4utae le5p0ye3 l7jjieqr i0jNr"]') | |
# print(our_target.get_attribute("title")) | |
continue | |
def get_last_message(): | |
print(f"Opening chat for {NAME}") | |
user = driver.find_element(by=By.XPATH,value=f'//span[@title="{NAME}"]') | |
user.click() | |
time.sleep(2) | |
messages = driver.find_elements(by=By.XPATH, value=f'//div[@class="_22Msk"]') | |
last_msg = messages[-1].find_element(by=By.XPATH, value=f'.//span[@class="i0jNr selectable-text copyable-text"]').find_element(by=By.XPATH, value=".//span").text | |
return last_msg | |
while True: | |
if has_unread(): | |
unread_message = get_last_message() | |
response = chatbot.get_response(unread_message) | |
send_message(response) | |
driver.refresh() | |
time.sleep(15) | |
else: | |
time.sleep(2) | |
# Close the browser | |
driver.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment