How to send a Whatsapp message with python
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.