Tiago Rangel de Sousa
Tiago Rangel's blog

Follow

Tiago Rangel's blog

Follow
Sending WhatsApp messages with Python and PyAutoGUI

Sending WhatsApp messages with Python and PyAutoGUI

Tiago Rangel de Sousa's photo
Tiago Rangel de Sousa
·Sep 22, 2022·

2 min read

Photo

Photo by Christian Wiediger on Unsplash

This post was originaly posted on my medium

The project

Hi everyone! Today I’m building an text-to-whatsapp python app

Bellow, I have the code, and I will explain everything in the comments.

Oh, but first run the following commands:

$ pip3 install web-browser
$ pip3 install pyautogui

And here we have our code:

import webbrowser # import the webbrowser library, see line bellow
import pyautogui #import the library that will simulate clicks
import time # for sleep
msg = input("Message: ")
phone = input("Telephone number: +")
sim = input("Press enter to send") # Verify
webbrowser.open("whatsapp://send?text=" + msg + "&phone=" + phone) # Open whatsapp's URL protocol. This will open the Whatsapp app and open the send dialog
time.sleep(9) #Give time for the program to execute
pyautogui.click(1870, 984) # Send message (simulate click on send button)
time.sleep(1) # wait 1 sec
pyautogui.click(1900, 4) # simulate click to close whatsapp

To use this code, you must have Whatsapp installed and a 1536x864 (width x height screen. If you don’t have a screen of that dimensions, open WhatsApp, execute the following code, and immediately position your mouse over the close button and the send button of WhatsApp. The program sleeps 5 seconds after execution. Then, change the X and Y coordinates of the pyautogui.click() functions to the results.

import pyautoguiimport
timetime.sleep(5)
print(pyautogui.position()) # Gives you: _Point(x=XXX, y=XXX)_

Thanks for reading!

 
Share this