Minimal FastAPI service to verify a 360dialog webhook, receive WhatsApp messages, store them in Postgres, and optionally echo replies.
- FastAPI
- Postgres (psycopg2-binary)
- Docker + docker-compose
- Env-driven config via
.env
# 360dialog
D360_API_KEY=your-360dialog-api-key
BASE_URL=https://waba.360dialog.io
PHONE_NUMBER_ID=your-phone-number-id # optional, not required for echo
VERIFY_TOKEN=change-me # used for GET /webhook verification
# Postgres
POSTGRES_USER=app
POSTGRES_PASSWORD=app
POSTGRES_DB=app
POSTGRES_HOST=db
POSTGRES_PORT=5432
Notes:
- The app reads env via Pydantic settings (
app/config.py
), mapping tosettings
fields. - For local dev,
.env
is loaded automatically by the app.
cd /Users/naveensabariguru/BTC/chatbot
docker compose up --build
Service: http://localhost:8000
(health endpoint is not implemented; use /webhook
for verification).
- Verification (GET):
- URL:
https://<public-domain>/webhook
- Params:
hub.mode=subscribe&hub.verify_token=change-me&hub.challenge=123
- The service will echo
hub.challenge
if the token matchesVERIFY_TOKEN
.
- URL:
- Messages (POST):
https://<public-domain>/webhook
Expose your local server via ngrok or cloudflared.
Send a sample message event:
curl -X POST http://localhost:8000/webhook \
-H 'Content-Type: application/json' \
-d '{
"contacts": [{"wa_id": "14155550123"}],
"messages": [{"from": "14155550123", "type": "text", "text": {"body": "Hi"}}],
"entry": [{"changes": [{"value": {"messages": [{"from": "14155550123", "type": "text", "text": {"body": "Hi"}}]}}]}]
}'
- On startup, the app will create a
messages
table if not present (app/db.py
). - It stores
sender
,message
, and acreated_at
timestamp for each incoming text.
app/main.py
: FastAPI app, GET/webhook
verification, POST/webhook
receipt, echo and DB insert.app/config.py
: Pydantic settings reading env keys.app/db.py
: psycopg2 connection and init.Dockerfile
,docker-compose.yml
: containerization and Postgres service.
- Use a production ASGI server command (remove
--reload
). - Use a secrets manager for env vars.
- Add request validation and signature verification if needed.
- Consider async HTTP client and DB access for higher throughput.