whatsapp_account.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. whatsapp_web_login = fields.Char(string="Login", readonly=False, copy=False)
  10. whatsapp_web_api_key = fields.Char(string="API Key", readonly=False, copy=False)
  11. def get_groups(self):
  12. """
  13. Obtiene los grupos de WhatsApp Web para la cuenta usando el wrapper HTTP de whatsapp-web.js.
  14. Returns:
  15. list: Lista de diccionarios con la información de los grupos
  16. """
  17. self.ensure_one()
  18. if not self.whatsapp_web_url:
  19. _logger.warning("No se ha configurado la URL de WhatsApp Web para la cuenta %s", self.name)
  20. return []
  21. try:
  22. headers = {"Content-Type": "application/json"}
  23. chats_payload = {
  24. "method": "getGroups",
  25. "args": []
  26. }
  27. response = requests.post(self.whatsapp_web_url, data=json.dumps(chats_payload), headers=headers)
  28. if response.status_code == 200:
  29. groups = response.json()
  30. return groups
  31. else:
  32. _logger.error("Error al obtener groups: %s", response.text)
  33. return []
  34. except Exception as e:
  35. _logger.error("Error en la petición de groups: %s", str(e))
  36. return []