Programming

Automate WhatsApp with PyAutoGUI

Mithu Mithu Sep 10, 2019 ยท 3 mins read
Automate WhatsApp with PyAutoGUI

 

 

Share

Automating WhatsApp with PyAutoGUI

Hey guys, What’s up? LOL.
Now that you’re here, Let’s do magic.  ๐Ÿ˜œ

In this tutorial we’ll be using PyAutoGUI to create a simple program to send message(s) on whatsapp automatically at a given time.

For example:
Say, you want to send someone a birthday wish whenever the time is exactly 12:00am, You could wake up all night or, use this program to automate the process.

PyAutoGUI is a cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.

Full documentation available at: pyautogui.readthedocs.org

Before you proceed, Keep in mind that while working with pyAutoGUI, you might encounter wierd problems such as unintentional mouse movements or keyboard inputs.
You must be able to overcome these.
Don't worry It won't in any way destroy your computer.
A simple sign out and re-sign in (or shutdown) will fix it.

Let's get started.

I assume, You've the latest version of python installed on your computer. If not, https://www.python.org/downloads/

The first step is to install the module.
To install PyAutoGUI, Launch Command Prompt and execute the following command(s):

If you're on Windows:

pip install pyautogui

On Linux, this is:

pip3 install python3-xlib
sudo apt-get install scrot
sudo apt-get install python3-tk
sudo apt-get install python3-dev
pip3 install pyautogui


Check if pyautogui is installed succesfully,
 - Launch Python IDLE and execute the following commands:

import pyautogui
pyautogui.position()

That should return the current position of your mouse on the screen. (As shown below)

PyAutoGUI installation check

If you're having trouble with the installation check out: https://pyautogui.readthedocs.io/en/latest/install.html

Now, make a folder, and create a python file called main.py

In main.py, import the pyautogui, time, and the webbrowser modules

#import the pyautogui, time, and the webbrowser modules
import webbrowser
import pyautogui as pygui, time 

The webbrowser module will be used to open Whatsapp Web in your default browser.
And, the time module, for delaying pyautogui until the page is fully loaded.

#open Whatsapp Web on default web browser
webbrowser.open('https://web.whatsapp.com')
#wait for 10 seconds. For slower connections, increase the value of sleep
time.sleep(10)

Next, set the contact and message to be sent.

contact = "787*******"  #can be name or a number
message ="Good Night!!!"

Now, here comes the tricky part,
You'll have to determine the position of the search bar in WhatsApp Web (marked in yellow).

WhatsApp search bar position

In my case the position was x=142, y=188
That depends on the screen size of your computer.
It'll probably be around those values.

This moves the position of your mouse to the search bar and clicks on it.

pygui.moveTo(x=142, y=188, duration = 0.7)
pygui.click()

This will enter the value of contact to the search bar and hit enter which ends up opening the conversation.

pygui.typewrite(contact, interval = 0.04)
pygui.press('enter')
time.sleep(1)

And, finally, let's send the message.

pygui.typewrite(message, interval = 0.03)
pygui.press('enter')

The final code should look something like this:

#import the pyautogui, time, and the webbrowser modules
import webbrowser
import pyautogui as pygui, time 

#open Whatsapp web in default web browser
webbrowser.open('https://web.whatsapp.com')

#wait for WhatsApp web to load completely
#Increase for slower connections. current is 10s
time.sleep(10)

contact = "787*******"    #this could be name or number
message ="Good Night!!!"

#move to search bar and click
pygui.moveTo(x=142, y=188, duration = 0.7)
pygui.click()

#enter contact name or number and hit enter.
#this opens the conversation
pygui.typewrite(contact, interval = 0.04)
pygui.press('enter')
time.sleep(1)

#type the message and send
pygui.typewrite(message, interval = 0.03)
pygui.press('enter')

Run the program by executing the command,

python main.py

(open CMD/Terminal in the directory your main.py file is in.)

And, That was just an example of how powerful pyAutoGUI module is. Go ahead and experiment with it. You can come up with some cool ideas. 

You could create a program that could draw, or something that fills up online forms automatically, take screenshots and what not? The possibilities are endless....

 

Thanks for reading. That's all for now. Hopefully, you learnt something out of this. ๐Ÿ˜Š
-Mithu Mondal

Join Newsletter
Get the latest news right in your inbox. We never spam!
Mithu
Written by Mithu Mondal Profile
Something went wrong! Please check your internet connection...

CRUD operations using .Net Core Web API and Swagger

.Net Core is been improving since from its first release on November 12, 2014. The latest version of .Net Core is .Net Core 3.0...

Gautam Sharma In Programming about 26 days ago

TinyMCE Image upload to seperate folder in .NET MVC

Upload images to a seperate folder from TinyMCE in ASP.NET MVC, since TinyMCE links images as blob, and sometimes it becomes hard to identify your images... 

Gautam Sharma In Programming about 2 months ago

Basic OSPF Configuration - CCNA

OSPF was designed expressly for IP networks and it supports IP subnetting and tagging of externally derived routing information.

Gautam Sharma In Programming about 2 months ago