|
| 1 | +fromseleniumimportwebdriver |
| 2 | +fromselenium.webdriver.chrome.optionsimportOptions |
| 3 | +fromselenium.webdriver.common.keysimportKeys |
| 4 | +fromselenium.webdriver.common.action_chainsimportActionChains |
| 5 | +fromrequestsimportget |
| 6 | +frombs4importBeautifulSoupasbs |
| 7 | +importkeyboard |
| 8 | +importtime |
| 9 | +importclick |
| 10 | +importos |
| 11 | +importsys |
| 12 | +importcsv |
| 13 | +importthreading |
| 14 | + |
| 15 | +chrome_options=Options() |
| 16 | +chrome_options.add_argument( |
| 17 | +"user-data-dir="+os.path.dirname(sys.argv[0])) |
| 18 | +driver=webdriver.Chrome(chrome_options=chrome_options) |
| 19 | +driver.maximize_window() |
| 20 | + |
| 21 | +driver.get("https://web.whatsapp.com") |
| 22 | +# Time to load the QR Code and scenning |
| 23 | +time.sleep(25) |
| 24 | + |
| 25 | +# Key in the value of the Chat name that you want to read the messages and reply |
| 26 | +target='"your_friend/group_name"' |
| 27 | + |
| 28 | +# Identify the Chatlist based on its element |
| 29 | +panel=driver.find_element_by_class_name('chatlist-panel-body') |
| 30 | + |
| 31 | +elem=None |
| 32 | +a=0 |
| 33 | +whileelemisNone: |
| 34 | +a+=300 |
| 35 | +try: |
| 36 | +driver.execute_script('arguments[0].scrollTop = %s'%a,panel) |
| 37 | +elem=driver.find_element_by_xpath( |
| 38 | +'//span[@title='+target+']') |
| 39 | +except: |
| 40 | +pass |
| 41 | + |
| 42 | +ac=ActionChains(driver) |
| 43 | +ac.move_to_element(elem).click().perform() |
| 44 | +time.sleep(2) |
| 45 | + |
| 46 | +url=driver.page_source |
| 47 | + |
| 48 | +defreadMessage(): |
| 49 | +threading.Timer(5.0,readMessage).start() |
| 50 | +url=driver.page_source |
| 51 | +soup=bs(url,"lxml") |
| 52 | + |
| 53 | +try: |
| 54 | +gotdiv=soup.find_all("div", {"class" :"msg msg-group" })[-1] |
| 55 | +exceptIndexError: |
| 56 | +gotdiv='null' |
| 57 | + |
| 58 | +ifgotdiv=='null': |
| 59 | +div=soup.find_all("div", {"class" :"bubble bubble-text copyable-text" })[-1] |
| 60 | +# print(div) |
| 61 | +else: |
| 62 | +div=soup.find_all("div", {"class" :"msg msg-group" })[-1] |
| 63 | + |
| 64 | +text=div.find_all('span') |
| 65 | +print(text) |
| 66 | + |
| 67 | +try: |
| 68 | +gottext=text[4].find_all(text=True)[1] |
| 69 | +exceptIndexError: |
| 70 | +gottext='null' |
| 71 | + |
| 72 | +ifgottext=='null': |
| 73 | +div=soup.find_all("div", {"class" :"chat-title" })[-1] |
| 74 | +name=div.find_all(text=True)[1] |
| 75 | +try: |
| 76 | +msg=text[-2].find_all(text=True)[1].lower() |
| 77 | +exceptIndexError: |
| 78 | +msg="You replied last" |
| 79 | +time=text[-1].find(text=True) |
| 80 | + |
| 81 | +else:#group |
| 82 | +name=text[3].find_all(text=True)[1] |
| 83 | +try: |
| 84 | +msg=text[4].find_all(text=True)[1].lower() |
| 85 | +exceptIndexError: |
| 86 | +msg="You replied last" |
| 87 | +try: |
| 88 | +time=text[-2].find(text=True) |
| 89 | +except: |
| 90 | +time="None" |
| 91 | + |
| 92 | + |
| 93 | +print(name,msg,time) |
| 94 | + |
| 95 | +# Getting appropriate reply from the csv |
| 96 | +# Bot will lookup the csv for reply Only if the text contains the word buddy |
| 97 | + |
| 98 | +if"buddy"inmsg: |
| 99 | + |
| 100 | +withopen('dict.csv',"r")asf: |
| 101 | +reader=csv.reader(f) |
| 102 | +chat= {} |
| 103 | + |
| 104 | +forrowinreader: |
| 105 | +key=row[0] |
| 106 | +chat[key]=row[1:] |
| 107 | +try: |
| 108 | +gotreply=chat[msg] |
| 109 | +exceptKeyError: |
| 110 | +gotreply='null' |
| 111 | + |
| 112 | +print(gotreply) |