Files
antigravity-skills-reference/skills/whatsapp-cloud-api/assets/boilerplate/python/app.py
ProgramadorBrasil 61ec71c5c7 feat: add 52 specialized AI agent skills (#217)
New skills covering 10 categories:

**Security & Audit**: 007 (STRIDE/PASTA/OWASP), cred-omega (secrets management)
**AI Personas**: Karpathy, Hinton, Sutskever, LeCun (4 sub-skills), Altman, Musk, Gates, Jobs, Buffett
**Multi-agent Orchestration**: agent-orchestrator, task-intelligence, multi-advisor
**Code Analysis**: matematico-tao (Terence Tao-inspired mathematical code analysis)
**Social & Messaging**: Instagram Graph API, Telegram Bot, WhatsApp Cloud API, social-orchestrator
**Image Generation**: AI Studio (Gemini), Stability AI, ComfyUI Gateway, image-studio router
**Brazilian Domain**: 6 auction specialist modules, 2 legal advisors, auctioneers data scraper
**Product & Growth**: design, invention, monetization, analytics, growth engine
**DevOps & LLM Ops**: Docker/CI-CD/AWS, RAG/embeddings/fine-tuning
**Skill Governance**: installer, sentinel auditor, context management

Each skill includes:
- Standardized YAML frontmatter (name, description, risk, source, tags, tools)
- Structured sections (Overview, When to Use, How it Works, Best Practices)
- Python scripts and reference documentation where applicable
- Cross-platform compatibility (Claude Code, Antigravity, Cursor, Gemini CLI, Codex CLI)

Co-authored-by: ProgramadorBrasil <214873561+ProgramadorBrasil@users.noreply.github.com>
Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-07 10:04:07 +01:00

116 lines
3.0 KiB
Python

"""WhatsApp Cloud API - Flask Application with Webhook Handler."""
import asyncio
import os
from dotenv import load_dotenv
from flask import Flask, request, jsonify
from whatsapp_client import WhatsAppClient
from webhook_handler import (
validate_hmac_signature,
verify_webhook,
parse_webhook_payload,
extract_message_content,
)
load_dotenv()
app = Flask(__name__)
# Initialize WhatsApp client
whatsapp = WhatsAppClient()
# === Webhook Routes ===
@app.route("/webhook", methods=["GET"])
def webhook_verify():
"""Handle webhook verification (GET challenge from Meta)."""
return verify_webhook()
@app.route("/webhook", methods=["POST"])
@validate_hmac_signature()
def webhook_receive():
"""Handle incoming messages and status updates."""
data = request.get_json()
# Parse webhook payload
parsed = parse_webhook_payload(data)
# Process messages
for message in parsed["messages"]:
asyncio.run(handle_incoming_message(message))
# Process status updates
for status in parsed["statuses"]:
handle_status_update(status)
# Always return 200 within 5 seconds
return "OK", 200
# === Message Handler ===
async def handle_incoming_message(message: dict) -> None:
"""Process an incoming message and send a response."""
from_number = message["from"]
content = extract_message_content(message)
print(f"Message from {from_number}: [{content['type']}] {content.get('text', '')}")
# Mark as read
await whatsapp.mark_as_read(message["id"])
# TODO: Implement your message handling logic here
# Example: Echo back the message
match content["type"]:
case "text":
await whatsapp.send_text(from_number, f"Recebi sua mensagem: \"{content['text']}\"")
case "button":
await whatsapp.send_text(from_number, f"Voce selecionou: {content['text']}")
case "list":
await whatsapp.send_text(from_number, f"Voce escolheu: {content['text']}")
case "image" | "document" | "video" | "audio":
await whatsapp.send_text(from_number, f"Recebi sua midia ({content['type']}).")
case _:
await whatsapp.send_text(from_number, "Desculpe, nao entendi. Como posso ajudar?")
# === Status Handler ===
def handle_status_update(status: dict) -> None:
"""Process a message status update."""
print(f"Status update: {status['id']} -> {status['status']}")
if status["status"] == "failed":
errors = status.get("errors", [])
print(f"Message delivery failed: {errors}")
# === Health Check ===
@app.route("/health")
def health():
"""Health check endpoint."""
return jsonify({"status": "ok"})
# === Start Server ===
if __name__ == "__main__":
port = int(os.environ.get("PORT", 3000))
print(f"WhatsApp webhook server running on port {port}")
print(f"Webhook URL: http://localhost:{port}/webhook")
print(f"Health check: http://localhost:{port}/health")
app.run(host="0.0.0.0", port=port, debug=True)