2026-03-06 17:16:31 -03:00
|
|
|
# Configuração WhatsApp - Bot Salesianos
|
|
|
|
|
|
|
|
|
|
## Status Atual
|
|
|
|
|
|
|
|
|
|
| Campo | Valor | Status |
|
|
|
|
|
|-------|-------|--------|
|
|
|
|
|
| Phone Number | +15558293147 | ✅ |
|
|
|
|
|
| Phone Number ID | 323250907549153 | ✅ Configurado |
|
|
|
|
|
| Business Account ID | 1261667644771701 | ✅ Configurado |
|
|
|
|
|
| APP ID | 948641861003702 | ✅ |
|
|
|
|
|
| Client Token | 84ba0c232681678376c7693ad2252763 | ⚠️ Temporário |
|
|
|
|
|
| API Key (Permanent Token) | EAAQdlso6aM8B... (configured) | ✅ Configurado |
|
|
|
|
|
| Verify Token | webhook_verify_salesianos_2024 | ✅ Configurado |
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
## ✅ Problemas Resolvidos (2026-03-06)
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
### Problema 1: Mensagens ignoradas pelo bot (RESOLVIDO)
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
**Sintoma**: Mensagens eram recebidas mas ignoradas pelo bot (query vazia no KB)
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
**Causa raiz**: JSON deserialization estava falhando - array `messages` aparecia vazio
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
**Solução aplicada**:
|
|
|
|
|
- ✅ Adicionado debug logging em `handle_webhook()` e `process_incoming_message()`
|
|
|
|
|
- ✅ Verificado estrutura do payload JSON
|
|
|
|
|
- ✅ Testado com script de simulação
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
**Resultado**: Mensagens agora são processadas corretamente
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
### Problema 2: Listas duplicadas/multipartes (RESOLVIDO)
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
**Sintoma**: Listas (li/ul) eram enviadas em chunks separados, causando duplicação
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
**Causa raiz**: Lógica de streaming enviava mensagens em pedaços durante a geração
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
**Solução aplicada**:
|
|
|
|
|
- ✅ Simplificado streaming em `botserver/src/whatsapp/mod.rs:597-623`
|
|
|
|
|
- ✅ Removido chunking - agora acumula todo conteúdo antes de enviar
|
|
|
|
|
- ✅ Mensagem só é enviada quando `is_final = true`
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
**Resultado**: Listas e todo conteúdo enviado como uma mensagem completa
|
2026-03-06 17:16:31 -03:00
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
## Código Modificado
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
### Arquivo: `botserver/src/whatsapp/mod.rs`
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
**Função**: `route_to_bot()` - Streaming simplificado
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
```rust
|
|
|
|
|
tokio::spawn(async move {
|
|
|
|
|
let mut buffer = String::new();
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
while let Some(response) = rx.recv().await {
|
|
|
|
|
let is_final = response.is_complete;
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
if !response.content.is_empty() {
|
|
|
|
|
buffer.push_str(&response.content);
|
|
|
|
|
}
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
// Only send when the complete message is ready
|
|
|
|
|
// This ensures lists and all content are sent as one complete message
|
|
|
|
|
if is_final && !buffer.is_empty() {
|
|
|
|
|
let mut wa_response = response;
|
|
|
|
|
wa_response.user_id.clone_from(&phone);
|
|
|
|
|
wa_response.channel = "whatsapp".to_string();
|
|
|
|
|
wa_response.content = buffer.clone();
|
|
|
|
|
wa_response.is_complete = true;
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
if let Err(e) = adapter_for_send.send_message(wa_response).await {
|
|
|
|
|
error!("Failed to send WhatsApp response: {}", e);
|
|
|
|
|
}
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
buffer.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
```
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
**Mudanças principais**:
|
|
|
|
|
- ❌ Removido: `MIN_CHUNKS_TO_SEND`, `chunk_count`, `in_list`, `list_indentation`
|
|
|
|
|
- ✅ Adicionado: Buffer simples, envio único quando `is_final`
|
2026-03-06 17:16:31 -03:00
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
## Fase 1: Configuração Básica ✅
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
- [x] **Obter Permanent Access Token** - ✅ CONFIGURADO
|
|
|
|
|
- [x] **Verificar config.csv atual** - ✅ TODOS OS CAMPOS CONFIGURADOS
|
|
|
|
|
- Arquivo: `/opt/gbo/data/salesianos.gbai/salesianos.gbot/config.csv`
|
|
|
|
|
- Campos: `whatsapp-phone-number-id`, `whatsapp-business-account-id`, `whatsapp-api-key`, `whatsapp-verify-token`
|
|
|
|
|
|
|
|
|
|
---
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
## Fase 2: Configuração do Webhook (PENDENTE PRODUÇÃO)
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
- [ ] **Configurar webhook na Meta Business Suite**
|
|
|
|
|
- URL de produção: `https://<seu-dominio>/webhook/whatsapp/<bot_id>`
|
|
|
|
|
- Verify Token: `webhook_verify_salesianos_2024`
|
|
|
|
|
- Subscrever eventos: `messages`, `messaging_postbacks`
|
|
|
|
|
|
|
|
|
|
- [ ] **Verificar se webhook está acessível externamente**
|
|
|
|
|
- Configurar reverse proxy (nginx/traefik)
|
|
|
|
|
- Configurar SSL/TLS (obrigatório para produção)
|
|
|
|
|
|
|
|
|
|
- [ ] **Testar verificação do webhook**
|
2026-03-06 17:16:31 -03:00
|
|
|
```bash
|
2026-03-07 07:46:49 -03:00
|
|
|
curl "https://<seu-dominio>/webhook/whatsapp/<bot_id>?hub.mode=subscribe&hub.challenge=test&hub.verify_token=webhook_verify_salesianos_2024"
|
2026-03-06 17:16:31 -03:00
|
|
|
```
|
|
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Fase 3: Testes
|
|
|
|
|
|
|
|
|
|
### Teste Local ✅
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
- [x] **Script de teste**: `/tmp/test_whatsapp_webhook.sh`
|
|
|
|
|
- [x] **Webhook local funcionando**: `http://localhost:8080/webhook/whatsapp/<bot_id>`
|
|
|
|
|
- [x] **Extração de conteúdo**: Funcionando
|
|
|
|
|
- [x] **Streaming de listas**: Corrigido
|
2026-03-06 17:16:31 -03:00
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
### Teste Produção (PENDENTE)
|
|
|
|
|
|
|
|
|
|
- [ ] **Testar com mensagem real do WhatsApp**
|
|
|
|
|
- Enviar mensagem para +15558293147
|
|
|
|
|
- Verificar se resposta vem completa (sem duplicação)
|
|
|
|
|
|
|
|
|
|
### Comandos de Debug
|
2026-03-06 17:16:31 -03:00
|
|
|
|
|
|
|
|
```bash
|
|
|
|
|
# Ver mensagens WhatsApp em tempo real
|
|
|
|
|
tail -f botserver.log | grep -iE "(whatsapp|Extracted|content)"
|
|
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
# Testar webhook localmente
|
2026-03-06 17:16:31 -03:00
|
|
|
/tmp/test_whatsapp_webhook.sh
|
|
|
|
|
|
|
|
|
|
# Verificar configuração do bot
|
|
|
|
|
cat /opt/gbo/data/salesianos.gbai/salesianos.gbot/config.csv | grep whatsapp
|
2026-03-07 07:46:49 -03:00
|
|
|
|
|
|
|
|
# Verificar saúde do servidor
|
|
|
|
|
curl http://localhost:8080/health
|
2026-03-06 17:16:31 -03:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
## Fase 4: Produção
|
2026-03-06 17:16:31 -03:00
|
|
|
|
|
|
|
|
- [ ] **Configurar SSL/TLS**
|
|
|
|
|
- Certificado válido para o domínio
|
|
|
|
|
- HTTPS obrigatório para webhooks
|
|
|
|
|
|
|
|
|
|
- [ ] **Rate Limiting**
|
|
|
|
|
- Verificar limites da API do WhatsApp
|
|
|
|
|
- Implementar throttling se necessário
|
|
|
|
|
|
|
|
|
|
- [ ] **Monitoramento**
|
|
|
|
|
- Alertas para falhas de webhook
|
|
|
|
|
- Logs estruturados
|
|
|
|
|
|
|
|
|
|
- [ ] **Backup do config.csv**
|
|
|
|
|
- Salvar configurações em local seguro
|
|
|
|
|
- Documentar credenciais (exceto secrets)
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Referências
|
|
|
|
|
|
|
|
|
|
- [WhatsApp Business API Docs](https://developers.facebook.com/docs/whatsapp/business-platform-api)
|
|
|
|
|
- [Meta Business Suite](https://business.facebook.com/)
|
|
|
|
|
- Arquivo de config: `/opt/gbo/data/salesianos.gbai/salesianos.gbot/config.csv`
|
2026-03-07 07:46:49 -03:00
|
|
|
- Webhook handler: `botserver/src/whatsapp/mod.rs`
|
|
|
|
|
- Test script: `/tmp/test_whatsapp_webhook.sh`
|
2026-03-06 17:16:31 -03:00
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Notas
|
|
|
|
|
|
2026-03-07 07:46:49 -03:00
|
|
|
- **Client Token** fornecido é temporário - necessário Permanent Access Token ✅ OBTIDO
|
|
|
|
|
- Token permanente armazenado no config.csv
|
|
|
|
|
- Webhook local funcionando - pendente configuração de produção
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
## Próximos Passos
|
|
|
|
|
|
|
|
|
|
1. [ ] Testar com mensagens reais do WhatsApp
|
|
|
|
|
2. [ ] Configurar webhook na Meta Business Suite para produção
|
|
|
|
|
3. [ ] Configurar SSL/TLS no servidor de produção
|
|
|
|
|
4. [ ] Monitorar logs em produção
|
|
|
|
|
5. [ ] Documentar processo de deploy
|