Hello there!
Today on Python weekly, We will be creating Whatsapp bot using pywhatkit.
A Little About PyWhatKit
Before we start, let me tell you about pywhatkit.
PyWhatKit is a Python library with various helpful features. It is an easy-to-use library that does not require you to do some additional setup. It can be used to:
Send WhatsApp messages.
Play a YouTube video.
Perform a Google Search.
Get information on a particular topic.
Converting text into handwritten text images.
Now that is you know what pywhatkit is.
Let's start coding ๐
Create and Activate your virtual environment.
Never forget to create and activate your virtual environment.
Why? Virtual environments create an isolated environment for Python projects.
Install PyWhatKit
This library can be installed by the pip command, open your command prompt, and type in the following command.
pip3 install pywhatkit
Create a .py file and start calling the functions
First, import the library using the command import pywhatkit and then proceed to call the functions
import pywhatkit
Add DateTime module
The DateTime module supplies classes for manipulating dates and times. This is a python package so no need to install it.
import pywhatkit
from DateTime import datetime
Your code should look like this.
A date in Python is not a data type of its own, but we can import a module named DateTime to work with dates as date objects.
today = datetime.now()
This displays the current date and time
Convert DateTime object to an equivalent string
shour = today.strftime("%H")
In the above program, %H is a format code. The strftime() method takes the format code as an argument and returns a formatted string based on it.
%H: Is a format code for hour (24-hour clock) as a decimal number.
Add information to message a Whatsapp number
mobile_no = input('Enter Mobile Number of Receiver: ')
message = input('Enter Message you want to send : ')
Add Minute and Hour you want to send your message
hour = int(input('Enter hour : '))
minute = int(input('Enter minute : '))
Finally, send the message
pywhatkit.sendwhatmsg(mobile_no,message,hour,minute)
This function can be used to send WhatsApp messages at a certain time.
.sendwhatmsg_instantly() can be used to send message instantly.
While kit.sendwhatmsg_to_group() can be used to send messages to a group.
Your code should look like this
import pywhatkit
from datetime import datetime
today = datetime.now()
shour = today.strftime("%H")
mobile_no = input('Enter Mobile Number of Receiver: ')
message = input('Enter Message you want to send : ')
hour = int(input('Enter hour : '))
minute = int(input('Enter minute : '))
pywhatkit.sendwhatmsg(mobile_no,message,hour,minute)
That's it, guys. You've just created your first Whatsapp bot using pywhatkit. I know it's a funny name ๐
The project is on Github here is the link:
github.com/Sophyia7/whatsapp-bot.git, if you have any issues let me know.
Let's Connect
Thanks for staying till the end โค๏ธ. Let's connect via
Email - iroegbusophia3@gmail.com
Discord - Sophyia#8929