| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- import logging
- import requests
- import json
- from odoo import fields, models
- _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_login = fields.Char(string="Login", readonly=False, copy=False)
- whatsapp_web_api_key = fields.Char(string="API Key", 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 []
|