SMS chatbot proof-of-concept using Flask + Twilio + NVIDIA NIM (OpenAI-compatible API).
- Twilio receives an inbound SMS and sends a webhook
POSTto this Flask app. - The app forwards the incoming message to NVIDIA NIM via the
openaiPython client. - The app returns a TwiML SMS response to Twilio.
- Twilio sends that response back to the sender's phone.
app.py: thin entrypointtxtai/__init__.py: Flask app factorytxtai/config.py: env/config loading and validationtxtai/routes/sms.py: webhook + health routestxtai/services/nim.py: NVIDIA NIM integrationtxtai/services/twilio_auth.py: Twilio signature validationtxtai/utils/text_sanitizer.py: markdown and emoji cleanupDockerfile: container image for Flask appdocker-compose.yml: app + ngrok stackscripts/start-with-ngrok.ps1: start stack and print ngrok HTTPS URL.env: local secrets/config (ignored by git).env-sample: config template
Copy .env-sample to .env and fill values:
TWILIO_AUTH_TOKEN(required only whenVERIFY_TWILIO_SIGNATURE=true)NVIDIA_API_KEYNVIDIA_BASE_URL(defaulthttps://integrate.api.nvidia.com/v1)NVIDIA_MODEL(pick a model available in your NVIDIA NIM account)SYSTEM_PROMPT(optional)VERIFY_TWILIO_SIGNATURE(truerecommended once webhook URL is fixed)PORT(default5000)NGROK_AUTHTOKEN(required for containerized ngrok)
Twilio setup values like account SID and phone number are configured in Twilio Console for webhook routing; they are not runtime-required by this app code.
# from project root
.\venv\Scripts\Activate.ps1
python app.pyServer endpoints:
- Health check:
http://localhost:5000/health - Twilio webhook:
http://localhost:5000/webhook/sms
In a second terminal:
ngrok http 5000Copy the HTTPS forwarding URL, e.g. https://abc123.ngrok-free.app.
Assuming Docker Desktop is installed and running:
# One-shot start: builds, starts, and prints webhook URL
powershell -ExecutionPolicy Bypass -File .\scripts\start-with-ngrok.ps1This command returns the final URL you should paste into Twilio:
https://<ngrok-domain>/webhook/sms
Manual docker commands:
docker compose up -d --buildCheck ngrok API directly for tunnels:
Invoke-RestMethod http://localhost:4040/api/tunnelsStop services:
docker compose down- Open Twilio Console -> Phone Numbers -> Manage -> Active numbers.
- Select your Twilio number.
- Under Messaging, set A message comes in to:
https://<your-ngrok-domain>/webhook/sms - Method:
HTTP POST. - Save.
Now text your Twilio number and you should receive an AI reply.
- If
VERIFY_TWILIO_SIGNATURE=true, Twilio request signatures are validated. - Replies are capped to ~1600 chars for SMS practicality.