whatsapp_account.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import logging
  2. import requests
  3. import json
  4. from odoo import fields, models
  5. _logger = logging.getLogger(__name__)
  6. class WhatsAppAccount(models.Model):
  7. _inherit = ['whatsapp.account']
  8. whatsapp_web_url = fields.Char(string="WhatsApp Web URL", readonly=False, copy=False)
  9. def get_groups(self):
  10. """
  11. Obtiene los grupos de WhatsApp Web para la cuenta usando el wrapper HTTP de whatsapp-web.js.
  12. Returns:
  13. list: Lista de diccionarios con la información de los grupos
  14. """
  15. self.ensure_one()
  16. if not self.whatsapp_web_url:
  17. _logger.warning("No se ha configurado la URL de WhatsApp Web para la cuenta %s", self.name)
  18. return []
  19. try:
  20. headers = {"Content-Type": "application/json"}
  21. chats_payload = {
  22. "method": "getGroups",
  23. "args": []
  24. }
  25. response = requests.post(self.whatsapp_web_url, data=json.dumps(chats_payload), headers=headers)
  26. if response.status_code == 200:
  27. groups = response.json()
  28. return groups
  29. else:
  30. _logger.error("Error al obtener groups: %s", response.text)
  31. return []
  32. except Exception as e:
  33. _logger.error("Error en la petición de groups: %s", str(e))
  34. return []