This is the next post in automation category. We will see how to automate the sending of WhatsApp messages without using official WhatsApp business APIs. Using WhatsApp business APIs is not easy as you need to get the template whitelisted and also usage is not free.
To automate the whatsapp messaging, we will create a virtual environment, install few dependencies in it and activate the virtual environment.
Dependencies:
selenium==4.7.2 webdriver-manager==3.8.5
Now the code part.
Here is the minimal code required to understand the concept and to send the message. We can always enhance this code to add more features and streamline the process.
(1) Import the dependencies.
import random import string import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_conditions from selenium.webdriver.support.ui import WebDriverWait from webdriver_manager.chrome import ChromeDriverManager
(2) Define Urls
BASE_URL = "https://web.whatsapp.com/" CHAT_URL = "https://web.whatsapp.com/send?phone={phone}&text&type=phone_number&app_absent=1"
(3) Create the chrome browser instance.
chrome_options = Options() chrome_options.add_argument("start-maximized") user_data_dir = ''.join(random.choices(string.ascii_letters, k=8)) chrome_options.add_argument("--user-data-dir=/tmp/chrome-data/" + user_data_dir) chrome_options.add_argument("--incognito") browser = webdriver.Chrome( ChromeDriverManager().install(), options=chrome_options, ) browser.get(BASE_URL) browser.maximize_window()
This will open an incognito window with Whatsapp URL. A QR code will be displayed which you need to scan.
Open you WhatsApp mobile application and scan the QR code to link the account. Once scan is completed and chats are loaded, script will execute the next part of code.
(4) Send message using the browser instance created above.
Phone number must be appended with international code without plus sign.
phone = 919999999999 message = 'Hi There. This is test message from PythonCircle.com'
Open the chat URL for this phone number and wait for few seconds to load the chats.
browser.get(CHAT_URL.format(phone=phone)) time.sleep(3)
Now search the chat input box using XPath and enter the message. Send the ENTER key after it.
inp_xpath = ( '//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[1]' ) input_box = WebDriverWait(browser, 60).until( expected_conditions.presence_of_element_located((By.XPATH, inp_xpath)) ) input_box.send_keys(message) input_box.send_keys(Keys.ENTER)
(5) Wait for the message to be sent.
time.sleep(5)
(6) Everything together
import random import string import time from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support import expected_conditions from selenium.webdriver.support.ui import WebDriverWait from webdriver_manager.chrome import ChromeDriverManager BASE_URL = "https://web.whatsapp.com/" CHAT_URL = "https://web.whatsapp.com/send?phone={phone}&text&type=phone_number&app_absent=1" chrome_options = Options() chrome_options.add_argument("start-maximized") user_data_dir = ''.join(random.choices(string.ascii_letters, k=8)) chrome_options.add_argument("--user-data-dir=/tmp/chrome-data/" + user_data_dir) chrome_options.add_argument("--incognito") browser = webdriver.Chrome(ChromeDriverManager().install(), options=chrome_options,) browser.get(BASE_URL) browser.maximize_window() phone = "919999999" message = 'Hi There. This is test message from PythonCircle.com' browser.get(CHAT_URL.format(phone=phone)) time.sleep(3) inp_xpath = ( '//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[1]' ) input_box = WebDriverWait(browser, 60).until( expected_conditions.presence_of_element_located((By.XPATH, inp_xpath)) ) input_box.send_keys(message) input_box.send_keys(Keys.ENTER) time.sleep(10)
Once the script is executed and message is sent, the window will be closed automatically. Session will end. If you need to send another message, you need to re-run the script.
Now running the script everytime to send a message and then scanning the QR code is not feasible. Manual intervention is required to send message. Hence we can not say that this is fully automated.
This was the first step to get you started. In the next article, we will develop a Django application with API endpoints to fully automate this process.
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.