Skip to content

Commit

Permalink
fix:text added
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhiiirpujari committed Jan 30, 2024
1 parent 780f635 commit 365fdb5
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"reqd": 1
},
{
"fetch_from": "language.language_code",
"fieldname": "language_code",
"fieldtype": "Data",
"label": "Language Code",
Expand Down Expand Up @@ -108,7 +109,7 @@
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2023-11-23 17:44:04.673291",
"modified": "2024-01-29 17:38:08.625131",
"modified_by": "Administrator",
"module": "Frappe Whatsapp",
"name": "WhatsApp Templates",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ class WhatsAppTemplates(Document):

def after_insert(self):
"""Set template code."""
self.template_name = self.template_name.lower().replace(' ', '_')
self.template_name = self.template_name.lower().replace(' ', '_')
print('###template name**********', self.template_name)
self.language_code = frappe.db.get_value(
"Language", self.language
).replace('-', '_')
print("*****lnaguage code****",self.language_code)

self.get_settings()
data = {
Expand Down Expand Up @@ -49,12 +51,21 @@ def after_insert(self):
})

try:
print("***********DATA************************", json.dumps(data, indent=4))

response = make_post_request(
f"{self._url}/{self._version}/{self._business_id}/message_templates",
headers=self._headers, data=json.dumps(data)
)
self.id = response['id']
self.status = response['status']
if response['status'] == 'Active':
self.status = 'Active'
elif response['status'] == 'In review':
self.status = 'In review'
else:
self.status == 'Rejected'

# self.status = response['status']
# frappe.db.set_value("WhatsApp Templates", self.name, "id", response['id'])
except Exception as e:
res = frappe.flags.integration_request.json()['error']
Expand All @@ -75,6 +86,7 @@ def on_update(self):
"type": "BODY",
"text": self.template,
}
print("************sample_value*****",self.sample_values)
if self.sample_values:
body.update({
"example": {
Expand Down Expand Up @@ -138,18 +150,55 @@ def get_header(self):
"type": "header",
"format": self.header_type
}

if self.header_type == "TEXT":
header['text'] = self.header

else:
if not self.sample:
key = frappe.get_doc(self.doctype, self.name).get_document_share_key()
link = get_pdf_link(self.doctype, self.name)
self.sample = f'{frappe.utils.get_url()}{link}&key={key}'
print("*****SAMPLE VALUE for text***********",self.sample)

elif self.header_type == 'IMAGE':
print("**********header_type **********",self.header_type)
header.update({"example": {
"header_handle": [self.sample]
}})
"header_handle": [self.sample]}
})
# header.update({
# "type": "IMAGE",
# "image": {
# "id": self.sample
# }
# })

# header['image'].update({
# "format": self.header_type,
# "image": self.sample # split('/')[-1],
# }).self.header

print("*****SAMPLE for IMage***********",self.sample)

elif self.header_type == 'DOCUMENT':
header["media"] = {
"type": "document",
"url": self.sample
}

# header["type"].update({
# "format": self.header_type,
# # "sample ": self.sample # split('/')[-1],
# })
print("*****SAMPLE for document***********",self.sample)




# elif not self.sample:
# key = frappe.get_doc(self.doctype, self.name).get_document_share_key()
# link = get_pdf_link(self.doctype, self.name)
# self.sample = f'{frappe.utils.get_url()}{link}&key={key}'
# header.update({"example": {
# "header_handle": [self.sample]
# }})


print("***********HEADER in return ************************",header,type(header))
return header


Expand All @@ -168,13 +217,18 @@ def fetch():
"authorization": f"Bearer {token}",
"content-type": "application/json"
}
print("*******HEDERS FROM fetch********",headers)

try:
response = make_request(
"GET",
f"{url}/{version}/{business_id}/message_templates",
headers=headers,
)

print("*********this is response************",response)

print("**********RESPONSE DATA:******",response['data'])

for template in response['data']:
# set flag to insert or update
Expand All @@ -186,21 +240,41 @@ def fetch():
doc = frappe.new_doc("WhatsApp Templates")
doc.template_name = template['name']

doc.status = template['status']
if doc.status == 'Pending':
doc.status == 'Active'
else:
doc.status == "Rejected"

# doc.status = template['status']
doc.language_code = template['language']
doc.category = template['category']
doc.id = template['id']

# update components
print("*******Template Components******",template['components'])
for component in template['components']:

# update header
if component['type'] == "HEADER":
doc.header_type = component['format']

# if format is text update sample text
# if format is text update sample text "url": component['image']
if component['format'] == 'TEXT':
doc.header = component['text']
elif component['format'] == 'IMAGE':
doc.sample = {
"type": "image",
"url": component['image']
}
# doc.sample = [{
# "type": "header",
# "parameters": [{
# "type": "image",
# "image": {
# "id": 'self.sample'
# }}]
# }]

# Update footer text
elif component['type'] == 'FOOTER':
doc.footer = component['text']
Expand All @@ -227,4 +301,10 @@ def fetch():
title=res.get("error_user_title", "Error"),
)

return "Successfully fetched templates from meta"
return "Successfully fetched templates from meta"






0 comments on commit 365fdb5

Please sign in to comment.