|
|
@@ -1,4 +1,6 @@
|
|
|
import logging
|
|
|
+import requests
|
|
|
+import json
|
|
|
|
|
|
from odoo import fields, models
|
|
|
|
|
|
@@ -7,4 +9,35 @@ _logger = logging.getLogger(__name__)
|
|
|
class WhatsAppAccount(models.Model):
|
|
|
_inherit = ['whatsapp.account']
|
|
|
|
|
|
- whatsapp_web_url = fields.Char(string="WhatsApp Web URL", readonly=False, copy=False)
|
|
|
+ whatsapp_web_url = fields.Char(string="WhatsApp Web URL", readonly=False, copy=False)
|
|
|
+
|
|
|
+ def get_groups(self):
|
|
|
+ """
|
|
|
+ Obtiene los grupos de WhatsApp Web para la cuenta usando el wrapper HTTP de whatsapp-web.js.
|
|
|
+ Returns:
|
|
|
+ list: Lista de diccionarios con la información de los grupos
|
|
|
+ """
|
|
|
+ self.ensure_one()
|
|
|
+
|
|
|
+ if not self.whatsapp_web_url:
|
|
|
+ _logger.warning("No se ha configurado la URL de WhatsApp Web para la cuenta %s", self.name)
|
|
|
+ return []
|
|
|
+
|
|
|
+ try:
|
|
|
+ headers = {"Content-Type": "application/json"}
|
|
|
+ chats_payload = {
|
|
|
+ "method": "getGroups",
|
|
|
+ "args": []
|
|
|
+ }
|
|
|
+
|
|
|
+ response = requests.post(self.whatsapp_web_url, data=json.dumps(chats_payload), headers=headers)
|
|
|
+ if response.status_code == 200:
|
|
|
+ groups = response.json()
|
|
|
+ return groups
|
|
|
+ else:
|
|
|
+ _logger.error("Error al obtener groups: %s", response.text)
|
|
|
+ return []
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ _logger.error("Error en la petición de groups: %s", str(e))
|
|
|
+ return []
|