Skip to content

[18.0][IMP] mail_gateway_whatsapp: Add support for buttons and variables in WhatsApp templates - #1780

Merged
OCA-git-bot merged 1 commit into
OCA:18.0from
Tecnativa:18.0-mail_gateway_whatsapp-add-variables
Feb 26, 2026
Merged

[18.0][IMP] mail_gateway_whatsapp: Add support for buttons and variables in WhatsApp templates#1780
OCA-git-bot merged 1 commit into
OCA:18.0from
Tecnativa:18.0-mail_gateway_whatsapp-add-variables

Conversation

@eduezerouali-tecnativa

@eduezerouali-tecnativa eduezerouali-tecnativa commented Dec 24, 2025

Copy link
Copy Markdown
Contributor

@eduezerouali-tecnativa
eduezerouali-tecnativa force-pushed the 18.0-mail_gateway_whatsapp-add-variables branch from a8bd1dc to a865522 Compare December 24, 2025 10:52
@eduezerouali-tecnativa eduezerouali-tecnativa changed the title [18.0][IMP] mail_whatsapp_template: Add support for buttons and variables in WhatsApp templates [18.0][IMP] mail_gateway_whatsapp: Add support for buttons and variables in WhatsApp templates Dec 24, 2025
@eduezerouali-tecnativa
eduezerouali-tecnativa force-pushed the 18.0-mail_gateway_whatsapp-add-variables branch from a865522 to e8ac1aa Compare December 24, 2025 11:49
@pedrobaeza pedrobaeza added this to the 18.0 milestone Jan 2, 2026
Comment thread mail_gateway_whatsapp/models/mail_whatsapp_template.py
@pedrobaeza
pedrobaeza force-pushed the 18.0-mail_gateway_whatsapp-add-variables branch from e8ac1aa to 2b25749 Compare January 17, 2026 09:08
@pedrobaeza

Copy link
Copy Markdown
Member

@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 tracking=True).

@carlos-lopez-tecnativa

Copy link
Copy Markdown
Contributor

@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 tracking=True).

I checked this, and nothing is missing from v17. The tracking was removed in the forward-port and is not present here, so you can continue.

@carlos-lopez-tecnativa carlos-lopez-tecnativa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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."))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise ValidationError(_("A maximum of 10 buttons is allowed."))
raise ValidationError(self.env._("A maximum of 10 buttons is allowed."))

@eduezerouali-tecnativa
eduezerouali-tecnativa force-pushed the 18.0-mail_gateway_whatsapp-add-variables branch 2 times, most recently from 20b04bf to d73f81b Compare February 2, 2026 13:20
@pedrobaeza

Copy link
Copy Markdown
Member

@eduezerouali-tecnativa please finish this.

Comment thread mail_gateway_whatsapp/models/mail_whatsapp_template.py Outdated
Comment thread mail_gateway_whatsapp/models/mail_whatsapp_template.py Outdated
Comment on lines +253 to +346
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
]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here

@eduezerouali-tecnativa
eduezerouali-tecnativa force-pushed the 18.0-mail_gateway_whatsapp-add-variables branch from d73f81b to e005eb4 Compare February 26, 2026 11:51
Comment thread mail_gateway_whatsapp/models/mail_whatsapp_template.py Outdated
Comment on lines +289 to +315
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})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same here

Comment thread mail_gateway_whatsapp/models/mail_whatsapp_template.py Outdated
@eduezerouali-tecnativa
eduezerouali-tecnativa force-pushed the 18.0-mail_gateway_whatsapp-add-variables branch from e005eb4 to 5cc21ff Compare February 26, 2026 15:58
Comment thread mail_gateway_whatsapp/models/mail_whatsapp_template.py Outdated
@carlos-lopez-tecnativa
carlos-lopez-tecnativa force-pushed the 18.0-mail_gateway_whatsapp-add-variables branch from 5cc21ff to 16efbea Compare February 26, 2026 18:30
@carlos-lopez-tecnativa
carlos-lopez-tecnativa force-pushed the 18.0-mail_gateway_whatsapp-add-variables branch from 16efbea to c4818c5 Compare February 26, 2026 18:34

@carlos-lopez-tecnativa carlos-lopez-tecnativa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the code to fix the problems. It has been tested and is working.

@pedrobaeza pedrobaeza left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/ocabot merge major

@OCA-git-bot

Copy link
Copy Markdown
Contributor

What a great day to merge this nice PR. Let's do it!
Prepared branch 18.0-ocabot-merge-pr-1780-by-pedrobaeza-bump-major, awaiting test results.

@OCA-git-bot
OCA-git-bot merged commit 7ade3b3 into OCA:18.0 Feb 26, 2026
5 checks passed
@OCA-git-bot

Copy link
Copy Markdown
Contributor

Congratulations, your PR was merged at c7acc58. Thanks a lot for contributing to OCA. ❤️

@carlos-lopez-tecnativa
carlos-lopez-tecnativa deleted the 18.0-mail_gateway_whatsapp-add-variables branch February 26, 2026 18:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants