Skip to content

Instantly share code, notes, and snippets.

@BBloggsbott
Last active September 30, 2019 17:21
Show Gist options
  • Save BBloggsbott/24b66e759ab52f15ba1b7881080b76c8 to your computer and use it in GitHub Desktop.
Save BBloggsbott/24b66e759ab52f15ba1b7881080b76c8 to your computer and use it in GitHub Desktop.
Send messages in bulk using WhatsApp without having to save numbers.

WhatsApp Message Bulk sender

Set up selenium before using it. Save your message and numbers in a file.

Make sure your numbers are in the given format: <countrycode><number>. For example, the number +91-12345-67890 will be 911234567890.

Usage

The command line arguments for this script is:

usage: whatsapp_sender.py [-h] [-nf --numbersFile] [-mf --msgFile]
                          [-ns --numberSep] [-d [--delay]]

Send messages to users in bulk through WhatsApp

optional arguments:
  -h, --help         show this help message and exit
  -nf --numbersFile  Path to file with all the numbers
  -mf --msgFile      Path to file with the message
  -ns --numberSep    Seperator for the numbers
  -d [--delay]       Delay between each step. Adjust based on your internet
                     speed.

The -d or --delay parameter is optional and is set to 2 seconds by default.

from selenium import webdriver
# Set up selenium using instructions from here: https://selenium-python.readthedocs.io/installation.html
import time
import urllib
import argparse
import logging
logging.getLogger().setLevel(logging.INFO)
def send_messages(msg, numbers, delay):
logging.info("Encoding message")
msg = urllib.parse.quote(msg)
logging.info("Starting Driver.")
driver = webdriver.Firefox()
time.sleep(delay)
for cnt, i in enumerate(numbers):
logging.info("Sending to recepient {}.".format(cnt+1))
driver.get("https://api.whatsapp.com/send?phone={}&text={}".format(i, msg))
time.sleep(delay)
button = driver.find_element_by_class_name("button")
button.click()
time.sleep(delay)
link = driver.find_element_by_class_name("action__link")
link.click()
check = True
logging.info("Log in to Whatsapp Web if requested.")
while check:
try:
driver.find_element_by_class_name("_3PxOr")
except:
check = False
check = True
send = None
while check:
try:
send = driver.find_element_by_class_name("_3M-N-")
check = False
except:
check = True
time.sleep(delay)
send.click()
time.sleep(delay)
logging.info("Sent {} message.".format(cnt+1))
driver.quit()
if __name__=="__main__":
logging.info("Parsing Arguments")
parser = argparse.ArgumentParser(description="Send messages to users in bulk through WhatsApp")
parser.add_argument("-nf", metavar = "--numbersFile", type=str,
help="Path to file with all the numbers")
parser.add_argument("-mf", metavar = "--msgFile", type=str,
help="Path to file with the message")
parser.add_argument("-ns", metavar = "--numberSep", type=str,
help="Seperator for the numbers")
parser.add_argument("-d", metavar = "--delay", type=int,
help="Delay between each step. Adjust based on your internet speed.", nargs="?")
args = parser.parse_args()
if args.d == None:
args.d = 2
logging.info("Done.")
logging.info("Reading message.")
msgf = open(args.mf, 'r')
msg = msgf.read()
msgf.close()
logging.info("Done.")
logging.info("Reading numbers.")
nf = open(args.nf, 'r')
numbers = nf.read()
nf.close()
numbers = numbers.split(args.ns)
logging.info("Done.")
send_messages(msg, numbers, args.d)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment