Open Whatsapp Windows app directly from python program using url

According to this FAQ page of Whatsapp on How to link to WhatsApp from a different app

Using the URL

whatsapp://send?phone=XXXXXXXXXXXXX&text=Hello

can be used to open the Whatsapp app on a Windows PC and perform a custom action.

It does work when it is opened in a browser. The URL opens Whatsapp installed and composes the message(parameter:text) for the given contact(parameter:phone)

I want to open the Whatsapp application directly using python script without the intervention of browser in between

I have tried using request and urlib, but they don't consider it as a valid schema for URL as it does not have a http:// or https:// instead it has whatsapp://.

requests.exceptions.InvalidSchema: No connection adapters were found for 'whatsapp://send'

Is there a library in python that would open the associated applications directly based on the URL.

P.S. I know the page is for Iphone but the link works perfectly on windows browser. Just need to know if that can be used.

Working with Python 3.7.9 on Windows 10


Solution 1:

You can use cmd exe to do such a job. Just try

import subprocess

subprocess.Popen(["cmd", "/C", "start whatsapp://send?phone=XXXXXXXXXXXXX&text=Hello"], shell=True)

edit: if you want to pass '&'(ampersand) in cmd shell you need to use escape char '^' for it.

please try that one

subprocess.Popen(["cmd", "/C", "start whatsapp://send?phone=XXXXXXXXXXXXX^&text=Hello"], shell=True)