Reading the latest WhatsApp messages with python
Hi! I had built this some time ago this code and now I want to share it with everyone. My idea was to open WhatsApp, select the phone number, and read the messages, but then I needed to do this:
- Open WhatsApp using WhatsApp's web protocol
- Wait 10 seconds
- focus on WhatsApp's window
- drag the cursor to select the latest messages
- copy
- Get the clipboard’s content
In this case, the coordinates below may not be correct in your case, so remember to update them:
import pyautogui
import time
time.sleep(5)
print(pyautogui.position())
But first, you need to install the libraries:
$ pip3 install webbrowser
$ pip3 install pyautogui
And here’s the full code:
import webbrowserimport pyautoguiimport timeimport pyperclipphone = pyautogui.prompt('Telephone number')webbrowser.open("whatsapp://send?phone=" + phone)time.sleep(10)pyautogui.click(1803, 916)time.sleep(0.2)pyautogui.dragTo(587, 127, 0.2, button='left')pyautogui.hotkey('ctrl', 'c')ret = pyperclip.paste()pyautogui.alert(ret, "results")print("\n\n*******************\n\n" + ret + "\n\n")pyautogui.click(1900, 4)
Thank you for reading!