0% found this document useful (0 votes)
155 views

Build A WhatsApp Bot Using Python - CodeWithCurious

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
155 views

Build A WhatsApp Bot Using Python - CodeWithCurious

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

/ projects, Python Projects / By Nikhil Kamble

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 .

Language And Interface :-


Build A WhatsApp Bot Using Python , A Versatile
Programming Language With Source
Code .

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.

How To Run The Code :-


Step
Step 1:
1: Set up the Twilio account using the
https://www.twilio.com/whatsapp
https://www.twilio.com/whatsapp .. Go to this link
and click on signup and start building button and fill in
your details and verify your email ID and mobile number.
Step
Step 22 :: A!er login, select the Develop option from the
le! menu and then further select the Messaging subject
then select the Try it out option, and in the last click on
Send a WhatsApp message. This will open up a new
webpage for setting up the WhatsApp Sandbox.
Step
Step 3:
3: Configure the Twilio WhatsApp Sandbox by
sending a message to this WhatsApp number with the
secret unique security code as shown in the below
images:
Send
Send the
the code
code as
as below
below format
format to
to the
the following
following
number:
number: +14155238886
+14155238886

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:

• To create and activate the python virtual environment:

• To install Twilio, flask and requests:

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.

Receiving message entered by the user and sending the


response. We can access theuser response that is coming
in the payload of the POST request with a key of ’Body’.

To Send Messages / respond to the user , we are going to


use MessagingResponse() function from Twilio .

So now we will build the chatbot logic, we are going to


ask the user to enter a topic that he/she want to learn
then we send the message to the bot .

Here, in this function, using user_msg will receive the


user response/query. Then we are using google search to
fetch the first 5 links from the Google search with the user
query and storing them into a list called result. A!er that,
are simply sending the first 5 links to the user using the
Twilio messaging service.

How To Run Bot :-

Firstly, run the above script using the following


command: python3
python3 main2.py
main2.py

Secondly, open up another terminal window and run the


following command to start the
ngrok server. ngrok
ngrok http
http 5000
5000

And third and last step we have to do is to set up the


forwarding URL in the WhatsApp
Sandbox Settings. Navigate to the following link and
paste the forwarding URL in the
selected location and click on save.
Link:
https://www.twilio.com/console/sms/whatsapp/sandbox

Source Code :
main.py

from flask import Flask


from googlesearch import search
import requests
from twilio.twiml.messaging_response
import MessagingResponse

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"

# list to store urls


result = []

# searching and storing urls


for i in search(q,
num_results=3):
result.append(i)

# 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 :

Find More Projects

Basic Logging System in


C++ With Source Code
 Nikhil Kamble • September 1, 2024 •
 C++ Projects, projects

Basic Logging System in C++ With Source


Code Introduction : It is one of the most
important practices in so!ware
development. Logging …

Snake Game Using C++


With Source Code
 Nikhil Kamble • September 1, 2024 •
 C++ Projects, projects

Snake Game Using C++ With Source Code


Introduction : Snake Game is a classical
arcade game where two snakes move
around the …

C++ Hotel Management


System With Source Code
 Nikhil Kamble • September 1, 2024 •
 C++ Projects, projects

C++ Hotel Management System With Source


Code Introduction : It is very important to
manage the hotels e"ectively to avail better
customer …

Student Management
System Using C++ With
Source Code
 Nikhil Kamble • September 1, 2024 •
 C++ Projects, projects

Student Management System Using C++


With Source Code Introduction : Student
Management System is an application that
provides all facilities for themaintenance …

Scraping Data From Google


Maps Using Python With
Source Code
 Nikhil Kamble • September 1, 2024 •
 projects, Python Projects

Scraping Data From Google Maps Using


Python With Source Code Introduction : In
this article, we are going to see how to …

Building A WhatsApp Bot


Using Python With Source
Code
 Nikhil Kamble • September 1, 2024 •
 projects, Python Projects

Building A WhatsApp Bot Using Python With


Source Code Introduction : In this article, we
are going to see how to create …

1 2 3 … 5 Next »

More Python Projects

Scraping Data From Google Maps


Using Python With Source Code

Building A WhatsApp Bot Using


Python With Source Code

Create a Screen Recorder Using


Python

Create a Telegram Bot Using


Python With Source Code

File Sharing App Using Python


With Source Code

21 Number Game Using Python


With Source Code

Automated Instagram Message


Sending Using Python With Source
Code

Joining Multiple Images To Display


Using Open CV Python With
Source Code

QR Code & Bar Code Detection


Using Open CV Python With
Source Code

Object Tracking Using Open CV


Python With Source Code

Resume Analyzer using Python


With Source Code

EMI Calculator Using Python With


Source Code

Typing Game Using Python With


Source Code

Tiles– A Sliding Puzzle Game in


Python

Spaceship Game Using Python

Simon Says– Classic Memory


Puzzle Game Using Python

Memory Game Using Python: A


CardMatching Game

Optical Illusion Game with Python

Creating a Connect Four Game


Using Python

Cannon Game Using Python

Build Your Own Paint App with


Python

Build a Pacman Game with Python

Blackjack Game Using Python

Aeroblasters: 2D Vertical Plane


Shooter Game Using Python and
Pygame

A Simple Maze Drawing Game in


Python

OMR MCǪ Automated Grading


Using Open CV Python

Shape Detection Using Open CV


Python

Text Detection Using Open CV


Python With Source Code

Object Measurement Using Open


CV Python With Source Code

Realtime Colour Detection Using


Open CV Python With Source Code

Volume & Brightness Control


Using Open CV Python

Vehicle Speed Estimation Using


Open CV Python With Source Code

CAPTCHA Generator Using Python

Google Search Clone using Python

Hostel Management System Using


Python with source code Python
Graphical Interface Tkinter

contra game using python pygame


with source code

Restaurant billing system using


Python with Tkinter Module with
GUI

Jungle Dash Game Using Python


With source code using Pygame

Tetris Game using Python pygame


with source code

Super Mario Game using Python


with source code

library management system using


python with source code using
Python GUI Tkinter (Graphical
User Interface)

space shooter game using python


with source code

Hotel Management System Using


Python with source code using
Graphical User interface GUI

Student Management System


using python with source code

Billing So!ware Using Python with


source code

Hospital Management Using


Python with source code

2048 game using python tkitner

Valentine Day project using


Python turtle

Drawing Rose Using Python Turtle


with python turtle graphics

Binary to decimal & decimal to


binary converter

Build mini projects in Python- Fast


and Simple

Make weather app using Django

Make an Indian flag using Turtle


Python

Make notepad using python


Tkinter

Get Info of phone number using


python

Car Racing Game Using Python

Convert Image to PDF Using


Python GUI

Library management using python


GUI

Creating GUI Piano Using Python

Get Instagram Account Details


using Python

Draw Pikachu with Python

You might also like