Automates a predefined sales funnel on WhatsApp, integrated with Kommo (CRM), Asaas (payments), Google Drive (digital delivery), and Make (optional orchestration).
flowchart LR
WA[WhatsApp Cloud API] --> API[FastAPI app]
MK[Make.com scenarios] --> API
API --> Funnel[Funnel engine]
Funnel --> Kommo[Kommo CRM]
Funnel --> Asaas[Asaas payments]
Funnel --> Drive[Google Drive]
Asaas -->|webhook| API
| Component | Role |
|---|---|
app/funnel.py |
State machine driven by config/funnel.yaml |
app/integrations/kommo.py |
Contacts, leads, pipeline status, notes |
app/integrations/asaas.py |
Customers, payment links, payment status |
app/integrations/google_drive.py |
File metadata and delivery link |
app/integrations/whatsapp.py |
Outbound messages (Meta Cloud API) |
app/main.py |
Webhooks: WhatsApp, Make, Asaas |
Inbound messages can arrive via:
- Meta WhatsApp Cloud API →
POST /webhooks/whatsapp - Make.com HTTP module →
POST /webhooks/make(useful if WhatsApp is connected through Kommo/Make)
cd d:\AI_Projects\whatsapp_chatbot
python -m venv .venv
.venv\Scripts\activate
pip install -r requirements.txt
copy .env.example .env
# Edit .env with your API keys
python run.pyAPI docs: http://localhost:8000/docs
Expose locally with ngrok or similar and set WEBHOOK_BASE_URL to your public URL.
- Create a Meta Business app with WhatsApp product.
- Set
WHATSAPP_ACCESS_TOKEN,WHATSAPP_PHONE_NUMBER_ID,WHATSAPP_VERIFY_TOKEN. - Configure webhook URL:
{WEBHOOK_BASE_URL}/webhooks/whatsapp.
- Create a private integration: Kommo developers.
- Copy long-lived token →
KOMMO_ACCESS_TOKEN, subdomain →KOMMO_SUBDOMAIN. - In Kommo, note pipeline ID and status IDs for: new lead, payment pending, paid, delivered → map to
.env.
- API key from Asaas dashboard →
ASAAS_API_KEY. - Sandbox:
ASAAS_ENV=sandbox(default). - Webhook URL:
{WEBHOOK_BASE_URL}/webhooks/asaas
Events:PAYMENT_RECEIVED,PAYMENT_CONFIRMED. - Set
ASAAS_WEBHOOK_TOKENto match the token Asaas sends in headerasaas-access-token.
- Create a Google Cloud project, enable Drive API.
- Service account JSON → save as
credentials/google-service-account.json. - Share the product folder/file with the service account email.
- Set
GOOGLE_APPLICATION_CREDENTIALSandGOOGLE_DRIVE_FILE_ID(material to deliver).
Edit config/funnel.yaml — messages, menu options, and step flow. Placeholders:
{product_value}— fromPRODUCT_VALUE{payment_url}— after Asaas link is created{delivery_link}— Google Drive view link
Typical scenario:
- Trigger: Kommo new message / WhatsApp / webhook from provider.
- HTTP →
POST {your-server}/webhooks/make
Body example:
{
"phone": "5511999999999",
"text": "{{message.body}}",
"name": "{{contact.name}}"
}- Header:
X-Make-Secret: <MAKE_WEBHOOK_SECRET>if configured.
Use Make for retries, logging, or when WhatsApp is not connected directly to Meta API.
| Method | Path | Purpose |
|---|---|---|
| GET | /health |
Health check |
| GET/POST | /webhooks/whatsapp |
Meta verification + inbound messages |
| POST | /webhooks/make |
Generic inbound from Make |
| POST | /webhooks/asaas |
Payment confirmation → auto-delivery |
| GET | /payments/success |
Redirect after Asaas checkout |
- Customer chooses purchase → bot creates Asaas customer + payment link (
externalReference= phone). - Kommo lead moves to payment pending; note stores link.
- Asaas webhook fires on payment →
on_payment_confirmedsends Drive link on WhatsApp. - Customer can also type 3 (Já paguei) to poll Asaas and trigger delivery.
- Kommo lead updates to paid / delivered.
whatsapp_chatbot/
├── app/
│ ├── main.py # FastAPI webhooks
│ ├── funnel.py # Sales funnel logic
│ ├── config.py # Settings
│ ├── storage.py # SQLite sessions
│ └── integrations/
│ ├── whatsapp.py
│ ├── kommo.py
│ ├── asaas.py
│ └── google_drive.py
├── config/funnel.yaml
├── requirements.txt
├── .env.example
└── run.py
- Run behind HTTPS (required by Meta/Asaas webhooks).
- Use PostgreSQL/Redis instead of SQLite for high volume (swap
storage.py). - Add idempotency keys for webhooks (Asaas may retry).
- Restrict webhook IPs where providers document them.
- Never commit
.envor service account JSON.
Private client project — all rights reserved unless otherwise agreed.