Skip to content

Instantly share code, notes, and snippets.

@da-steve101
Last active May 7, 2017 12:56
Show Gist options
  • Save da-steve101/31f2b6ffc6099a141e3359bc382ea045 to your computer and use it in GitHub Desktop.
Save da-steve101/31f2b6ffc6099a141e3359bc382ea045 to your computer and use it in GitHub Desktop.
a basic chat bot
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common import exceptions
import time
from retry import retry
from chatterbot import ChatBot
chatbot = ChatBot(
'Ron Obvious',
trainer='chatterbot.trainers.ChatterBotCorpusTrainer'
)
# Train based on the english corpus
chatbot.train("chatterbot.corpus.english")
print "bot trained ... "
usernameStr = '[email protected]'
passwordStr = 'pass'
hangouts_url = "https://accounts.google.com/signin/v2/identifier?service=talk&passive=1209600&continue=https%3A%2F%2Fhangouts.google.com%2F&followup=https%3A%2F%2Fhangouts.google.com%2F&flowName=GlifWebSignIn&flowEntry=ServiceLogin"
browser = webdriver.Chrome("/home/stephen/tmp/chromedriver")
browser.get( hangouts_url )
username = browser.find_element_by_name("identifier")
username.send_keys(usernameStr)
content_tags = browser.find_elements_by_tag_name("content" )
content_tags[0].click()
time.sleep(3)
passwd = browser.find_element_by_name("password")
passwd.send_keys(passwordStr)
content_tags = browser.find_elements_by_tag_name("content" )
content_tags[1].click()
time.sleep(10)
conv_iframe = browser.find_elements_by_xpath("//iframe[@aria-label='Contacts and conversations']")[0]
browser.switch_to_frame( conv_iframe )
time.sleep( 2 )
div_name = "gS5QBe R8jgRe"
all_divs = browser.find_elements_by_tag_name("div")
conv_divs = [ d for d in all_divs if d.get_attribute("class") == div_name ]
dars_conv = [ c for c in conv_divs if "DARS" in c.text ][0]
dars_conv.click()
time.sleep( 3 )
# conv is now open, get back to default to find right conv iframe
def to_dars_context():
browser.switch_to_default_content()
time.sleep( 3 )
dars_iframes = browser.find_elements_by_tag_name("iframe")
dars_iframe = [ df for df in dars_iframes if df.get_attribute("aria-label") is not None and "DARS" in df.get_attribute("aria-label") ][0]
browser.switch_to_frame( dars_iframe )
def get_dars_msg():
to_dars_context()
all_divs = browser.find_elements_by_tag_name( "div" )
sel_divs = [ df for df in all_divs if df.get_attribute("class") is not None and "vE dQ editable" in df.get_attribute("class") ]
return sel_divs[0]
@retry(exceptions.StaleElementReferenceException, tries=3, delay=2)
def send_msg( msg_text ):
dars_msg = get_dars_msg()
dars_msg.send_keys( msg_text + "\n")
@retry(exceptions.StaleElementReferenceException, tries=3, delay=2)
def lastmsg():
all_spans = browser.find_elements_by_tag_name( "span" )
msgs = [ s for s in all_spans if s.get_attribute( "style" ) == "text-align: left;" ]
return msgs[-1].text
def landmines():
last_msg = lastmsg()
print "last_msg = " + last_msg
if "why" in last_msg or "Why" in last_msg:
return send_msg( "landmines" )
if "420" in last_msg:
return send_msg( "blaze it" )
if "bot" in last_msg:
resp = chatbot.get_response( last_msg )
print "resp = " + resp.text
return send_msg( resp.text )
to_dars_context()
while True:
time.sleep( 1 )
landmines()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment