import os import json import requests from typing import Dict, Optional, Any WACRM_URL = os.environ.get("WACRM_URL", "http://wacrm:3000") WACRM_API_KEY = os.environ.get("WACRM_API_KEY") def wacrm_send(to: str, type: str = "text", text: str = None, template: Dict = None, media_url: str = None) -> Dict[str, Any]: """ Plugin de Hermes para enviar mensajes via WACRM API. Uso desde skills: wacrm_send(to="+526641234567", type="text", text="Hola! Tu cita esta confirmada") Args: to: Numero de telefono destino (formato E.164: +52664...) type: Tipo de mensaje: 'text', 'template', 'image', 'document' text: Contenido del mensaje (para type='text') template: Dict con {name, language, params} (para type='template') media_url: URL del archivo (para type='image' o 'document') Returns: Dict con la respuesta de WACRM API """ if not WACRM_API_KEY: raise ValueError("WACRM_API_KEY no configurada en variables de entorno") payload = {"to": to, "type": type} if type == "text" and text: payload["text"] = text elif type == "template" and template: payload["template"] = template elif type in ("image", "document") and media_url: payload["media_url"] = media_url response = requests.post( f"{WACRM_URL}/api/v1/messages", headers={ "Authorization": f"Bearer {WACRM_API_KEY}", "Content-Type": "application/json" }, json=payload, timeout=30 ) if not response.ok: error_data = response.json() if response.content else {} raise Exception(f"WACRM send failed: {error_data.get('error', {}).get('message', response.status_text)}") return response.json() def wacrm_send_template(to: str, template_name: str, language: str = "es_MX", params: list = None) -> Dict[str, Any]: """ Atajo para enviar templates de WhatsApp aprobados. Uso: wacrm_send_template(to="+526641234567", template_name="recordatorio_cita_skeen", params=["Sofia", "5 de julio", "10:00 AM"]) """ if params is None: params = [] return wacrm_send( to=to, type="template", template={ "name": template_name, "language": language, "params": params } ) def wacrm_send_text(to: str, text: str) -> Dict[str, Any]: """ Atajo para enviar mensaje de texto simple. Uso: wacrm_send_text(to="+526641234567", text="Hola! Como estas?") """ return wacrm_send(to=to, type="text", text=text) # Registro del plugin para Hermes PLUGIN_NAME = "wacrm_send" PLUGIN_VERSION = "1.0.0" PLUGIN_DESCRIPTION = "Envio de mensajes WhatsApp via WACRM API" # Herramientas expuestas TOOLS = { "wacrm_send": wacrm_send, "wacrm_send_template": wacrm_send_template, "wacrm_send_text": wacrm_send_text, }