Skip to content

📧 Configuração SendGrid no Google Cloud

Variáveis de Ambiente Necessárias

Você precisa configurar as seguintes variáveis de substituição no Cloud Build Triggers:

1. SENDGRID_API_KEY

Valor atual (do .env):

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

2. SENDGRID_FROM_EMAIL

Valor:

noreply@theifriend.com

3. SENDGRID_FROM_NAME

Valor:

iFriend


Como Configurar no Google Cloud Console

Opção 1: Via Console Web

  1. Acesse: https://console.cloud.google.com/cloud-build/triggers
  2. Selecione seu trigger (ex: slack-bot-deploy)
  3. Clique em EDITAR
  4. Role até Variáveis de substituição
  5. Adicione as 3 variáveis:
_SENDGRID_API_KEY = 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
_SENDGRID_FROM_EMAIL = noreply@theifriend.com
_SENDGRID_FROM_NAME = iFriend
  1. Clique em SALVAR

Opção 2: Via gcloud CLI

# Configure o trigger com as variáveis SendGrid
gcloud builds triggers update [TRIGGER_NAME] \
  --substitutions=_SENDGRID_API_KEY="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",_SENDGRID_FROM_EMAIL="noreply@theifriend.com",_SENDGRID_FROM_NAME="iFriend"

Substitua [TRIGGER_NAME] pelo nome do seu trigger


Testando Localmente

Build Local com Docker

# Build da imagem
docker build -t ifriend-agent-slack -f Dockerfile.slack .

# Run com variáveis de ambiente
docker run -p 8080:8080 \
  -e SENDGRID_API_KEY="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" \
  -e SENDGRID_FROM_EMAIL="noreply@theifriend.com" \
  -e SENDGRID_FROM_NAME="iFriend" \
  -e SLACK_BOT_TOKEN="your-token" \
  -e SLACK_SIGNING_SECRET="your-secret" \
  ifriend-agent-slack

Teste Python Local (sem Docker)

# Ativar venv
source venv/bin/activate

# As variáveis já estão no .env, só rodar:
python slack_bot.py

Validando Instalação

1. Verificar se sendgrid está no requirements.txt

cat ifriend_agent/requirements.txt | grep sendgrid

Deve retornar:

sendgrid

2. Verificar se está instalado no venv

pip list | grep sendgrid

Deve retornar algo como:

sendgrid    6.12.5

3. Testar import

python -c "from sendgrid import SendGridAPIClient; print('✅ SendGrid OK')"

Verificando Deploy

1. Verificar variáveis de ambiente no Cloud Run

gcloud run services describe ifriend-agent-slack \
  --region=us-central1 \
  --format="value(spec.template.spec.containers[0].env)"

Deve incluir:

name: SENDGRID_API_KEY
value: SG.hH...
---
name: SENDGRID_FROM_EMAIL
value: noreply@theifriend.com
---
name: SENDGRID_FROM_NAME
value: iFriend

2. Verificar logs do Cloud Run

gcloud run services logs read ifriend-agent-slack \
  --region=us-central1 \
  --limit=50

Procure por: - ✅ [enviar_email_tool] Enviando email para: ... - ✅ [enviar_email_tool] ✅ Email enviado com sucesso! - ❌ No module named 'sendgrid' (erro a ser corrigido)


Troubleshooting

Erro: "No module named 'sendgrid'"

Causa: SendGrid não foi instalado durante o build Docker

Solução:

  1. Verificar se sendgrid está no ifriend_agent/requirements.txt
  2. Fazer rebuild da imagem Docker:
# Trigger manual do Cloud Build
gcloud builds submit --config=cloudbuild.slack.yaml

Erro: "SENDGRID_API_KEY não configurada"

Causa: Variável de ambiente não foi passada para o Cloud Run

Solução:

  1. Verificar se as variáveis estão no trigger do Cloud Build
  2. Re-deploy manual:
gcloud run deploy ifriend-agent-slack \
  --image gcr.io/[PROJECT_ID]/ifriend-agent-slack:latest \
  --region us-central1 \
  --set-env-vars SENDGRID_API_KEY="SG.hH...",SENDGRID_FROM_EMAIL="noreply@theifriend.com",SENDGRID_FROM_NAME="iFriend"

Erro: "Email não chega"

Causa: SendGrid API key inválida ou email não verificado

Solução:

  1. Verificar API key no SendGrid Dashboard: https://app.sendgrid.com/settings/api_keys
  2. Verificar sender authentication: https://app.sendgrid.com/settings/sender_auth
  3. Confirmar que noreply@theifriend.com está verificado no SendGrid

Checklist de Deploy

Antes de fazer deploy, confirme:

  • [ ] sendgrid está em ifriend_agent/requirements.txt
  • [ ] Variáveis _SENDGRID_* estão no Cloud Build Trigger
  • [ ] Variáveis estão no cloudbuild.slack.yaml (já está ✅)
  • [ ] Email noreply@theifriend.com está verificado no SendGrid
  • [ ] API key é válida e tem permissões de envio

Próximos Passos

Após configurar as variáveis:

  1. Commit e push:

    git add cloudbuild.slack.yaml
    git commit -m "feat: Adicionar suporte a envio de email via SendGrid"
    git push origin main
    

  2. Fazer deploy:

    gcloud builds submit --config=cloudbuild.slack.yaml
    

  3. Testar no Slack:

    @ifriend-agent Pode enviar um orçamento de tour em Paris para teste@exemplo.com
    


Última atualização: 6 de dezembro de 2025