Oh no! Where's the JavaScript?
Your Web browser does not have JavaScript enabled or does not support JavaScript. Please enable JavaScript on your Web browser to properly view this Web site, or upgrade to a Web browser that does support JavaScript.

WhatsApp AI Chatbot Sample Project

Last updated on 4 days ago
K
KevinMember
Posted 4 days ago
WhatsApp AI Chatbot Sample Project

Project Name: Smart WhatsApp Assistant

Goal:
Build a WhatsApp chatbot that answers user questions using an AI model (like ChatGPT or a custom-trained NLP model). You can extend it with features like FAQs, appointment booking, or product recommendations.

Tech Stack
* **WhatsApp Business API** or **Twilio WhatsApp API** (Twilio is simpler for personal/small-scale use)
* **Python** or **Node.js** backend
* **OpenAI GPT API** or Hugging Face models for AI responses
* **Flask / FastAPI** (Python) or **Express.js** (Node.js) for the server
* Optional: **MongoDB** or **Firebase** for user session storage

Basic Features
1. **WhatsApp integration** via Twilio sandbox
2. **AI responses** using OpenAI GPT (or other NLP models)
3. **Basic intents** like:
* Answering general questions
* Giving weather or news updates
* Handling simple tasks (e.g. “Set a reminder”)
4. **Session memory** (optional but improves continuity)

*High-Level Steps**

1. **Set up WhatsApp API:**

* Use [Twilio WhatsApp sandbox](https://www.twilio.com/whatsapp)
* Verify your number and set up a webhook URL

2. **Create backend server:**

* Use Flask or Express to listen for incoming messages

3. **Call AI model:**

* Use OpenAI’s GPT endpoint (/v1/chat/completions)
* Format incoming WhatsApp text and send it to GPT
* Return GPT response as WhatsApp reply

4. **Deploy and test:**

* Use tools like **Ngrok** during development to expose local server
* Deploy final version to **Render**, **Heroku**, or **AWS**
---
### 💬 **Example Interaction**
**User (via WhatsApp):**
> What's the weather like in London?
**Bot:**

> The weather in London today is mostly cloudy with a high of 18°C. ☁️
K
KevinMember
Posted 4 days ago
Before running the code:

1. **Create accounts**:

* [OpenAI](https://platform.openai.com/)
* [Twilio](https://www.twilio.com/whatsapp)

2. **Get your keys**:

* Twilio **Account SID**, **Auth Token**, and **WhatsApp sandbox number**
* OpenAI **API Key**

3. **Install dependencies**:

bash
 pip install flask openai twilio python-dotenv
 

WhatsApp + GPT Chatbot – app.py

python
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
import openai
import os
from dotenv import load_dotenv

load_dotenv() # Load .env file

# OpenAI API Key
openai.api_key = os.getenv("OPENAI_API_KEY")

app = Flask(__name__)

@app.route("/bot", methods=["POST"])
def bot():
 incoming_msg = request.values.get("Body", "").strip()
 print(f"Received: {incoming_msg}")

 # Create GPT response
 response = openai.ChatCompletion.create(
 model="gpt-3.5-turbo", # or gpt-4
 messages=[
 {"role": "system", "content": "You are a helpful WhatsApp assistant."},
 {"role": "user", "content": incoming_msg}
 ]
 )
 reply = response.choices[0].message['content'].strip()

 # Send reply via Twilio
 twilio_response = MessagingResponse()
 twilio_response.message(reply)
 return str(twilio_response)

if __name__ == "__main__":
 app.run(port=5000)


.env File

Create a .env file in the same directory:

env
OPENAI_API_KEY=your_openai_api_key_here


Twilio Setup

1. Go to Twilio Console → Programmable Messaging → WhatsApp
2. Use the sandbox number (e.g., whatsapp:+14155238886)
3. Set **Webhook URL** (e.g., using [ngrok](https://ngrok.com/)):

bash
 ngrok http 5000
 


Set the **inbound message webhook** to:


 https://your-ngrok-url.ngrok.io/bot
 

Test It

Send a WhatsApp message to your Twilio sandbox number like:

> Hello, what can you do?

And you’ll receive an AI-generated response via WhatsApp!
You can view all discussion threads in this forum.
You cannot start a new discussion thread in this forum.
You cannot reply in this discussion thread.
You cannot start on a poll in this forum.
You cannot upload attachments in this forum.
You cannot download attachments in this forum.
Sign In
Not a member yet? Click here to register.
Forgot Password?
Users Online Now
Guests Online 4
Members Online 0

Total Members: 17
Newest Member: apitech