Reemplaza Evolution API con bridge Node.js propio usando Baileys. QR se genera en ~10 segundos. Auto-reply con chatbot IA. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
"""WhatsApp service via Baileys Bridge (self-hosted, free).
|
|
|
|
Simple REST bridge at localhost:21465 that wraps WhatsApp Web via Baileys.
|
|
"""
|
|
|
|
import requests
|
|
from config import WHATSAPP_BRIDGE_URL
|
|
|
|
HEADERS = {'Content-Type': 'application/json'}
|
|
|
|
|
|
def get_status():
|
|
try:
|
|
return requests.get(f'{WHATSAPP_BRIDGE_URL}/status', timeout=5).json()
|
|
except Exception as e:
|
|
return {'state': 'error', 'error': str(e)}
|
|
|
|
|
|
def get_qr():
|
|
try:
|
|
return requests.get(f'{WHATSAPP_BRIDGE_URL}/qr', timeout=5).json()
|
|
except Exception as e:
|
|
return {'state': 'error', 'error': str(e)}
|
|
|
|
|
|
def connect():
|
|
try:
|
|
return requests.post(f'{WHATSAPP_BRIDGE_URL}/connect', headers=HEADERS, timeout=5).json()
|
|
except Exception as e:
|
|
return {'state': 'error', 'error': str(e)}
|
|
|
|
|
|
def send_message(phone, text):
|
|
try:
|
|
return requests.post(f'{WHATSAPP_BRIDGE_URL}/send', headers=HEADERS, json={'phone': phone, 'message': text}, timeout=15).json()
|
|
except Exception as e:
|
|
return {'error': str(e)}
|
|
|
|
|
|
def send_quote(phone, quote_data):
|
|
lines = [f"*Cotizacion #{quote_data.get('id', '')}*", ""]
|
|
for item in quote_data.get('items', []):
|
|
lines.append(f"- {item.get('quantity', 1)}x {item.get('name', '')} ${item.get('subtotal', 0):,.2f}")
|
|
lines.append(f"\nSubtotal: ${quote_data.get('subtotal', 0):,.2f}")
|
|
lines.append(f"IVA: ${quote_data.get('tax_total', 0):,.2f}")
|
|
lines.append(f"*Total: ${quote_data.get('total', 0):,.2f}*")
|
|
return send_message(phone, "\n".join(lines))
|
|
|
|
|
|
def logout():
|
|
try:
|
|
return requests.post(f'{WHATSAPP_BRIDGE_URL}/logout', headers=HEADERS, timeout=5).json()
|
|
except Exception as e:
|
|
return {'error': str(e)}
|
|
|
|
|
|
def process_incoming(webhook_data):
|
|
data = webhook_data.get('data', {})
|
|
key = data.get('key', {})
|
|
message = data.get('message', {})
|
|
return {
|
|
'phone': key.get('remoteJid', '').replace('@s.whatsapp.net', ''),
|
|
'text': message.get('conversation', '') or message.get('extendedTextMessage', {}).get('text', ''),
|
|
'from_me': key.get('fromMe', False),
|
|
'message_id': key.get('id', ''),
|
|
}
|