Skip to main content
added 5 characters in body
Source Link
SuperBiasedMan
  • 13.5k
  • 5
  • 37
  • 62

I'd like to know if I can improve the performance of my recent bot /, and maybe some design patterns too. 

So far, it's warning a user if it's using some bad words ( whichwhich are parsed from bad_words.txt) and it's entertaining the users by posting jokes from jokes.txt.

I'd like to know if I can improve the performance of my recent bot / and maybe some design patterns. So far, it's warning a user if it's using some bad words ( which are parsed from bad_words.txt) and it's entertaining the users by posting jokes from jokes.txt.

I'd like to know if I can improve the performance of my recent bot, and maybe some design patterns too. 

So far, it's warning a user if it's using some bad words (which are parsed from bad_words.txt) and it's entertaining the users by posting jokes from jokes.txt.

deleted 21 characters in body; edited tags; edited title
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Improving a simple ImprSimple chat bot written in python

Here is the code:

Improving a simple chat bot written in python

Here is the code:

ImprSimple chat bot written in python

Source Link
Grajdeanu Alex
  • 9.3k
  • 4
  • 32
  • 71

Improving a simple chat bot written in python

I'd like to know if I can improve the performance of my recent bot / and maybe some design patterns. So far, it's warning a user if it's using some bad words ( which are parsed from bad_words.txt) and it's entertaining the users by posting jokes from jokes.txt.

Here is the code:

from selenium import webdriver
import time
import random


def login_to_rst(base_url, username, password):
    xpaths = {
        'usernameTxtField': "//input[@name='auth']",
        'passwordTxtField': "//input[@name='password']",
        'submitButton': "//*[@id='ipsLayout_mainArea']/div/div[2]/div/form/li[5]/div/button"
    }
    FIREFOX_DRIVER.get(base_url)
    FIREFOX_DRIVER.find_element_by_xpath(xpaths['usernameTxtField']).clear()
    FIREFOX_DRIVER.find_element_by_xpath(xpaths['usernameTxtField']).send_keys(username)
    FIREFOX_DRIVER.find_element_by_xpath(xpaths['passwordTxtField']).clear()
    FIREFOX_DRIVER.find_element_by_xpath(xpaths['passwordTxtField']).send_keys(password)
    FIREFOX_DRIVER.find_element_by_xpath(xpaths['submitButton']).click()
    FIREFOX_DRIVER.get(base_url)


def write_to_chat(chat_url):
    FIREFOX_DRIVER.get(chat_url)

    bad_words = [line.rstrip('\n') for line in open('bad_words.txt')]

    time.sleep(5)
    message = "Please use a decent language !"
    xpaths = {
        'textArea': "//*[@id='ipsTabs_elChatTabs_chatroom_panel']/div[1]/div[1]/textarea",
        'submitMessage': "//*[@id='ipsTabs_elChatTabs_chatroom_panel']/div[1]/div[1]/div[3]/button"
    }

    parsed_messages, rst_messages_list = [], []
    keep_running = True
    while keep_running:
        divs = FIREFOX_DRIVER.find_elements_by_xpath('//ul[@class="ipsList_reset cChatContainer"]/li/div')
        for div in divs:
            rst_messages_list = []
            if div.id not in parsed_messages:
                rst_messages_list.append(div.text)
                print(rst_messages_list)
                parsed_messages.append(div.id)

                for unique_message in rst_messages_list:
                    for word in bad_words:
                        if word in unique_message:
                            FIREFOX_DRIVER.find_element_by_xpath(xpaths['textArea']).clear()
                            FIREFOX_DRIVER.find_element_by_xpath(xpaths['textArea']).send_keys(message)
                            FIREFOX_DRIVER.find_element_by_xpath(xpaths['submitMessage']).click()

                    if '/message1' in unique_message or '/Message1' in unique_message:
                        FIREFOX_DRIVER.find_element_by_xpath(xpaths['textArea']).clear()
                        FIREFOX_DRIVER.find_element_by_xpath(xpaths['textArea']).send_keys("text to be sent")
                        FIREFOX_DRIVER.find_element_by_xpath(xpaths['submitMessage']).click()
                    if '/message2' in unique_message or '/Message2' in unique_message:
                        FIREFOX_DRIVER.find_element_by_xpath(xpaths['textArea']).clear()
                        FIREFOX_DRIVER.find_element_by_xpath(xpaths['textArea']).send_keys("2016 © me")
                        FIREFOX_DRIVER.find_element_by_xpath(xpaths['submitMessage']).click()
                    if '/message3' in unique_message:
                        line = random.choice(open('jokes.txt').readlines())
                        FIREFOX_DRIVER.find_element_by_xpath(xpaths['textArea']).clear()
                        FIREFOX_DRIVER.find_element_by_xpath(xpaths['textArea']).send_keys(line)
                        FIREFOX_DRIVER.find_element_by_xpath(xpaths['submitMessage']).click()


if __name__ == "__main__":
    FIREFOX_DRIVER = webdriver.Firefox()

    login_to_rst('https://link-login', 'user', 'pass')
    write_to_chat('https://link-final-url')

I accept any constructive critiques as long as they are followed by a rational explanation.