Skip to content
This repository was archived by the owner on Feb 28, 2022. It is now read-only.

Workaround for emoji problem #5

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 47 additions & 18 deletions two_replikas_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

#Import libraries
import time
from emoji import UNICODE_EMOJI

from selenium import webdriver
from selenium.webdriver.common.keys import Keys


user1=""
password1=""
user2=""
password2=""


#Login function
def login(email, password, browser):
browser.get('https://my.replika.ai/login')
Expand All @@ -24,24 +31,16 @@ def login(email, password, browser):
except:
pass

#remove emojis from string (chromedriver can't process)
def remove_emojis(text_string):
emojiless_text_string = ""
for character in text_string:
if character in UNICODE_EMOJI:
character = ' '
emojiless_text_string = emojiless_text_string + character
return emojiless_text_string



#Instantiate browser 1 and 2
browser1 = webdriver.Chrome()
browser2 = webdriver.Chrome()

#Login browser 1
login('rep1_email', 'rep1_password', browser1) #Replace with your first rep email and password

login(user1, password1, browser1)
#Login browser 2
login ('rep2_email', 'rep2_password', browser2) #Replace with second rep email and password
login(user2, password2, browser2)

#Start conversation
conversation_starter = "Hey, what do you think is the meaning of life?" #Giving the conversation a start point. Could replace this with anything you like.
Expand All @@ -50,26 +49,56 @@ def remove_emojis(text_string):
text_box1.send_keys(conversation_starter)
text_box1.send_keys(Keys.RETURN)

# Mod: Checks message for trigger words, returns Boolean
def checkDownvote(message):

# insert triggerwords to downvote
matches = []

if any(x in message for x in matches):
return True

# Mod: Checks message for trigger words, returns Boolean
def checkUpvote(message):
# insert triggerwords to upvote
matches = []

if any(x in message for x in matches):
return True


#Take most recent response from Rep 1
def get_most_recent_response(browser):
time.sleep(10) #Give rep time to compose response
response = browser.find_element_by_xpath("//div[@tabindex='0']").text

# Mod: Check for upvoting and downvoting
if checkDownvote(response)==True:
browser.execute_script("document.querySelector('div[tabindex=\"0\"] button[data-testid=\"chat-message-downvote-button\"]').click()")
elif checkUpvote(response)==True:
browser.execute_script("document.querySelector('div[tabindex=\"0\"] button[data-testid=\"chat-message-upvote-button\"]').click()")

words_to_strip = ['thumb', 'up', 'down'] #Remove reaction text
response_words = response.split()
response_words_edited = [word for word in response_words if word not in words_to_strip]
response = ' '.join(response_words_edited)
stop_words = ['hug','nuzzle','snuggle']
for stop_word in stop_words:
if stop_word in response_words_edited:
response = "Let's talk about something else"
for stop_word in stop_words:
if stop_word in response_words_edited:
response = "Let's talk about something else"
print(f"Edited response: {response}")
return response

#Insert start text in rep 2
def type_most_recent_response(browser, response):
text_box = browser.find_element_by_id("send-message-textarea")
response = remove_emojis(response) #Remove emojis if using Chrome (Chromedriver can't process)
text_box.send_keys(response)

# Mod: Workaround for emoji problem
script="var elm = arguments[0],txt=arguments[1];elm.value += txt;"
browser.execute_script(script, text_box, response)

# Mod: neccessary else send_keys throws an error
text_box.send_keys(" ")
Comment on lines +95 to +101
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sending the response to the textarea via javascript. The empy space in line 101 is needed to dispatch the native events so that the send works. Simpler this way than to dispatch the events in javascript.

text_box.send_keys(Keys.RETURN)

#Converse back and forth (x100)
Expand Down