Python API to Whatsapps

Prasad
2 min readApr 29, 2023

--

To connect WhatsApp with Python, you can use a Python library called “yowsup”. Yowsup is a Python library that allows you to interact with WhatsApp through their public API.

Here are the basic steps to use yowsup to connect to WhatsApp:

1. Register a WhatsApp account: You will need a valid WhatsApp account to use the yowsup library. You can use your personal phone number, but it’s recommended to use a dedicated number for your application.

2. Install yowsup: You can install yowsup using pip. Run the following command in your terminal: `pip install yowsup2`.

3. Obtain credentials: You will need to obtain a phone number, password, and a unique identity string (called a “mcc” and “mnc”) to authenticate with the WhatsApp API. You can obtain these credentials by using the yowsup-cli tool.

4. Use yowsup to send and receive messages: Once you have obtained your credentials, you can use the yowsup library to send and receive messages from your WhatsApp account.

from yowsup.layers import YowLayerEvent, YowParallelLayer
from yowsup.layers.auth import YowAuthenticationProtocolLayer
from yowsup.layers.protocol_messages import YowMessagesProtocolLayer
from yowsup.layers.network import YowNetworkLayer
from yowsup.layers.stacks import YowStack

class WhatsAppClient:
def __init__(self, username, password):
self.stack = YowStack([
YowAuthenticationProtocolLayer,
YowMessagesProtocolLayer,
YowNetworkLayer
])
self.stack.setProp(YowAuthenticationProtocolLayer.PROP_CREDENTIALS, (username, password))
self.stack.setProp(YowNetworkLayer.PROP_ENDPOINT, 'android.phone')
self.stack.setProp(YowNetworkLayer.PROP_CONNECT_TIMEOUT, 10)
self.stack.setProp(YowNetworkLayer.PROP_RECEIVE_TIMEOUT, 30)
self.stack.setProp(YowMessagesProtocolLayer.PROP_RECEIVE_OFFLINE_MESSAGES, True)
self.stack.setProp(YowLayerEvent.PROP_RECEIPT_POLICY, YowParallelLayer.RECEIPT_POLICY_OPTIMISTIC)
self.stack.setProp(YowLayerEvent.PROP_DISCARD_POLICY, YowParallelLayer.DISCARD_POLICY_FIRST)
self.stack.setProp(YowLayerEvent.PROP_RETRANSMIT_POLICY, YowParallelLayer.RETRANSMIT_POLICY_ALWAYS)

def start(self):
self.stack.start()

def stop(self):
self.stack.stop()

def send_message(self, phone_number, message):
message_id = self.stack.broadcastEvent(YowLayerEvent(YowMessagesProtocolLayer.EVENT_SEND_MESSAGE, {
'phone_number': phone_number,
'message': message
}))
return message_id

def on_message_received(self, message):
print(message)

if __name__ == '__main__':
client = WhatsAppClient('your_phone_number', 'your_password')
client.start()
client.send_message('recipient_phone_number', 'Hello, world!')

Auto response — with Old school

you can use the selenium Python package along with a web driver like chromedriver or geckodriver to automate the WhatsApp Web interface.Here is an example code snippet that uses selenium to send an auto-response to incoming messages:

from selenium import webdriver
import time
MESSAGE = "Hello! Thanks for your message. I will get back to you soon."
RESPONSE = "Auto-response: " + MESSAGE
driver = webdriver.Chrome()
driver.get("https://web.whatsapp.com/")
time.sleep(15) # Wait for user to scan QR code
unread_chats = driver.find_elements_by_xpath('//span[@class="OUeyt"]')
for chat in unread_chats:
if chat.get_attribute("title") != "":
chat.click()
break
input_box = driver.find_element_by_xpath('//div[@class="_3FRCZ copyable-text selectable-text"][@contenteditable="true"][@data-tab="1"]')
input_box.send_keys(RESPONSE)
# Find the send button and click it
send_button = driver.find_element_by_xpath('//span[@data-icon="send"]')
send_button.click()
driver.quit()

This code will open the WhatsApp Web interface and automatically send an auto-response to the first unread chat that has an incoming message. You can modify the code to customize the response message and to handle multiple incoming messages or different chat scenarios. Note that this approach requires you to keep the browser window open and logged in to your WhatsApp account.

--

--

Prasad
Prasad

Written by Prasad

I am a OpenSource Enthusiast|Python Lover who attempts to find simple explanations for questions and share them with others

No responses yet