Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.56 KB | None | 0 0
  1. Cleto DLC:
  2. # Imports
  3. from selenium import webdriver
  4. import os
  5. from selenium.webdriver.common.action_chains import ActionChains
  6. from selenium.webdriver.common.keys import Keys
  7. from pynput.keyboard import Key, Controller
  8. import pynput
  9. import time
  10.  
  11. class Whatsappy:
  12.  
  13. # Creating an instance of Whatsappy
  14. def __init__(self, browser, name):
  15.  
  16. self.keyboard = Controller()
  17. # Creating a variable to the lastMessage
  18. self.lastMessage = ""
  19. # Receiving the name
  20. self.name = name
  21.  
  22. # Choosing the browser and creating the driver
  23. if browser == "Chrome":
  24. self.driver = webdriver.Chrome()
  25.  
  26. elif browser == "Firefox":
  27. self.driver = webdriver.Firefox()
  28.  
  29. else:
  30. print("Error! This browser don't exist in our list.")
  31. exit()
  32.  
  33. # Entering on the Whatsapp Web site
  34. self.driver.get("https://web.whatsapp.com")
  35.  
  36. # waiting the QR CODE scan
  37. while True:
  38. try:
  39. user = self.driver.find_element_by_xpath('//span[@title = "{}"]'.format(name))
  40. user.click()
  41. break
  42. except:
  43. pass
  44.  
  45. # Getting the username using whoami command
  46. username = os.popen("whoami").read()
  47. username = str(username)[:len(username)-1]
  48.  
  49. # Waiting the loading of the page
  50. time.sleep(2)
  51.  
  52. # Sending the initial message
  53. self.sendMessage("{} connected your Jarvis named by {}.".format(username, self.name))
  54.  
  55. # Function to send messages
  56. def sendMessage(self, message):
  57.  
  58. # Searching the text box
  59. textBox = self.driver.find_element_by_xpath("//div[@spellcheck='true']")
  60. # Checking if the message isn't empty
  61. if message != "":
  62. # Adding the bot name to the message
  63. message = self.name + ": \n" + str(message) + "\n"
  64. # Sending the characteres
  65. textBox.send_keys(message)
  66.  
  67. # davi q fez, nem sei como funciona, é noix
  68. def send_attachment(self, path):
  69.  
  70. # Attachment Drop Down Menu
  71. clipButton = self.driver.find_element_by_xpath('//div[@title="Anexar"]')
  72. clipButton.click()
  73. time.sleep(1)
  74.  
  75. # To send Videos and Images.
  76. mediaButtons = self.driver.find_elements_by_class_name('GK4Lv')
  77. mediaButtons[0].click()
  78. self.keyboard.type(path)
  79. self.keyboard.press(pynput.keyboard.Key.enter)
  80. self.keyboard.release(pynput.keyboard.Key.enter)
  81. time.sleep(2)
  82. self.keyboard.press(pynput.keyboard.Key.enter)
  83. self.keyboard.release(pynput.keyboard.Key.enter)
  84. time.sleep(2)
  85. self.keyboard.press(pynput.keyboard.Key.enter)
  86. self.keyboard.release(pynput.keyboard.Key.enter)
  87.  
  88. # Checking if have a new message
  89. def checkNewMessage(self):
  90.  
  91. # Catching all the audios
  92. audios = self.driver.find_elements_by_tag_name('audio')
  93. if len(audios) != 0:
  94. lastAudio = audios[-1].get_attribute('src')
  95.  
  96. # Catching all the messages
  97. messages = self.driver.find_elements_by_class_name('_3zb-j')
  98. # Supposed new message is the last message
  99. newMessage = messages[-1].text
  100.  
  101. # Checking if the new message isn't igual to the last message
  102. if newMessage != self.lastMessage:
  103. # We have a new message
  104. self.lastMessage = newMessage
  105. return newMessage
  106.  
  107. else:
  108. # No new message
  109. return ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement