whatsapp_account.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. import logging
  2. import requests
  3. import json
  4. import mimetypes
  5. import base64
  6. from markupsafe import Markup
  7. from odoo import fields, models, _
  8. from odoo.addons.whatsapp.tools.whatsapp_api import WhatsAppApi
  9. from odoo.tools import plaintext2html
  10. _logger = logging.getLogger(__name__)
  11. class WhatsAppAccount(models.Model):
  12. _inherit = "whatsapp.account"
  13. whatsapp_web_url = fields.Char(
  14. string="WhatsApp Web URL", readonly=False, copy=False
  15. )
  16. whatsapp_web_login = fields.Char(string="Login", readonly=False, copy=False)
  17. whatsapp_web_api_key = fields.Char(string="API Key", readonly=False, copy=False)
  18. def get_groups(self):
  19. """
  20. Obtiene los grupos de WhatsApp Web para la cuenta desde la base de datos de la plataforma.
  21. Returns:
  22. list: Lista de diccionarios con la información de los grupos en formato compatible con Odoo
  23. """
  24. self.ensure_one()
  25. if not self.whatsapp_web_url:
  26. _logger.warning(
  27. "No se ha configurado la URL de WhatsApp Web para la cuenta %s",
  28. self.name,
  29. )
  30. return []
  31. if not self.whatsapp_web_login:
  32. _logger.warning(
  33. "No se ha configurado el Login (session_name) para la cuenta %s",
  34. self.name,
  35. )
  36. return []
  37. if not self.whatsapp_web_api_key:
  38. _logger.warning(
  39. "No se ha configurado la API Key para la cuenta %s", self.name
  40. )
  41. return []
  42. try:
  43. # Construir URL del nuevo endpoint
  44. base_url = self.whatsapp_web_url.rstrip("/")
  45. session_name = self.whatsapp_web_login
  46. url = f"{base_url}/api/v1/{session_name}/groups"
  47. headers = {
  48. "Content-Type": "application/json",
  49. "X-API-Key": self.whatsapp_web_api_key,
  50. }
  51. response = requests.get(url, headers=headers, timeout=30)
  52. if response.status_code == 200:
  53. groups = response.json()
  54. _logger.info(
  55. "Grupos obtenidos desde la base de datos: %d grupos", len(groups)
  56. )
  57. return groups
  58. else:
  59. _logger.error(
  60. "Error al obtener groups: %s - %s",
  61. response.status_code,
  62. response.text,
  63. )
  64. return []
  65. except Exception as e:
  66. _logger.error("Error en la petición de groups: %s", str(e))
  67. return []
  68. def _find_or_create_partner_from_payload(self, contacts_data):
  69. """
  70. Identify or create a partner based on webhook contacts list.
  71. Priority:
  72. 1. Mobile (last 10 digits match) -> Update WA ID if needed.
  73. 2. WA ID (exact match) -> Update Mobile/Name if needed.
  74. 3. Create new partner.
  75. Returns: res.partner record
  76. """
  77. if not contacts_data:
  78. return self.env["res.partner"]
  79. contact_info = contacts_data[0]
  80. wa_id = contact_info.get("wa_id")
  81. mobile = contact_info.get("phone_number") # Normalized phone from webhook
  82. # Try to get profile name, fallback to wa_id
  83. profile_name = contact_info.get("profile", {}).get("name") or wa_id
  84. # Lid handling: If the main ID is an LID (Lyophilized ID used for privacy),
  85. # we still prefer to link it to a real phone number if available.
  86. # The payload might have "wa_id" as the LID and "phone_number" as the real number,
  87. # or vice-versa depending on context. We trust 'phone_number' field for mobile search.
  88. partner = self.env["res.partner"]
  89. # Strategy 1: Search by Mobile (last 10 digits)
  90. if mobile and len(mobile) >= 10:
  91. last_10 = mobile[-10:]
  92. partner = (
  93. self.env["res.partner"]
  94. .sudo()
  95. .search([("mobile", "like", f"%{last_10}")], limit=1)
  96. )
  97. if partner:
  98. _logger.info(f"Partner found by mobile {last_10}: {partner.name}")
  99. # Update WA ID if missing or different (and valid)
  100. if wa_id and partner.whatsapp_web_id != wa_id:
  101. partner.write({"whatsapp_web_id": wa_id})
  102. return partner
  103. # Strategy 2: Search by WhatsApp Web ID
  104. if wa_id:
  105. partner = (
  106. self.env["res.partner"]
  107. .sudo()
  108. .search([("whatsapp_web_id", "=", wa_id)], limit=1)
  109. )
  110. if partner:
  111. _logger.info(f"Partner found by WA ID {wa_id}: {partner.name}")
  112. # Update mobile if missing
  113. if mobile and not partner.mobile:
  114. partner.write({"mobile": mobile})
  115. return partner
  116. # Strategy 3: Create New Partner
  117. vals = {
  118. "name": profile_name,
  119. "whatsapp_web_id": wa_id,
  120. "mobile": mobile,
  121. }
  122. _logger.info(f"Creating new partner from webhook: {vals}")
  123. partner = self.env["res.partner"].sudo().create(vals)
  124. return partner
  125. def _process_messages(self, value):
  126. """
  127. Sobrescritura completa para manejar mensajes enviados desde la misma cuenta (self-messages)
  128. y rutearlos al chat correcto.
  129. Refactorizado para soportar grupos vía metadata y creación Lazy.
  130. """
  131. # Log del payload recibido para debug
  132. _logger.info(
  133. "DEBUG - WhatsApp Webhook Value: %s",
  134. json.dumps(value, indent=4, default=str),
  135. )
  136. if "messages" not in value and value.get("whatsapp_business_api_data", {}).get(
  137. "messages"
  138. ):
  139. value = value["whatsapp_business_api_data"]
  140. wa_api = WhatsAppApi(self)
  141. # 1. Identificar Remitente (Sender)
  142. contacts_data = value.get("contacts", [])
  143. sender_partner = self._find_or_create_partner_from_payload(contacts_data)
  144. # Fallback Name if partner creation failed (rare)
  145. sender_name = sender_partner.name if sender_partner else "Unknown"
  146. # Determinar el ID del teléfono actual (para detectar auto-mensajes)
  147. my_phone_id = value.get("metadata", {}).get("phone_number_id")
  148. for messages in value.get("messages", []):
  149. parent_msg_id = False
  150. parent_id = False
  151. channel = False
  152. sender_mobile = messages["from"]
  153. message_type = messages["type"]
  154. # Lógica para detectar self-messages
  155. is_self_message = False
  156. # Check explicit flag first (if provided by bridge)
  157. if messages.get("fromMe") or messages.get("from_me"):
  158. is_self_message = True
  159. # Check phone ID match as fallback
  160. # Check phone ID match as fallback ( Metadata OR stored phone_uid)
  161. if not is_self_message:
  162. sender_clean = sender_mobile.replace("@c.us", "")
  163. # Compare vs Metadata ID
  164. if my_phone_id:
  165. my_clean = my_phone_id.replace("@c.us", "")
  166. if sender_clean == my_clean:
  167. is_self_message = True
  168. # Compare vs Account Phone UID (if metadata failed/missing)
  169. if not is_self_message and self.phone_uid:
  170. account_clean = self.phone_uid.replace("@c.us", "")
  171. if sender_clean == account_clean:
  172. is_self_message = True
  173. if is_self_message:
  174. # Intentar obtener el destinatario real
  175. if "to" in messages:
  176. sender_mobile = messages["to"]
  177. elif "id" in messages and "true_" in messages["id"]:
  178. # Fallback: intentar parsear del ID (formato true_NUMBER@c.us_ID o true_NUMBER_ID)
  179. # Relaxed check: Removed mandatory @c.us to support raw numbers
  180. try:
  181. # Extraer parte entre true_ y @ o primer _
  182. parts = messages["id"].split("_")
  183. if len(parts) > 1:
  184. jid = parts[1] # 5215581845273@c.us or 5215581845273
  185. sender_mobile = jid
  186. _logger.info(
  187. "Recuperado destinatario real desde ID: %s",
  188. sender_mobile,
  189. )
  190. except Exception as e:
  191. _logger.warning("Error parseando ID de self-message: %s", e)
  192. _logger.info(
  193. "Detectado self-message. Redirigiendo a chat de: %s (Original From: %s)",
  194. sender_mobile,
  195. messages["from"],
  196. )
  197. # --- RECONCILIATION LOGIC ---
  198. # Si viene un job_id en metadata, reconciliar el ID antes de chequear duplicados.
  199. # Esto maneja el caso donde el "Echo" del mensaje trae el ID real y confirma el envío del worker.
  200. job_id = value.get("metadata", {}).get("job_id")
  201. if job_id:
  202. pending_msg = (
  203. self.env["whatsapp.message"]
  204. .sudo()
  205. .search([("job_id", "=", job_id)], limit=1)
  206. )
  207. if pending_msg and pending_msg.msg_uid != messages["id"]:
  208. _logger.info(
  209. "Reconciliando Message ID desde payload de mensajes: JobID %s -> Real ID %s",
  210. job_id,
  211. messages["id"],
  212. )
  213. pending_msg.msg_uid = messages["id"]
  214. # Opcional: Asegurar estado si es necesario, aunque si es un echo,
  215. # el estado 'sent' ya debería estar set por el envío inicial.
  216. # ----------------------------
  217. # --- DEDUPLICATION LOGIC ---
  218. # Check if this message was already processed or sent by Odoo
  219. # This prevents the "Echo" effect when Odoo sends a message and Webhook confirms it
  220. existing_wa_msg = (
  221. self.env["whatsapp.message"]
  222. .sudo()
  223. .search([("msg_uid", "=", messages["id"])], limit=1)
  224. )
  225. if existing_wa_msg and message_type != "reaction":
  226. _logger.info(
  227. "Skipping duplicate message %s (already exists)", messages["id"]
  228. )
  229. # Optionally update status here if needed, but avoiding duplicate mail.message is key
  230. continue
  231. # ---------------------------
  232. # Context / Reply Handling
  233. target_record = False
  234. if "context" in messages and messages["context"].get("id"):
  235. parent_whatsapp_message = (
  236. self.env["whatsapp.message"]
  237. .sudo()
  238. .search([("msg_uid", "=", messages["context"]["id"])])
  239. )
  240. if parent_whatsapp_message:
  241. parent_msg_id = parent_whatsapp_message.id
  242. parent_id = parent_whatsapp_message.mail_message_id
  243. if parent_id:
  244. # Check where the parent message belongs
  245. if (
  246. parent_id.model
  247. and parent_id.model != "discuss.channel"
  248. and parent_id.res_id
  249. ):
  250. # It's a reply to a document (Ticket, Order, etc.)
  251. try:
  252. target_record = self.env[parent_id.model].browse(
  253. parent_id.res_id
  254. )
  255. _logger.info(
  256. f"Reply routed to Document: {parent_id.model} #{parent_id.res_id}"
  257. )
  258. except Exception as e:
  259. _logger.warning(
  260. f"Could not load target record {parent_id.model} #{parent_id.res_id}: {e}"
  261. )
  262. target_record = False
  263. else:
  264. # It's a reply in a channel
  265. channel = (
  266. self.env["discuss.channel"]
  267. .sudo()
  268. .search([("message_ids", "in", parent_id.id)], limit=1)
  269. )
  270. # 2. Lógica de Grupos (Metadata - Decoupled & Lazy)
  271. group_metadata = value.get("metadata", {}).get("group")
  272. # Support legacy group_id only if group dict missing
  273. if not group_metadata and value.get("metadata", {}).get("group_id"):
  274. group_metadata = {"id": value.get("metadata", {}).get("group_id")}
  275. if group_metadata and not target_record:
  276. # Check if group module is installed (Use 'in' operator for models with dots)
  277. if "ww.group" in self.env:
  278. # Process Group (Lazy Create + Organic Member Add)
  279. group = self.env["ww.group"].process_webhook_group(
  280. self, group_metadata, sender_partner
  281. )
  282. if group and group.channel_id and not channel:
  283. channel = group.channel_id
  284. _logger.info(
  285. "Mensaje de grupo ruteado a canal: %s", channel.name
  286. )
  287. else:
  288. _logger.warning(
  289. "Recibido mensaje de grupo pero ww.group no está instalado."
  290. )
  291. # 3. Canal Directo (Si no es grupo y no tenemos target ni channel aun)
  292. if not target_record and not channel:
  293. channel = self._find_active_channel(
  294. sender_mobile, sender_name=sender_name, create_if_not_found=True
  295. )
  296. # --- RENAME LOGIC FOR 1:1 CHATS ---
  297. # Solo si estamos usando un canal (no si vamos a un documento)
  298. if channel and channel.channel_type == "whatsapp" and sender_partner:
  299. is_group_channel = False
  300. if channel.whatsapp_number and channel.whatsapp_number.endswith(
  301. "@g.us"
  302. ):
  303. is_group_channel = True
  304. if not is_group_channel and channel.name != sender_partner.name:
  305. _logger.info(
  306. f"Renaming channel {channel.id} from '{channel.name}' to '{sender_partner.name}'"
  307. )
  308. channel.sudo().write({"name": sender_partner.name})
  309. # -----------------------------------
  310. # Define Target Record if not set (fallback to channel)
  311. if not target_record:
  312. target_record = channel
  313. if not target_record:
  314. _logger.error("Could not determine target record for message")
  315. continue
  316. # Determinar autor (Author ID)
  317. # Preferimos usar el partner identificado del payload
  318. author_id = sender_partner.id if sender_partner else False
  319. # If no sender partner, try channel partner if target is channel
  320. if (
  321. not author_id
  322. and getattr(target_record, "_name", "") == "discuss.channel"
  323. ):
  324. author_id = target_record.whatsapp_partner_id.id
  325. if is_self_message:
  326. # Si es mensaje propio, usar el partner de la compañía o OdooBot
  327. author_id = self.env.ref("base.partner_root").id
  328. kwargs = {
  329. "message_type": "whatsapp_message",
  330. "author_id": author_id,
  331. "subtype_xmlid": "mail.mt_comment",
  332. "parent_id": parent_id.id if parent_id else None,
  333. }
  334. if message_type == "text":
  335. kwargs["body"] = plaintext2html(messages["text"]["body"])
  336. elif message_type == "button":
  337. kwargs["body"] = messages["button"]["text"]
  338. elif message_type in ("document", "image", "audio", "video", "sticker"):
  339. filename = messages[message_type].get("filename")
  340. is_voice = messages[message_type].get("voice")
  341. mime_type = messages[message_type].get("mime_type")
  342. caption = messages[message_type].get("caption")
  343. # Hybrid Handling: Check for local base64 content
  344. data_base64 = messages[message_type].get("data_base64")
  345. if data_base64:
  346. _logger.info("Usando contenido base64 local para %s", message_type)
  347. try:
  348. datas = base64.b64decode(data_base64)
  349. except Exception as e:
  350. _logger.error("Error al decodificar data_base64: %s", e)
  351. datas = b""
  352. else:
  353. # Fallback to standard flow (download from Meta)
  354. datas = wa_api._get_whatsapp_document(messages[message_type]["id"])
  355. if not filename:
  356. extension = mimetypes.guess_extension(mime_type) or ""
  357. filename = message_type + extension
  358. kwargs["attachments"] = [(filename, datas)]
  359. if caption:
  360. kwargs["body"] = plaintext2html(caption)
  361. elif message_type == "location":
  362. url = Markup(
  363. "https://maps.google.com/maps?q={latitude},{longitude}"
  364. ).format(
  365. latitude=messages["location"]["latitude"],
  366. longitude=messages["location"]["longitude"],
  367. )
  368. body = Markup(
  369. '<a target="_blank" href="{url}"> <i class="fa fa-map-marker"/> {location_string} </a>'
  370. ).format(url=url, location_string=_("Location"))
  371. if messages["location"].get("name"):
  372. body += Markup("<br/>{location_name}").format(
  373. location_name=messages["location"]["name"]
  374. )
  375. if messages["location"].get("address"):
  376. body += Markup("<br/>{location_address}").format(
  377. location_address=messages["location"]["address"]
  378. )
  379. kwargs["body"] = body
  380. elif message_type == "contacts":
  381. body = ""
  382. for contact in messages["contacts"]:
  383. body += Markup(
  384. "<i class='fa fa-address-book'/> {contact_name} <br/>"
  385. ).format(
  386. contact_name=contact.get("name", {}).get("formatted_name", "")
  387. )
  388. for phone in contact.get("phones", []):
  389. body += Markup("{phone_type}: {phone_number}<br/>").format(
  390. phone_type=phone.get("type"),
  391. phone_number=phone.get("phone"),
  392. )
  393. kwargs["body"] = body
  394. elif message_type == "reaction":
  395. msg_uid = messages["reaction"].get("message_id")
  396. whatsapp_message = (
  397. self.env["whatsapp.message"]
  398. .sudo()
  399. .search([("msg_uid", "=", msg_uid)])
  400. )
  401. if whatsapp_message:
  402. # Use sender_partner for reaction if available
  403. partner_id = sender_partner
  404. # FALLBACK: If no sender_partner found (common in Groups where contacts is empty),
  405. # try to find partner by the 'from' field (mobile number)
  406. if not partner_id and messages.get("from"):
  407. mobile = messages["from"]
  408. if len(mobile) >= 10:
  409. partner_id = (
  410. self.env["res.partner"]
  411. .sudo()
  412. .search(
  413. [("mobile", "like", f"%{mobile[-10:]}")], limit=1
  414. )
  415. )
  416. if (
  417. not partner_id
  418. and getattr(target_record, "_name", "") == "discuss.channel"
  419. ):
  420. partner_id = target_record.whatsapp_partner_id
  421. emoji = messages["reaction"].get("emoji")
  422. whatsapp_message.mail_message_id._post_whatsapp_reaction(
  423. reaction_content=emoji, partner_id=partner_id
  424. )
  425. continue
  426. else:
  427. _logger.warning("Unsupported whatsapp message type: %s", messages)
  428. continue
  429. # Fix: Only pass whatsapp_inbound_msg_uid if valid for this channel type
  430. # Standard channels (like groups) do not support this param and will crash
  431. if getattr(target_record, "_name", "") == "discuss.channel":
  432. if (
  433. hasattr(target_record, "channel_type")
  434. and target_record.channel_type == "whatsapp"
  435. ):
  436. kwargs["whatsapp_inbound_msg_uid"] = messages["id"]
  437. target_record.message_post(**kwargs)