DEV Community

Tiago Rangel
Tiago Rangel

Posted on • Originally published at blog.tiagorangel.com on

Sending WhatsApp messages with Python and PyAutoGUI

This post was originaly posted on my medium

The project

Hi everyone! Today Im building a 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

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

To use this code, you must have Whatsapp installed and a 1536x864 (width x height screen. If you dont 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)_

Enter fullscreen mode Exit fullscreen mode

Thanks for reading!

Top comments (0)