fix: get template messages from api
This commit is contained in:
parent
cc47eb976e
commit
680a379afe
@ -40,6 +40,86 @@ def parse_mobile_no(mobile_no: str):
|
|||||||
"""
|
"""
|
||||||
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 == '+'])
|
||||||
|
|
||||||
|
@frappe.whitelist()
|
||||||
|
def get_whatsapp_messages(reference_doctype, reference_name):
|
||||||
|
messages = frappe.get_all(
|
||||||
|
"WhatsApp Message",
|
||||||
|
filters={
|
||||||
|
"reference_doctype": reference_doctype,
|
||||||
|
"reference_name": reference_name
|
||||||
|
},
|
||||||
|
fields=[
|
||||||
|
'name',
|
||||||
|
'type',
|
||||||
|
'to',
|
||||||
|
'from',
|
||||||
|
'content_type',
|
||||||
|
'message_type',
|
||||||
|
'attach',
|
||||||
|
'template',
|
||||||
|
'use_template',
|
||||||
|
'message_id',
|
||||||
|
'is_reply',
|
||||||
|
'reply_to_message_id',
|
||||||
|
'creation',
|
||||||
|
'message',
|
||||||
|
'status',
|
||||||
|
'reference_doctype',
|
||||||
|
'reference_name',
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
# Filter messages to get only Template messages
|
||||||
|
template_messages = [message for message in messages if message['message_type'] == 'Template']
|
||||||
|
|
||||||
|
# Iterate through template messages
|
||||||
|
for template_message in template_messages:
|
||||||
|
# Find the template that this message is using
|
||||||
|
template = frappe.get_doc("WhatsApp Templates", template_message['template'])
|
||||||
|
|
||||||
|
# If the template is found, add the template details to the template message
|
||||||
|
if template:
|
||||||
|
template_message['template_name'] = template.template_name
|
||||||
|
template_message['template'] = template.template
|
||||||
|
template_message['header'] = template.header
|
||||||
|
template_message['footer'] = template.footer
|
||||||
|
|
||||||
|
# Filter messages to get only reaction messages
|
||||||
|
reaction_messages = [message for message in messages if message['content_type'] == 'reaction']
|
||||||
|
|
||||||
|
# Iterate through reaction messages
|
||||||
|
for reaction_message in reaction_messages:
|
||||||
|
# Find the message that this reaction is reacting to
|
||||||
|
reacted_message = next((m for m in messages if m['message_id'] == reaction_message['reply_to_message_id']), None)
|
||||||
|
|
||||||
|
# If the reacted message is found, add the reaction to it
|
||||||
|
if reacted_message:
|
||||||
|
reacted_message['reaction'] = reaction_message['message']
|
||||||
|
|
||||||
|
# Filter messages to get only replies
|
||||||
|
reply_messages = [message for message in messages if message['is_reply']]
|
||||||
|
|
||||||
|
# Iterate through reply messages
|
||||||
|
for reply_message in reply_messages:
|
||||||
|
# Find the message that this message is replying to
|
||||||
|
replied_message = next((m for m in messages if m['message_id'] == reply_message['reply_to_message_id']), None)
|
||||||
|
|
||||||
|
# If the replied message is found, add the reply details to the reply message
|
||||||
|
doc = frappe.get_doc(reply_message['reference_doctype'], reply_message['reference_name'])
|
||||||
|
from_name = replied_message['from']
|
||||||
|
for c in doc.contacts:
|
||||||
|
if c.is_primary:
|
||||||
|
from_name = c.full_name or c.mobile_no
|
||||||
|
if replied_message:
|
||||||
|
reply_message['reply_message'] = replied_message['template'] if replied_message['message_type'] == 'Template' else replied_message['message']
|
||||||
|
reply_message['header'] = replied_message.get('header') or ''
|
||||||
|
reply_message['footer'] = replied_message.get('footer') or ''
|
||||||
|
reply_message['reply_to'] = replied_message['name']
|
||||||
|
reply_message['reply_to_type'] = replied_message['type']
|
||||||
|
reply_message['reply_to_from'] = from_name
|
||||||
|
|
||||||
|
return [message for message in messages if message['content_type'] != 'reaction']
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def create_whatsapp_message(reference_doctype, reference_name, message, to, attach, reply_to, content_type="text"):
|
def create_whatsapp_message(reference_doctype, reference_name, message, to, attach, reply_to, content_type="text"):
|
||||||
doc = frappe.new_doc("WhatsApp Message")
|
doc = frappe.new_doc("WhatsApp Message")
|
||||||
|
|||||||
@ -934,73 +934,15 @@ const all_activities = createResource({
|
|||||||
|
|
||||||
const showWhatsappTemplates = ref(false)
|
const showWhatsappTemplates = ref(false)
|
||||||
|
|
||||||
const whatsappMessages = createListResource({
|
const whatsappMessages = createResource({
|
||||||
type: 'list',
|
url: 'crm.api.whatsapp.get_whatsapp_messages',
|
||||||
doctype: 'WhatsApp Message',
|
cache: ['whatsapp_messages', doc.value.data.name],
|
||||||
cache: ['whatsapp_message', doc.value.data.name],
|
params: {
|
||||||
fields: [
|
|
||||||
'name',
|
|
||||||
'type',
|
|
||||||
'to',
|
|
||||||
'from',
|
|
||||||
'content_type',
|
|
||||||
'attach',
|
|
||||||
'template',
|
|
||||||
'use_template',
|
|
||||||
'message_id',
|
|
||||||
'is_reply',
|
|
||||||
'reply_to_message_id',
|
|
||||||
'creation',
|
|
||||||
'message',
|
|
||||||
'status',
|
|
||||||
],
|
|
||||||
filters: {
|
|
||||||
reference_doctype: props.doctype,
|
reference_doctype: props.doctype,
|
||||||
reference_name: doc.value.data.name,
|
reference_name: doc.value.data.name,
|
||||||
status: ['!=', 'failed'],
|
|
||||||
},
|
},
|
||||||
orderBy: 'modified desc',
|
|
||||||
pageLength: 99999,
|
|
||||||
auto: true,
|
auto: true,
|
||||||
transform: (data) => {
|
transform: (data) => sortByCreation(data),
|
||||||
data = sortByCreation(data)
|
|
||||||
// loop on filtered data where message.content_type == 'reaction'
|
|
||||||
data
|
|
||||||
.filter((message) => message.content_type == 'reaction')
|
|
||||||
.forEach((message) => {
|
|
||||||
// find the message that this reaction is reacting to
|
|
||||||
const reactedMessage = data.find(
|
|
||||||
(m) => m.message_id == message.reply_to_message_id
|
|
||||||
)
|
|
||||||
// if the reacted message is found, add the reaction to it
|
|
||||||
if (reactedMessage) {
|
|
||||||
reactedMessage.reaction = message.message
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// loop on filtered data where message.is_reply == 1
|
|
||||||
data
|
|
||||||
.filter((message) => message.is_reply)
|
|
||||||
.forEach((message) => {
|
|
||||||
// find the message that this message is replying to
|
|
||||||
const repliedMessage = data.find(
|
|
||||||
(m) => m.message_id == message.reply_to_message_id
|
|
||||||
)
|
|
||||||
// if the replied message is found, add the reply to it
|
|
||||||
if (repliedMessage) {
|
|
||||||
message.reply_message = repliedMessage.message
|
|
||||||
message.reply_to = repliedMessage.name
|
|
||||||
message.reply_to_type = repliedMessage.type
|
|
||||||
message.reply_to_from =
|
|
||||||
(repliedMessage.from &&
|
|
||||||
getContact(doc.value.data.mobile_no)?.full_name) ||
|
|
||||||
getLeadContact(doc.value.data.mobile_no)?.full_name ||
|
|
||||||
repliedMessage.from
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return data.filter((message) => message.content_type != 'reaction')
|
|
||||||
},
|
|
||||||
onSuccess: () => nextTick(() => scroll()),
|
onSuccess: () => nextTick(() => scroll()),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user