Before we start coding a spam bot, we first need to install python with pip. If you haven’t done that already you can follow the instructions here.
After that part is done, we need to install the python keyboard package. To do so, open the terminal on Linux and MacOS or the command prompt on Windows. In the window that now appears, type in pip install keyboard.
Now that everything is set up, we need to create a python file by simply creating a new file with the extension .py, e. g. spambot.py. To begin with the coding part, just open up that file in a code editor of your choice. I am using Visual Studio Code for this tutorial.
Coding the spam bot
You can find the full source code here.
The first thing we need to do is import some modules. To do so, write the following lines into your python file.
import keyboard import sys import time
We use the keyboard module for automatically typing into text fields. The sys module we use for command line arguments, and the time module is used for setting the spam delay.
Next, we define some variables and functions to handle the hotkey that starts and stops the bot.
# hotkey state pressed = False # define hotkey pressed method def setPressed(): global pressed pressed = not pressed # define hotkey keyboard.add_hotkey("alt + a", setPressed) # you can change this to any other key combination
After that, we include the last part. It is responsible for checking the provided command line arguments and spamming when the hotkey is pressed.
currentTime = 0.0 if len(sys.argv) < 2: print("Usage: py spambot.py <message> [ -d <delay in seconds> | -t <exit time in seconds> ]") # Provide the user with usage information sys.exit(1) delay = 0.25 exitTime = None if "-d" in sys.argv: try: if float(sys.argv[sys.argv.index("-d") + 1]) >= 0.01: delay = float(sys.argv[sys.argv.index("-d") + 1]) else: print("Delay can't be lower than 0.01") sys.exit(1) except: print("Only float values are allowed for delay attribute!") # Exit if the user didn't provide a valid number sys.exit(1) if "-t" in sys.argv: try: exitTime = float(sys.argv[sys.argv.index("-t") + 1]) except: print("Only float values are allowed for exit time attribute!") # Exit if the user didn't provide a valid number sys.exit(1) msg = str(sys.argv) + "\n" if exitTime is not None: exitTime -= delay while True: while pressed: # When the user has pressed the hotkey if exitTime is not None: # if we've got an exit time, exit when it's reached if currentTime <= exitTime: keyboard.write(msg) time.sleep(delay) currentTime += delay else: exit(code=0) else: # otherwise spam until the user presses the hotkey again keyboard.write(msg) time.sleep(delay)
Done! That’s how you code an advanced spam bot in Python.
To now finally use the spam bot, you have to again open your terminal and then change the directory to the path where you created your file. You can do so by typing cd <Your Directory Path>. Then type:
python spambot.py <message to spam> [ -d <spam delay in seconds> | -t <exit time in seconds>]
Replace spambot.py with your own file name and provide the command line arguments. The arguments in the squared brackets () are optional. If you want to spam a message with spaces, put it in quotation marks.
To start/stop spamming, press the hotkey alt + a.
Thanks for reading!