Build A WhatsApp Bot Using Python - CodeWithCurious
Build A WhatsApp Bot Using Python - CodeWithCurious
Building A WhatsApp
Bot Using Python With
Source Code
Introduction :
In this article, we are going to see how to create a
WhatsApp Bot using Python. A WhatsApp bot is
application so!ware that is able to carry on
communication with humans in a spoken or written
manner. And today we are going to learn how we can
create a WhatsApp bot using python. First, let’s see the
requirements for building the WhatsApp bot using python
language.
A Whatsapp Bot can send and receive messages and
perform tasks like sending notifications , etc . To build
this you should use Twilio API which provides the
necessary tools to connect your Python script to
Whatsapp . In This we will use the Python library which is
named as Flask . It is used to handle the incoming and
outgoing messages .
Required Modules Or
Packages :-
1. A Twilio account and a smartphone with an active
phone number and WhatsApp installed.
2. Must have Python 3.9 or newer installed in the system.
3.
3. Flask:
Flask: We will be using a flask to create a web
application that responds to incoming WhatsApp
messages with it.
4.
4. Ngrok:
Ngrok: Ngrok will help us to connect the Flask
application running on your system to a public URL that
Twilio can connect to. This is necessary for the
development version of the chatbot because your
computer is likely behind a router or firewall, so it isn’t
directly reachable on the Internet.
secret
secret code
code :: join
join <secret-code>
<secret-code>
Step
Step 4:
4: Now, send the secret code to the above
WhatsApp message and you will receive a confirmation
message .
Step
Step 5:
5: Open up the terminal and run the following
command to create a directory for the bot, to create a
virtual environment for python, to install all the
necessary packages.
• To create the directory and navigate to that directory:
Code Explaination :-
This Python script makes the WhatsApp Bot. Ensures that
You Have Downloaded the modules given above . Import
the necessary files needed to run this flask app.
Source Code :
main.py
app = Flask(__name__)
@app.route("/", methods=["POST"])
# chatbot logic
def bot():
# user input
user_msg =
request.values.get('Body',
'').lower()
# creating object of
MessagingResponse
response = MessagingResponse()
# User Query
q = user_msg +
"geeksforgeeks.org"
# displaying result
msg = response.message(f"---
Results for '{user_msg}' ---")
for result in search_results:
msg =
response.message(result)
return str(response)
if __name__ == "__main__":
app.run()
Output :
Student Management
System Using C++ With
Source Code
Nikhil Kamble • September 1, 2024 •
C++ Projects, projects
1 2 3 … 5 Next »