Blog available for sell
This blog is available for sale. Please 'contact us' if interested.
Advertise with us
Django application to automate the WhatsApp messaging

In part 1 on this article, we tried automating the messaging sending via WhatsApp web using selenium. Drawback of the approach was that, a new instance of browser was created everytime we need to send the message. And we need to scan the QR code everytime new browser instance is created.

In this article we will fix the above issue. We will also develop a Django application to expose few  API endpoints. Then we will use those endpoints to send the WhatsApp message.

(1) Creating Django application

If this is the first time you are working with Django, please follow this article to setup and run simplest Django application.

Please note that for this article, I used Python 3.10 and Django 3.2. 

(2) Virtual Environment

Create a virtual environment using python 3.10 and install below dependencies in it. Save the below content in a file requirements.txt and run the command pip install -r requirements.txt after activating the virtual environment.


(2) URLs

Create a file in your app and add the below content to it.

Replace app1 with your app name.

from django.urls import path

from app1 import views
from django.urls import path

app_name = "app1"

urlpatterns = [
    path(r'send/', views.send, name='send'),
    path(r'instance/', views.create_instance, name='create_instance'),
    path(r'instance/<str:instance_id>/', views.delete_instance, name='delete_instance'),

Here first URL i.e. send/ is to send the WhatsApp messages using GET and POST methods both. Both methods are supported. Curl to send messages is below.
Second URL i.e. instance/ is used to create a new browser instance. You have to scan the QR code only once and you can re-use that instance everytime you need to send the WhatsApp message.
Third URL is to destroy/delete the instance.

(3) Project URLs

Link you app's URLs with Project URLs. Copy the below content in project's file

from django.urls import path, include

urlpatterns = [
    path(r'', include('app1.urls', namespace="app1")),

(4) Views

In file, paste the below content. I will explain the code shortly.

import json
import logging
import os
import random
import string
import sys
import time
from django.conf import settings
from django.contrib import messages
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.decorators import user_passes_test
from django.contrib.auth.models import User
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect, render
from django.urls import reverse
from django.views.decorators.csrf import csrf_exempt
from selenium import webdriver
from selenium.common.exceptions import (
from import Options
from selenium.webdriver.common.action_chains import ActionChains
from import By
from selenium.webdriver.common.keys import Keys
from import expected_conditions
from import WebDriverWait
from urllib.parse import quote
from import ChromeDriverManager

CHAT_URL = "{phone}&text&type=phone_number&app_absent=1"
headers = {'content_type': 'application/json'}

# keep session_id and object reference of all open browsers
# implement LRU later
active_sessions = {


def create_instance(request):
    chrome_options = Options()
    user_data_dir = ''.join(random.choices(string.ascii_letters, k=8))
    chrome_options.add_argument("--user-data-dir=/tmp/chrome-data/" + user_data_dir)

    browser = webdriver.Chrome(

    handles = browser.window_handles
    for _, handle in enumerate(handles):
        if handle != browser.current_window_handle:

    # print(browser.session_id)
    # print(browser.__dict__)
    {'port': 0, 'vendor_prefix': 'goog', 'service': < object at 0x7f909886db40>, 'command_executor': <selenium.webdriver.chromium.remote_connection.ChromiumRemoteConnection object at 0x7f90989d7c70>, '_is_remote': False, 'session_id': '3519b8069ac6cd1c15fa126c47659201', 'caps': {'acceptInsecureCerts': False, 'browserName': 'chrome', 'browserVersion': '107.0.5304.121', 'chrome': {'chromedriverVersion': '107.0.5304.62 (1eec40d3a5764881c92085aaee66d25075c159aa-refs/branch-heads/5304@{#942})', 'userDataDir': './User_Data'}, 'goog:chromeOptions': {'debuggerAddress': 'localhost:33499'}, 'networkConnectionEnabled': False, 'pageLoadStrategy': 'normal', 'platformName': 'linux', 'proxy': {}, 'setWindowRect': True, 'strictFileInteractability': False, 'timeouts': {'implicit': 0, 'pageLoad': 300000, 'script': 30000}, 'unhandledPromptBehavior': 'dismiss and notify', 'webauthn:extension:credBlob': True, 'webauthn:extension:largeBlob': True, 'webauthn:virtualAuthenticators': True}, 'pinned_scripts': {}, 'error_handler': <selenium.webdriver.remote.errorhandler.ErrorHandler object at 0x7f90989d7640>, '_switch_to': <selenium.webdriver.remote.switch_to.SwitchTo object at 0x7f90989d7b80>, '_mobile': < object at 0x7f90989d7940>, '_file_detector': <selenium.webdriver.remote.file_detector.LocalFileDetector object at 0x7f90989d7550>, '_authenticator_id': None}

    # this make sure we have a list of all active session.
    # also keeping a global reference of this session/browser stops it from closing.
    # otherwise browser will close automatically after this function exists
    session_id = browser.__dict__.get('session_id')
    active_sessions[session_id] = browser

    # browser is now ready with QR code.
    # let's wait someone to scan this QR in WA application and start a new session
    return JsonResponse({
        'instance': session_id

def delete_instance(request, instance_id):
    if request.method == 'DELETE':
        instance = active_sessions.pop(instance_id, None)
        logging.getLogger("info").info("instance {instance} deleted".format(instance=instance))
        response = {
            'status': 'success',
            'message': 'instance {instance} deleted successfully'.format(instance=instance_id)
        return HttpResponse(json.dumps(response), headers=headers, status=200)
        return HttpResponse(status=405)

def send_message(data):
    instance = data.get('instance', '')
    instance = instance.strip()
    if not instance:
        print('no instance found')
        return HttpResponse(
                'message': 'no instance found'
            }), headers=headers, status=400

    phone = data.get('phone', '')
    phone = phone.strip('+')
    if not phone:
        print('no phone found')
        return HttpResponse(
                'message': 'no phone found'
            }), headers=headers, status=400

    message = data.get('message', '')
    message = message.strip()
    if not message:
        print('no message found')
        return HttpResponse(
                'message': 'no message found'
            }), headers=headers, status=400

    browser = active_sessions.get(instance, None)
    if not browser:
        print('no active instance found')
        return HttpResponse(
                'message': f'no active instance with instance id {instance} found'.format(instance=instance)
            }), headers=headers, status=400
    # open chat URL to this number and wait for window/chats to load

    # find text input box
    inp_xpath = (
    input_box = WebDriverWait(browser, 60).until(
        expected_conditions.presence_of_element_located((By.XPATH, inp_xpath))
    for line in message.split("\n"):

    logging.getLogger('info').info(f"Message sent successfully to {phone}")
    return HttpResponse(
            'status': 'success',
            'message': 'Message sent successfully to {phone}'.format(phone=phone)

def send(request):
    if request.method == 'GET':
        data = request.GET
        return send_message(data)

    if request.method == 'POST':
        data = request.body  # byte
        data = json.loads(data.decode('utf-8'))  # json/dict
        return send_message(data)

        return HttpResponse(status=405)

(5) Logging

We are logging the info and errors in log files. For this to work, we need to define the logging configuration. 

Create a file parallel to file.

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'large': {
            'format': '%(asctime)s  %(levelname)s  %(process)d  %(pathname)s  ' +
                      '%(funcName)s  %(lineno)d  %(message)s  '
        'small': {
            'format': '%(asctime)s  %(levelname)s  %(pathname)s  %(lineno)d  %(message)s  '
        'tiny': {
            'format': '%(asctime)s  %(message)s  '
    'handlers': {
        'errors_file': {
            'level': 'ERROR',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'when': 'midnight',
            'interval': 1,
            'filename': os.path.join(BASE_DIR, 'logs/errors.log'),
            'formatter': 'large',
        'warning_file': {
            'level': 'WARNING',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'when': 'midnight',
            'interval': 1,
            'filename': os.path.join(BASE_DIR, 'logs/warnings.log'),
            'formatter': 'large',
        'info_file': {
            'level': 'INFO',
            'class': 'logging.handlers.TimedRotatingFileHandler',
            'when': 'midnight',
            'interval': 1,
            'filename': os.path.join(BASE_DIR, 'logs/info.log'),
            'formatter': 'large',
    'loggers': {
        'error': {
            'handlers': ['errors_file'],
            'level': 'WARNING',
            'propagate': False,
        'warning': {
            'handlers': ['warning_file'],
            'level': 'WARNING',
            'propagate': False,
        'info': {
            'handlers': ['info_file'],
            'level': 'INFO',
            'propagate': False,



Import file in file.

    from .logger_settings import *
except Exception as e:
    print("Unable to load logger settings")

(6) Running application

Django code is ready. Run the application using command python runserver. If everything is correct, you will see below messages on terminal.

Watching for file changes with StatReloader
Performing system checks...

System check identified no issues (0 silenced).

You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python migrate' to apply them.
January 03, 2023 - 12:49:05
Django version 3.2.16, using settings 'wa.settings'
Starting development server at
Quit the server with CONTROL-C.

(7) Testing

You can check if application is working as expected or not by using below CURL requests. 
Open a new terminal and run this curl to create a new browser instance.

curl localhost:8000/instance/

This will start a new browser session in incognito mode and open the base URL of whatsapp web. A QR code will be shown. Scan this QR code in your WhatsApp app and link the devices. 

Once device is linked, you can send the messages.

This API will return the JSON response containing the instance Id. We will use this instance Id in next CURL.

(8) Sending message

Use below curl to send the Whatsapp message.

curl --location --request POST 'localhost:8000/send/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "phone": "Phone number with country code (without +)",
    "message": "Hello Https://",
    "instance": "Instance Id here"

Replace the values in above curl with your phone number and instance Id.

You will see that the message is sent.

Note :-  Message is sent using your account. Abusing this process may result in blocking your account. Please use wisely.

(9) Understanding the code.

I have tried to write a self explanatory code with proper comments and messages. 

Create Instance method is trying to create a new browser instance. To keep the track of all active sessions so that we can re-use the once created session everytime a user wants to send the message with scanning QR code again and again, we will keep the reference of active session in a dictionary. Since the browser instance reference is still alive due to reference in dictionary, browser won't close automatically as it was happening in previous article.

Send method fetches the existing session from dictionary and uses it send the message.

Once you are done and no longer wants to send more messages, you can destroy the instance by calling destroy API as below.

curl -X DELETE localhost:8000/instance/<instance-id>/

These are REST APIs. APIs are returning JSON response. 

You can enhance this project as per your requirements.

In the next article we will host this application on a remote server like digitalocean or pythonanywhere so that application keep running 24 hours. 

Host your Django Application for free on PythonAnyWhere.
If you want complete control of your application and server, you should consider 
DigitalOcean. Create an account with this link and get $200 credits.

1 comment on 'Django Application To Automate The Whatsapp Messaging'
Login to comment

Mohammed Baashar Sept. 28, 2023, 2:39 p.m.
Hello, thanks tons for the perfect explanation and article; Is part III coming ? You don't have dates in the articles. Thanks again.

Related Articles:
Automating PDF generation using Python reportlab module
Generating PDF using python reportlab module, Adding table to PDF using Python, Adding Pie Chart to PDF using Python, Generating PDF invoice using Python code, Automating PDF generation using Python reportlab module...
Accessing Gmail Inbox using Python imaplib module
In this article, we are accessing Gmail inbox using IMAP library of python, We covered how to generate the password of an App to access the gmail inbox, how to read inbox and different category emails, how to read promotional email, forum email and updates, How to search in email in Spam folder, how to search an email by subject line, how to get header values of an email, how to check DKIM, SPF, and DMARC headers of an email...
Sending email with attachments using Python built-in email module
Sending email with attachments using Python built-in email module, adding image as attachment in email while sending using Python, Automating email sending process using Python, Automating email attachment using python...
Automating Facebook page posts using python script
posting on facebook page using python script, automating the facebook page posts, python code to post on facebook page, create facebook page post using python script...
DigitalOcean Referral Badge

© 2022-2023 Python Circle   Contact   Sponsor   Archive   Sitemap