A Guide to Sending WhatsApp Messages without API using Python

Babar Ali Jamali
3 min readJan 7, 2023
Whatsapp

This Article is useful for business messaging using python and android without using any API.

WhatsApp is a popular messaging app that allows users to send messages, photos, and videos to each other. WhatsApp also provides a business API that allows businesses to send messages to WhatsApp users programmatically, but this requires an account with WhatsApp and the completion of an onboarding process.

In this article, we will look at how to send WhatsApp messages using Python and Android without using the WhatsApp API. We will do this by using an Android emulator and the PyAutoGUI library to automate the process of sending messages through the WhatsApp client on an Android emulator.

Setting up the environment

To follow along with this tutorial, you will need the following:

  • A computer running MacOS
  • An Android emulator (such as BlueStacks or NOX Player) installed on your computer
  • A WhatsApp account set up on the Android emulator
  • An Android phone connected to your computer and with USB debugging enabled
  • The PyAutoGUI library installed on your computer (install it using pip install pyautogui)

Sending a WhatsApp message using Python

To send a WhatsApp message using Python and an Android emulator, we will use the PyAutoGUI library to automate the process of sending a message through the WhatsApp client on the Android emulator.

First, we need to open the WhatsApp client on the Android emulator. We can do this using the following code:

import pyautogui

# Open the WhatsApp client on the Android emulator
pyautogui.hotkey("command", "a")
pyautogui.typewrite("whatsapp", interval=0.1)
pyautogui.hotkey("enter")

This code uses the hotkey and typewrite functions of the PyAutoGUI library to open the WhatsApp client on the Android emulator. The hotkey function simulates the pressing of a combination of keys, and the typewrite function simulates the typing of a string of text.

Next, we need to search for the contact we want to send the message to. We can do this using the following code:

# Wait for the WhatsApp window to appear
pyautogui.sleep(10)

# Search for the contact
pyautogui.hotkey("command", "a")
pyautogui.typewrite(contact, interval=0.1)
pyautogui.hotkey("enter")

After searching for the contact, we need to wait for the contact’s chat window to appear, and then type the message into the chat window and send it. We can do this using the following code:

# Wait for the contact's chat window to appear
pyautogui.sleep(5)

# Type the message into the chat window
pyautogui.hotkey("command", "a")
pyautogui.typewrite(message, interval=0.1)

# Send the message
pyautogui.hotkey("enter")

The sleep function of the PyAutoGUI library is used to pause the script for a specified number of seconds, to allow time for the contact's chat window to appear. The hotkey and typewrite functions are used to type the message into the chat window, and the enter key is pressed to send the message.

We can combine all of the code above into a single function that takes the contact name and the message as arguments and sends the message to the specified contact:

def send_message(contact, message):
# Open the WhatsApp client on the Android emulator
pyautogui.hotkey("command", "a")
pyautogui.typewrite("whatsapp", interval=0.1)
pyautogui.hotkey("enter")

# Wait for the WhatsApp window to appear
pyautogui.sleep(10)

# Search for the contact
pyautogui.hotkey("command", "a")
pyautogui.typewrite(contact, interval=0.1)
pyautogui.hotkey("enter")

# Wait for the contact's chat window to appear
pyautogui.sleep(5)

# Type the message into the chat window
pyautogui.hotkey("command", "a")
pyautogui.typewrite(message, interval=0.1)

# Send the message
pyautogui.hotkey("enter")

# Example usage
send_message("Babar Ali Jamali", "Asalam-o-Alaikum, how are you?")

This function can be used to send a message to any WhatsApp contact by specifying the contact’s name and the message to send.

Conclusion

In this article, we have looked at how to send WhatsApp messages using Python and Android without using the WhatsApp API. We have done this by using an Android emulator and the PyAutoGUI library to automate the process of sending messages through the WhatsApp client on the Android emulator. This can be a useful approach if you do not have a WhatsApp Business account or do not want to go through the onboarding process with WhatsApp.

Say thanks to Babar Ali Jamali (Python Programmer, IT Professional…) & ChatGPT team for implementation help.

For More Python Programming articles you will need to subscribe and follow me i will publish soon more.

Hope this article will be helpful.

--

--

Babar Ali Jamali

I am IT Professional, Google Cyber Security, IBM's Certified Cyber Security Analyst, Web Developer, Vulnerability/Malware Analysis and Python Programmer.