[18.0][IMP] mail_gateway_whatsapp: Add support for buttons and variables in WhatsApp templates - #1780
Conversation
a8bd1dc to
a865522
Compare
a865522 to
e8ac1aa
Compare
e8ac1aa to
2b25749
Compare
|
@carlos-lopez-tecnativa can you review it and check if anything should be fw-ported from the final 17 PR (I think at least the removal of |
I checked this, and nothing is missing from v17. The |
carlos-lopez-tecnativa
left a comment
There was a problem hiding this comment.
I haven’t tested this PR yet, but I think we can update the translation method in v18. @eduezerouali-tecnativa
| def _check_buttons(self): | ||
| for template in self: | ||
| if len(template.button_ids) > 10: | ||
| raise ValidationError(_("A maximum of 10 buttons is allowed.")) |
There was a problem hiding this comment.
| raise ValidationError(_("A maximum of 10 buttons is allowed.")) | |
| raise ValidationError(self.env._("A maximum of 10 buttons is allowed.")) |
20b04bf to
d73f81b
Compare
|
@eduezerouali-tecnativa please finish this. |
| def _compute_variable_ids(self): | ||
| for template in self: | ||
| to_remove = self.env["mail.whatsapp.template.variable"] | ||
| to_keep = self.env["mail.whatsapp.template.variable"] | ||
| new_values = [] | ||
| header_variables = list(re.findall(REG_VARIABLE, template.header or "")) | ||
| body_variables = set(re.findall(REG_VARIABLE, template.body or "")) | ||
| # header | ||
| current_header_variable = template.variable_ids.filtered( | ||
| lambda line: line.line_type == "header" | ||
| ) | ||
| if header_variables and not current_header_variable: | ||
| new_values.append( | ||
| { | ||
| "name": header_variables[0], | ||
| "line_type": "header", | ||
| "template_id": template.id, | ||
| } | ||
| ) | ||
| elif not header_variables and current_header_variable: | ||
| to_remove += current_header_variable | ||
| elif current_header_variable: | ||
| to_keep += current_header_variable | ||
| # body | ||
| current_body_variables = template.variable_ids.filtered( | ||
| lambda line: line.line_type == "body" | ||
| ) | ||
| new_body_variable_names = [ | ||
| var_name | ||
| for var_name in body_variables | ||
| if var_name not in current_body_variables.mapped("name") | ||
| ] | ||
| deleted_variables = current_body_variables.filtered( | ||
| lambda var, body_variables=body_variables: var.name | ||
| not in body_variables | ||
| ) | ||
|
|
||
| new_values += [ | ||
| {"name": var_name, "line_type": "body", "template_id": template.id} | ||
| for var_name in set(new_body_variable_names) | ||
| ] | ||
| to_remove += deleted_variables | ||
| to_keep += current_body_variables - deleted_variables | ||
| template.variable_ids = [(3, to_remove.id) for to_remove in to_remove] + [ | ||
| Command.create(vals) for vals in new_values | ||
| ] | ||
|
|
||
| @api.depends("header", "body") | ||
| def _compute_variable_ids(self): | ||
| for template in self: | ||
| to_remove = self.env["mail.whatsapp.template.variable"] | ||
| to_keep = self.env["mail.whatsapp.template.variable"] | ||
| new_values = [] | ||
| header_variables = list(re.findall(REG_VARIABLE, template.header or "")) | ||
| body_variables = set(re.findall(REG_VARIABLE, template.body or "")) | ||
| # header | ||
| current_header_variable = template.variable_ids.filtered( | ||
| lambda line: line.line_type == "header" | ||
| ) | ||
| if header_variables and not current_header_variable: | ||
| new_values.append( | ||
| { | ||
| "name": header_variables[0], | ||
| "line_type": "header", | ||
| "template_id": template.id, | ||
| } | ||
| ) | ||
| elif not header_variables and current_header_variable: | ||
| to_remove += current_header_variable | ||
| elif current_header_variable: | ||
| to_keep += current_header_variable | ||
| # body | ||
| current_body_variables = template.variable_ids.filtered( | ||
| lambda line: line.line_type == "body" | ||
| ) | ||
| new_body_variable_names = [ | ||
| var_name | ||
| for var_name in body_variables | ||
| if var_name not in current_body_variables.mapped("name") | ||
| ] | ||
| deleted_variables = current_body_variables.filtered( | ||
| lambda var, body_variables=body_variables: var.name | ||
| not in body_variables | ||
| ) | ||
|
|
||
| new_values += [ | ||
| {"name": var_name, "line_type": "body", "template_id": template.id} | ||
| for var_name in set(new_body_variable_names) | ||
| ] | ||
| to_remove += deleted_variables | ||
| to_keep += current_body_variables - deleted_variables | ||
| template.variable_ids = [(3, to_remove.id) for to_remove in to_remove] + [ | ||
| Command.create(vals) for vals in new_values | ||
| ] |
d73f81b to
e005eb4
Compare
| for button in self.button_ids: | ||
| button_data = {"type": button.button_type.upper(), "text": button.name} | ||
| if button.button_type == "url": | ||
| button_data["url"] = button.website_url | ||
| if button.url_type == "dynamic": | ||
| button_data["url"] += "{{1}}" | ||
| button_data["example"] = button.variable_ids[0].sample_value | ||
| elif button.button_type == "phone_number": | ||
| button_data["phone_number"] = button.call_number | ||
| buttons.append(button_data) | ||
| if buttons: | ||
| components.append({"type": "BUTTONS", "buttons": buttons}) | ||
| # TODO: add more components(location, etc) | ||
| components.append({"type": "FOOTER", "text": self.footer}) | ||
| buttons = [] | ||
| for button in self.button_ids: | ||
| button_data = {"type": button.button_type.upper(), "text": button.name} | ||
| if button.button_type == "url": | ||
| button_data["url"] = button.website_url | ||
| if button.url_type == "dynamic": | ||
| button_data["url"] += "{{1}}" | ||
| button_data["example"] = button.variable_ids[0].sample_value | ||
| elif button.button_type == "phone_number": | ||
| button_data["phone_number"] = button.call_number | ||
| buttons.append(button_data) | ||
| if buttons: | ||
| components.append({"type": "BUTTONS", "buttons": buttons}) |
There was a problem hiding this comment.
The same here
e005eb4 to
5cc21ff
Compare
5cc21ff to
16efbea
Compare
… WhatsApp templates
16efbea to
c4818c5
Compare
carlos-lopez-tecnativa
left a comment
There was a problem hiding this comment.
I updated the code to fix the problems. It has been tested and is working.
|
What a great day to merge this nice PR. Let's do it! |
|
Congratulations, your PR was merged at c7acc58. Thanks a lot for contributing to OCA. ❤️ |
This is FWP from #1723
cc @Tecnativa TT57145
ping @pedrobaeza @carlos-lopez-tecnativa @CarlosRoca13