fix: return [] if twilio-integration app is installed while getting whatsapp messages

This commit is contained in:
Shariq Ansari 2025-01-03 02:37:05 +05:30
parent 3dc7a254b1
commit bd8b9ca472

View File

@ -1,6 +1,8 @@
import frappe
import json import json
import frappe
from frappe import _ from frappe import _
from crm.api.doc import get_assigned_users from crm.api.doc import get_assigned_users
from crm.fcrm.doctype.crm_notification.crm_notification import notify_user from crm.fcrm.doctype.crm_notification.crm_notification import notify_user
@ -82,8 +84,8 @@ def get_lead_or_deal_from_number(number):
def parse_mobile_no(mobile_no: str): def parse_mobile_no(mobile_no: str):
"""Parse mobile number to remove spaces, brackets, etc. """Parse mobile number to remove spaces, brackets, etc.
>>> parse_mobile_no('+91 (766) 667 6666') >>> parse_mobile_no("+91 (766) 667 6666")
... '+917666676666' ... "+917666676666"
""" """
return "".join([c for c in mobile_no if c.isdigit() or c == "+"]) return "".join([c for c in mobile_no if c.isdigit() or c == "+"])
@ -104,6 +106,10 @@ def is_whatsapp_installed():
@frappe.whitelist() @frappe.whitelist()
def get_whatsapp_messages(reference_doctype, reference_name): def get_whatsapp_messages(reference_doctype, reference_name):
# twilio integration app is not compatible with crm app
# crm has its own twilio integration in built
if "twilio-integration" in frappe.get_installed_apps():
return []
if not frappe.db.exists("DocType", "WhatsApp Message"): if not frappe.db.exists("DocType", "WhatsApp Message"):
return [] return []
messages = [] messages = []
@ -170,9 +176,7 @@ def get_whatsapp_messages(reference_doctype, reference_name):
) )
# Filter messages to get only Template messages # Filter messages to get only Template messages
template_messages = [ template_messages = [message for message in messages if message["message_type"] == "Template"]
message for message in messages if message["message_type"] == "Template"
]
# Iterate through template messages # Iterate through template messages
for template_message in template_messages: for template_message in template_messages:
@ -184,35 +188,23 @@ def get_whatsapp_messages(reference_doctype, reference_name):
template_message["template_name"] = template.template_name template_message["template_name"] = template.template_name
if template_message["template_parameters"]: if template_message["template_parameters"]:
parameters = json.loads(template_message["template_parameters"]) parameters = json.loads(template_message["template_parameters"])
template.template = parse_template_parameters( template.template = parse_template_parameters(template.template, parameters)
template.template, parameters
)
template_message["template"] = template.template template_message["template"] = template.template
if template_message["template_header_parameters"]: if template_message["template_header_parameters"]:
header_parameters = json.loads( header_parameters = json.loads(template_message["template_header_parameters"])
template_message["template_header_parameters"] template.header = parse_template_parameters(template.header, header_parameters)
)
template.header = parse_template_parameters(
template.header, header_parameters
)
template_message["header"] = template.header template_message["header"] = template.header
template_message["footer"] = template.footer template_message["footer"] = template.footer
# Filter messages to get only reaction messages # Filter messages to get only reaction messages
reaction_messages = [ reaction_messages = [message for message in messages if message["content_type"] == "reaction"]
message for message in messages if message["content_type"] == "reaction"
]
# Iterate through reaction messages # Iterate through reaction messages
for reaction_message in reaction_messages: for reaction_message in reaction_messages:
# Find the message that this reaction is reacting to # Find the message that this reaction is reacting to
reacted_message = next( reacted_message = next(
( (m for m in messages if m["message_id"] == reaction_message["reply_to_message_id"]),
m
for m in messages
if m["message_id"] == reaction_message["reply_to_message_id"]
),
None, None,
) )
@ -230,18 +222,12 @@ def get_whatsapp_messages(reference_doctype, reference_name):
for reply_message in reply_messages: for reply_message in reply_messages:
# Find the message that this message is replying to # Find the message that this message is replying to
replied_message = next( replied_message = next(
( (m for m in messages if m["message_id"] == reply_message["reply_to_message_id"]),
m
for m in messages
if m["message_id"] == reply_message["reply_to_message_id"]
),
None, None,
) )
# If the replied message is found, add the reply details to the reply message # If the replied message is found, add the reply details to the reply message
from_name = ( from_name = get_from_name(reply_message) if replied_message["from"] else _("You")
get_from_name(reply_message) if replied_message["from"] else _("You")
)
if replied_message: if replied_message:
message = replied_message["message"] message = replied_message["message"]
if replied_message["message_type"] == "Template": if replied_message["message_type"] == "Template":