Coders Packet

WhatsApp Spambot with pyautogui using Python

By Mriganka Paul

This Project is used to send custom messages to targeted users via WhatsApp a predefined number of times. The default message is "Hi!"

The Python module pyautogui helps users to automate the keypresses and mouse clicks programmatically.

This article is for education purposes only and may be used by a developer to test the Python integration with the web browser. Spamming is not likely to be done and may cause trouble if done to unknown people.

So, What is Spamming?

It refers to sending unsolicited messages to a large number of systems over the internet. 

Is there any real-life application for the same?

Yes, firstly we can send multiple reminders to a particular person.

Secondly, we can fill the inbox of our best friend at a midnight and he/she might be surprised to see a stream of messages the next morning. Great Prank huh?

 

Well, jokes aside, spambots are lightweight code and are really useful to test the accessibility of the browser using python code directly.

 

What is the Approach to running it?

Here you go:

Install python on your computer and make sure that the version is the latest, for more features.

We will be using Jupyter Notebook to run this Spambot.

Open cmd and type-> jupyter notebook:

jupyter notebook
[I 19:56:53.814 NotebookApp] Serving notebooks from local directory: C:\Users\admin
[I 19:56:53.814 NotebookApp] Jupyter Notebook 6.4.8 is running at:
[I 19:56:53.816 NotebookApp] http://localhost:8888/?token=4cf10b67c5bc4d04ec904c529701d49f6bd72d92e6eb5abd
[I 19:56:53.816 NotebookApp]  or http://127.0.0.1:8888/?token=4cf10b67c5bc4d04ec904c529701d49f6bd72d92e6eb5abd
[I 19:56:53.816 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 19:56:54.510 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///C:/Users/admin/AppData/Roaming/jupyter/runtime/nbserver-8520-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=4cf10b67c5bc4d04ec904c529701d49f6bd72d92e6eb5abd
     or http://127.0.0.1:8888/?token=4cf10b67c5bc4d04ec904c529701d49f6bd72d92e6eb5abd

This starts running jupyter on your browser.

From the top right select new and from the dropdown select Python3 (ipykernel)

Start running the code.

pip install pyautogui

Press Shift+Enter. This installs the pyautogui on your computer.

Collecting pyautogui
  Using cached PyAutoGUI-0.9.53.tar.gz (59 kB)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Collecting pymsgbox
  Using cached PyMsgBox-1.0.9.tar.gz (18 kB)
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
  Getting requirements to build wheel: finished with status 'done'
  Preparing metadata (pyproject.toml): started
  Preparing metadata (pyproject.toml): finished with status 'done'
Collecting PyTweening>=1.0.1
  Using cached pytweening-1.0.4.tar.gz (14 kB)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Collecting pyscreeze>=0.1.21
  Using cached PyScreeze-0.1.28.tar.gz (25 kB)
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
  Getting requirements to build wheel: finished with status 'done'
  Preparing metadata (pyproject.toml): started
  Preparing metadata (pyproject.toml): finished with status 'done'
Collecting pygetwindow>=0.0.5
  Using cached PyGetWindow-0.0.9.tar.gz (9.7 kB)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Collecting mouseinfo
  Using cached MouseInfo-0.1.3.tar.gz (10 kB)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Collecting pyrect
  Using cached PyRect-0.1.4.tar.gz (15 kB)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Collecting pyperclip
  Using cached pyperclip-1.8.2.tar.gz (20 kB)
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'done'
Using legacy 'setup.py install' for pyautogui, since package 'wheel' is not installed.
Using legacy 'setup.py install' for pygetwindow, since package 'wheel' is not installed.
Using legacy 'setup.py install' for PyTweening, since package 'wheel' is not installed.
Using legacy 'setup.py install' for mouseinfo, since package 'wheel' is not installed.
Using legacy 'setup.py install' for pyperclip, since package 'wheel' is not installed.
Using legacy 'setup.py install' for pyrect, since package 'wheel' is not installed.
Building wheels for collected packages: pyscreeze, pymsgbox
  Building wheel for pyscreeze (pyproject.toml): started
  Building wheel for pyscreeze (pyproject.toml): finished with status 'done'
  Created wheel for pyscreeze: filename=PyScreeze-0.1.28-py3-none-any.whl size=13023 sha256=de9d671d31a39f104589d0da059abef2bca18b4717f8e0143c6e0c13e85af9ba
  Stored in directory: c:\users\mriganka\appdata\local\pip\cache\wheels\a2\5b\86\99f1d8fac5d92de0ccb3f0d4ad15e3f4278baf75a9b0f20b93
  Building wheel for pymsgbox (pyproject.toml): started
  Building wheel for pymsgbox (pyproject.toml): finished with status 'done'
  Created wheel for pymsgbox: filename=PyMsgBox-1.0.9-py3-none-any.whl size=7420 sha256=f0dba868eac2981ad3ff61aefb48ec9c8672131808b8933bfc0f2e52870883d7
  Stored in directory: c:\users\mriganka\appdata\local\pip\cache\wheels\7f\13\8c\584c519464297d9637f9cd29fd1dcdf55e2a2cab225c76a2db
Successfully built pyscreeze pymsgboxNote: you may need to restart the kernel to use updated packages.

Installing collected packages: pyrect, pyperclip, PyTweening, pyscreeze, pymsgbox, pygetwindow, mouseinfo, pyautogui
    Running setup.py install for pyrect: started
    Running setup.py install for pyrect: finished with status 'done'
    Running setup.py install for pyperclip: started
    Running setup.py install for pyperclip: finished with status 'done'
    Running setup.py install for PyTweening: started
    Running setup.py install for PyTweening: finished with status 'done'
    Running setup.py install for pygetwindow: started
    Running setup.py install for pygetwindow: finished with status 'done'
    Running setup.py install for mouseinfo: started
    Running setup.py install for mouseinfo: finished with status 'done'
    Running setup.py install for pyautogui: started
    Running setup.py install for pyautogui: finished with status 'done'
Successfully installed PyTweening-1.0.4 mouseinfo-0.1.3 pyautogui-0.9.53 pygetwindow-0.0.9 pymsgbox-1.0.9 pyperclip-1.8.2 pyrect-0.1.4 pyscreeze-0.1.28

Then we have to install web-browser module.

pip install webbrowser

We install the web-browser module and in most cases, it is preinstalled.

Then we write the main code.

import pyautogui
import webbrowser as wb
import time
wb.open("web.whatsapp.com")
time.sleep(60)
for i in range(500):
    pyautogui.press('H')
    pyautogui.press('i')
    pyautogui.press('!')
    pyautogui.press('enter')

This code upon running will automatically open Microsoft edge on your computer, with WhatsApp web.

You need to log in to your WhatsApp web, so make sure that you are putting time.sleep in line 5 to a value >= 60 seconds.

 

Result:

This Block of code will open your WhatsApp and then send 500 messages (Hi!) to a person whom you select. The default message will be Hi!.

How can we alter this code?

You can do the following modifications:

1. Change the sleep time to anything above 60. The more seconds you add, the longer it will take to generate automated messages.

2. You can change the message to anything you like but make sure to enter only a single character. 

Eg- If you want to print Hello, the following lines will be added.

pyautogui.press('H')

pyautogui.press('e')

pyautogui.press('l')

pyautogui.press('l')

pyautogui.press('o')

 

Make sure to open the chat of the targeted person when you open WhatsApp so that it can start sending messages after sleep time.

Hope it helps.

Download Complete Code

Comments

No comments yet