How to send a Whatsapp message with python

Tiago Rangel
2 min readApr 23, 2022

Photo by Christian Wiediger on Unsplash

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-browserpip3 install pyautogui

And here we have our code:

import webbrowser # import the webbrowser library, see line bellowimport pyautogui #import the library that will simulate clicksimport time # for sleepmsg = input("Message: ")phone = input("Telephone number: +")sim = input("Press enter to send") # Verifywebbrowser.open("whatsapp://send?text=" + msg + "&phone=" + phone) # Open whatsapp's URL protocol. This will open the Whatsapp app and open the send dialogtime.sleep(9) #Give time for the program to executepyautogui.click(1870, 984) # Send message (simulate click on send button)time.sleep(1) # wait 1 secpyautogui.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)

Bye! Please clap 👏 (I would love 50 claps!) if you like this post 😀. If you need help, just comment.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Tiago Rangel
Tiago Rangel

Written by Tiago Rangel

Hi! I'm Tiago, a student at Lisbon, Portugal.

No responses yet

Write a response