Use Python To Send WhatsApp Message

Sharing is caring!

Last Updated on July 14, 2022 by Jay

Not everyone checks email regularly so we are also going to use Python to send Whatsapp messages to ourselves when our bot discovers a price drop on Amazon. Check out the following links if you need help with any of the steps.

Tools We Need For Sending Whatsapp Message With Python

  1. A working phone number & Whatsapp account – if you don’t have a Whatsapp, go register for one, it’s free.
  2. Selenium & Chrome driver – you should already have this if you followed us from part 1 of the Amazon price notification bot.

Set up Chrome user profile & Selenium

By default, Selenium uses a “new” Chrome profile every time it opens a browser. Meaning that there’s no browsing history, no cookies, nothing stored in the newly opened Chrome browser. It’s a fresh Chrome browser that just came out of the factory.

This isn’t a problem for our Amazon site because we don’t need to be logged in to view product information. However, this is a problem for Whatsapp, because if Selenium opens a “new” Chrome browser then it means we still need to log in to Whatsapp. Try the following code, replace {ur_mobile_number} with your working mobile number, then you’ll see what I mean:

from selenium import webdriver

wassup_url = r'https://web.whatsapp.com/send?phone={ur_mobile_number}&text&app_absent=0'

driver = webdriver.Chrome(r'C:\Users\jay\Desktop\PythonInOffice\amazon_price_alert_bot\chromedriver.exe')

driver.get(wassup_url )

A Chrome browser pops up and, and you should see a screen that asks you to scan a QR code to log in to Whatsapp…

It means we need to manually use our phone to scan it before the bot can access our Whatsapp. This is unacceptable because we are building a 100% automatic bot!

To fix this, we need to do two things:

  1. Create a Chrome user profile and let Selenium use it instead the default (i.e. a blank slate profile every time), because Chrome browser needs to “remember” that we are already logged in to Whatsapp.
  2. One-time only manually scan QR to log in Whatsapp. When we do this, the browser is smart enough to remember our credential and will stay logged in going forward, as long as we use the user profile created in step #1.

Let’s create a folder called “whatsapp_profile” in the current working directory. You can choose any location and any folder name. Then we’ll add the following into code, make sure to add your chrome profile folder for the user-data-dir argument inside the .add_argument method.

from selenium.webdriver.chrome.options import Options

opt = Options()
opt.add_argument(r'user-data-dir=C:\Users\jay\Desktop\PythonInOffice\amazon_price_alert_bot\whatsapp_profile')

driver = webdriver.Chrome(r'C:\Users\jay\Desktop\PythonInOffice\amazon_price_alert_bot\chromedriver.exe', options=opt)

Run the above code, you’ll notice a bunch of things populated in the “whatsapp_profile” folder. A Chrome browser should open up with the Whatsapp page, go ahead and use your phone to scan the QR code to log in. After logging in, you can close the browser and Selenium. Try re-running the code, this time you should land on a Whatsapp page that’s already logged in. YAY!!

Also because of the URL we used, the send?phone={ur_mobile_number} means open to the page that’s ready to send a message to the phone number listed.

Send Whatsapp Message Using Python

Send Whatsapp Messages Using Python & Selenium

Although we are on the Whatsapp app, it’s still a web browser, which means that we can automate it using Selenium.

Right-click on the blank chatbox, find the XPath. If you don’t know how to do this, you should check out part 1 of the tutorial.

Then we assign that chatbox element to a variable chatbox, which we can interact with later.

The .send_keys() method will simulate keystrokes from our keyboard, so we can input any text.

We also imported the Keys object from selenium. This is used for special keys such as pressing the ENTER key, etc.

Note we also added two time.sleep() for pausing the program for 20 seconds and 0.1 seconds respectively. The reason for the 20 is because sometimes it takes a long time for Whatsapp to log in. The 0.1 second wait time is to make sure we “press enter” after completing the text input “hello from python”.

from selenium.webdriver.common.keys import Keys

time.sleep(20)
chatbox = driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div/div/div[2]/div[1]/div/div[2]')

chatbox.send_keys('hello from python')
time.sleep(0.1)
chatbox.send_keys(Keys.RETURN)

Interested in finding out what other special Keys we can use? Import the Keys object into Python, then dir(Keys). dir is a built-in python method that can show attributes of an object.

>>> dir(Keys)
['ADD', 'ALT', 'ARROW_DOWN', 'ARROW_LEFT', 'ARROW_RIGHT', 'ARROW_UP', 'BACKSPACE', 'BACK_SPACE', 'CANCEL', 'CLEAR', 'COMMAND', 'CONTROL', 'DECIMAL', 'DELETE', 'DIVIDE', 'DOWN', 'END', 'ENTER', 'EQUALS', 'ESCAPE', 'F1', 'F10', 'F11', 'F12', 'F2', 'F3', 'F4', 'F5', 'F6', 'F7', 'F8', 'F9', 'HELP', 'HOME', 'INSERT', 'LEFT', 'LEFT_ALT', 'LEFT_CONTROL', 'LEFT_SHIFT', 'META', 'MULTIPLY', 'NULL', 'NUMPAD0', 'NUMPAD1', 'NUMPAD2', 'NUMPAD3', 'NUMPAD4', 'NUMPAD5', 'NUMPAD6', 'NUMPAD7', 'NUMPAD8', 'NUMPAD9', 'PAGE_DOWN', 'PAGE_UP', 'PAUSE', 'RETURN', 'RIGHT', 'SEMICOLON', 'SEPARATOR', 'SHIFT', 'SPACE', 'SUBTRACT', 'TAB', 'UP', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

Now we can send a Whatsapp message to ourselves. Next, let’s automate the whole thing so we don’t even need to think about it.

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *