How to send bulk messages to multiple numbers on WhatsApp with pywhatkit (python)
There are various ways to send bulk messages
on WhatsApp to your customers, but this is an automatic way to send messages to
them at the right time without lifting a finger. Well, you’ve lifted the finger
by adjusting the code,but it's free.
You can use WhatsApp broadcast, but as we all
know, there is a limit.
There are paid apps on the Play Store, but if
you're starting a business as a beginner, you don’t have money for them.
You can code with WhatsApp's API, but that still
requires money.
And for me, selenium is complicated.
Hence, this method
This requires your phone numbers to be in an
excel list. You can use any app to do that; the one I used is on the Play
Store. It's called Contacts to Excel, PDF, and Text. It's completely free, and
it converts your contact list to an excel file.
You will need to login to WhatsApp Web to use
this code.
The IDE I used is Visual Studio Code, and the
programming language used is Python.
Pywhatkit explanation:
enables thePywhatkit is a Python library that
enables automation of WhatsApp messages. It’s simple to use and offers features
such as sending messages or images to individuals or groups, playing YouTube
videos, and even converting text to handwriting. It is one of the most popular
libraries for WhatsApp and YouTube automation.
Code
explanation:
Import pywhatkit # pip install pywhatkit
This is a line of code in python that helps
import the pywhatkit library. You have to download pywhatkit first before you
can use it. To download pywhatkit, type pip install pywhatkit in your
terminal".
Import datetime
This is a line of code in python that helps
you download datetime with pip install datetime. This accesses the current time
in your current location.
Import keyboard as k #pip install keyboard
This is a line of code in python that helps
you download keyboards with pip install keyboard. This is used to click the
send button on whatsapp.
Import time
This is a line of code in python that helps
you don’t need to install python; it comes with python.
Import pandas as pd
This is a line of code in python that helps
you import pandas with pip install pandas. It is used to access excel to see
where your phone numbers are located.
phones= []
This line of code in python is used to hold
the phone numbers for people with multiple numbers on excel.
Numbers = []
In this line of code in python, the name
"numbers" holds an empty list, and that empty list is going to hold
our contacts.
# read the excel file
df = pd.read_excel('exceltest.xlsx')
This is a line of code in python that reads
the excel file named exceltest. The file exceltest is stored in the same folder
as my code, and i opened the folder with my ide. Just make sure they are in the
same place.
for index, row in df.iterrows():
This is a line of code in python that sends
messages in a loop.
area_code = "+234"
This is a line of code in python. Replace it
with your area code.
contact_number = str([row['number'])
This is a line of code in python where
"row ‘number’ " searches for a row with the letter number in your
excel file, so make sure the column containing your contacts is labeled numbers
or change numbers to the letter at the top of the column with your contact.
’Str() converts the contact number to a string and stores it in a variable
labeledcontact_number.
print(contact_number)
This is a line of python code that displays
the number on the terminal.
If contact_number. Startswith("0"):
This is a line of python code where the loop
checks if the contact starts with 0.
contact_number = area_code + contact_number [1:]
In this is a line of code in python that, if
the number starts with 0, is going to remove the 0 and add your area code to
the number.
If ',' in contact_number:
In
this is a line of code in python. I added this if loop because some of the
numbers in my contact column had commas because some people have more than one
number. But it checks if the contact has a comma in it.
phones = contact_number.split(',')
This line of code in python takes a variable
contact_number and splits it into a list of values using the split() method.
The split() method takes a delimiter as an argument, in this case, a comma
(","). The resulting list of substrings is then assigned to the variable
p.
for p in phones:
In this is a line of code in python where the
for loop loops through the list of phones.
If p.startswith("0"):
In this is a line of code in python if any
number in the list starts with 0.
p = area_code + p [1:]
In this line of code in python, the area code
is added to the number and 0 is removed.
else:
Numbers.append(p)
In this is a line of code in python if the
contact is not added to the list of numbers.
else:
contact_number = contact_number
In this is a line of code in python where the
contact number is left alone if it doesn’t fall into any of the if conditions.
Numbers.append(contact_number)
In this line of code in python, the numbers
are added to the list of numbers declared above.
print(numbers)
print("started")
In this is a line of code in python that prints
the number and starts on the terminal.
This is a line of python code that loops
through each number (contacts). It will open a new tab for every contact. Don't
worry if you have time, just close them.
for i in range(len(numbers)):
This is a line of python code that loops
through the number of items in the list.
pywhatkit.sendwhatmsg(numbers[i], happy new
month', datetime.datetime.now().hour,datetime.datetime.now().minute+1,32)
In this is a line of code in python. This format
is compulsory to use: pywhatkit.sendwhatmsg(). It is a function that helps send
the message. It accepts the phone number to be sent, the message to be sent,
and the time in hours and minutes. The function datetime was used to make it
automatically use the current time, and +1 was added to make it send in 1
minute time. Because pywhatkit needs at least 1 minute to prepare to send your
message, the time format of 32 seconds is the timing for delivering your
message after opening whatsapp web.
k.press_and_release('enter')
This line of code in python helps you press
enter to send the message.
Print("whole job done..")
code:
import pywhatkit # pip install pywhatkit
import datetime
import keyboard as k #pip install keyboard
import pandas as pd
numbers = []
# Read the Excel file
df = pd.read_excel('exceltest.xlsx')
Send messages in a loop.
for index, row in df.iterrows():
area_code = "+234"
contact_number = str(row['Number'])
print(contact_number)
if contact_number.
startswith("0"):
contact_number = area_code + contact_number
[1:]
if ',' in contact_number:
phones = contact_number.split(',')
for p in phones:
if
p.startswith("0"):
p = area_code + p[1:]
else:
numbers.append(p)
else:
contact_number = contact_number
numbers.append(contact_number)
print(numbers)
print("Started")
for i in range(len(numbers)):
pywhatkit.sendwhatmsg(numbers[i],'happy
new month',
datetime.datetime.now().hour,datetime.datetime.now().minute+1,32)
k.press_and_release('enter')
print("Whole Job Done..")
Picture of the code running
Picture of the code
End of code
No comments: