An AI-powered WhatsApp chatbot that answers finance-related queries using OpenAI's function calling and tool integrations. Built with FastAPI, Twilio, LangChain-style prompt workflows, and PostgreSQL — deployed on Render.com.
- ✅ Answers finance-related questions via WhatsApp using OpenAI responses
- ✅ Detects intent (stock/market queries) and triggers real-time web search tool
- ✅ Stores full chat history in PostgreSQL via SQLAlchemy ORM
- ✅ Handles multi-part message delivery with auto-chunking
- ✅ Formats AI responses for clean, readable WhatsApp rendering
- ✅ Runs on FastAPI with Twilio WhatsApp integration
- ✅ Logging, error handling, retry logic, and user state tracking via
response_id - ✅ Fully deployed and live on Render (API + DB)
- FastAPI – Backend API framework
- OpenAI SDK – Language + tool-calling responses
- Twilio SDK – WhatsApp integration
- SQLAlchemy + PostgreSQL – Persistent message logging
- Alembic – DB migrations
- Render.com – Prod deployment for API + DB
- python-decouple – Secure environment configuration
- Python 3.9+
- A Twilio account with WhatsApp sandbox access
- An OpenAI API key
- A PostgreSQL instance (local or cloud-hosted)
- (Optional) Ngrok for local webhook testing
- Clone the repo
git clone https://github.com/nawazkhan/ai-finance-chatbot.git
cd ai-finance-chatbot- Create a
.envfile
# OpenAI
OPENAI_API_KEY=your-openai-key
OPENAI_MODEL=text-completion-model-id
# Twilio
TWILIO_ACCOUNT_SID=your-sid
TWILIO_AUTH_TOKEN=your-auth-token
TWILIO_NUMBER=
# PostgreSQL
DB_DRIVER=postgresql
DB_USER=your_user
DB_PASSWORD=your_password
DB_HOST=localhost
DB_PORT=5432
DB_NAME=your_db
- Install dependencies
pip install -r requirements.txt- Run database migrations
alembic upgrade head- Start the server
uvicorn main:app --reload- Expose locally with ngrok (for Twilio)
ngrok http 8000- Configure Twilio webhook to:
https://your-ngrok-url.ngrok.io/message
Deployed on Render.com with:
- FastAPI backend on a Render web service
- PostgreSQL DB as a managed Render database
- Auto-deploy from GitHub + environment variables configured via dashboard
Built by Nawaz Khan
Email: [email protected]
MIT License – open for use and modification with attribution