diff --git a/mail_gateway_whatsapp/README.rst b/mail_gateway_whatsapp/README.rst index ba74ae21e2..2c86582341 100644 --- a/mail_gateway_whatsapp/README.rst +++ b/mail_gateway_whatsapp/README.rst @@ -115,6 +115,7 @@ Contributors * `Tecnativa `_: * Carlos Lopez +* Frank Cespedes Other credits ~~~~~~~~~~~~~ diff --git a/mail_gateway_whatsapp/__manifest__.py b/mail_gateway_whatsapp/__manifest__.py index 253e51d2e5..07fdf72ba8 100644 --- a/mail_gateway_whatsapp/__manifest__.py +++ b/mail_gateway_whatsapp/__manifest__.py @@ -16,6 +16,7 @@ "security/ir.model.access.csv", "wizards/whatsapp_composer.xml", "wizards/mail_compose_gateway_message.xml", + "views/mail_whatsapp_template_variable_views.xml", "views/mail_whatsapp_template_views.xml", "views/mail_gateway.xml", ], diff --git a/mail_gateway_whatsapp/models/__init__.py b/mail_gateway_whatsapp/models/__init__.py index f2833eae30..cb5678a18f 100644 --- a/mail_gateway_whatsapp/models/__init__.py +++ b/mail_gateway_whatsapp/models/__init__.py @@ -4,3 +4,4 @@ from . import mail_channel from . import res_partner from . import mail_whatsapp_template +from . import mail_whatsapp_template_variable diff --git a/mail_gateway_whatsapp/models/mail_gateway.py b/mail_gateway_whatsapp/models/mail_gateway.py index 8e1ee5dff7..03933cb059 100644 --- a/mail_gateway_whatsapp/models/mail_gateway.py +++ b/mail_gateway_whatsapp/models/mail_gateway.py @@ -55,6 +55,7 @@ def button_import_whatsapp_template(self): for template_data in meta_info.get("data", []): ws_template = templates_by_id.get(template_data["id"]) if ws_template: + ws_template.variable_ids.unlink() ws_template.write( WhatsappTemplate._prepare_values_to_import(self, template_data) ) diff --git a/mail_gateway_whatsapp/models/mail_gateway_whatsapp.py b/mail_gateway_whatsapp/models/mail_gateway_whatsapp.py index 00ea59ed7e..74e0cb1b7d 100644 --- a/mail_gateway_whatsapp/models/mail_gateway_whatsapp.py +++ b/mail_gateway_whatsapp/models/mail_gateway_whatsapp.py @@ -297,15 +297,34 @@ def _send_payload( "to": channel.gateway_channel_token, } if whatsapp_template: + variables = whatsapp_template.get_variable_values() payload.update( { "type": "template", "template": { "name": whatsapp_template.template_name, "language": {"code": whatsapp_template.language}, + "components": [], }, } ) + body_variables = variables.get("body") + if body_variables: + parameters = [] + if variables["type"] == "number": + for value in body_variables.values(): + parameters.append( + { + "type": "text", + "text": value, + } + ) + elif variables["type"] == "name": + # TODO: Implement name type + pass + payload["template"]["components"].append( + {"type": "body", "parameters": parameters} + ) else: payload.update( { diff --git a/mail_gateway_whatsapp/models/mail_whatsapp_template.py b/mail_gateway_whatsapp/models/mail_whatsapp_template.py index 66ede77d07..5a835f55de 100644 --- a/mail_gateway_whatsapp/models/mail_whatsapp_template.py +++ b/mail_gateway_whatsapp/models/mail_whatsapp_template.py @@ -1,4 +1,5 @@ # Copyright 2024 Tecnativa - Carlos López +# Copyright NuoBiT Solutions - Frank Cespedes # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). import re @@ -21,6 +22,10 @@ class MailWhatsAppTemplate(models.Model): name = fields.Char(required=True) body = fields.Text(required=True) + variable_ids = fields.One2many( + comodel_name="mail.whatsapp.template.variable", inverse_name="template_id" + ) + variable_type = fields.Selection(selection=[("number", "Number"), ("name", "Name")]) header = fields.Char() footer = fields.Char() template_name = fields.Char( @@ -82,6 +87,25 @@ def _compute_template_name(self): r"\W+", "_", slugify(template.name or "") ) + def get_variable_values(self): + values = {"type": self.variable_type} + body_variables = self.variable_ids.filtered(lambda x: x.section == "body") + if body_variables: + body = {} + for variable in body_variables: + body.update({variable.name: variable.default_value}) + values.update({"body": body}) + return values + + def get_body(self): + values = self.get_variable_values() + body_variables = values.get("body") + body = self.body + if body_variables: + for key, value in body_variables.items(): + body = body.replace(f"{{{{{key}}}}}", value) + return body + def button_back2draft(self): self.write({"state": "draft"}) @@ -159,6 +183,7 @@ def button_sync_template(self): ) response.raise_for_status() json_data = response.json() + self.variable_ids.unlink() vals = self._prepare_values_to_import(gateway, json_data) self.write(vals) except Exception as err: @@ -185,6 +210,23 @@ def _prepare_values_to_import(self, gateway, json_data): vals["header"] = component["text"] elif component["type"] == "BODY": vals["body"] = component["text"] + for key, value in component.get("example", {}).items(): + if key == "body_text": + vals["variable_type"] = "number" + for var_list in value: + for variable, var_value in enumerate(var_list, start=1): + vals.setdefault("variable_ids", []).append( + ( + 0, + 0, + { + "section": "body", + "name": variable, + "parameter_type": "text", + "default_value": var_value, + }, + ) + ) elif component["type"] == "FOOTER": vals["footer"] = component["text"] else: diff --git a/mail_gateway_whatsapp/models/mail_whatsapp_template_button.py b/mail_gateway_whatsapp/models/mail_whatsapp_template_button.py new file mode 100644 index 0000000000..292e058565 --- /dev/null +++ b/mail_gateway_whatsapp/models/mail_whatsapp_template_button.py @@ -0,0 +1,18 @@ +# Copyright NuoBiT Solutions - Frank Cespedes +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class MailWhatsAppTemplateButton(models.Model): + _name = "mail.whatsapp.template.button" + _description = "Mail WhatsApp template button" + + template_id = fields.Many2one(comodel_name="mail.whatsapp.template") + template_state = fields.Selection(related="template_id.state", readonly=True) + text = fields.Char() + url = fields.Char() + url_type = fields.Selection( + selection=[("static", "Static"), ("dynamic", "Dynamic")], + string="URL Type", + ) diff --git a/mail_gateway_whatsapp/models/mail_whatsapp_template_variable.py b/mail_gateway_whatsapp/models/mail_whatsapp_template_variable.py new file mode 100644 index 0000000000..d5812e5880 --- /dev/null +++ b/mail_gateway_whatsapp/models/mail_whatsapp_template_variable.py @@ -0,0 +1,22 @@ +# Copyright NuoBiT Solutions - Frank Cespedes +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + + +from odoo import fields, models + + +class MailWhatsAppTemplateVariable(models.Model): + _name = "mail.whatsapp.template.variable" + _description = "Mail WhatsApp template variable" + _order = "name" + + template_id = fields.Many2one(comodel_name="mail.whatsapp.template") + template_state = fields.Selection(related="template_id.state", readonly=True) + section = fields.Selection( + selection=[("body", "Body")], + ) + name = fields.Char() + parameter_type = fields.Selection( + selection=[("text", "Text")], + ) + default_value = fields.Char() diff --git a/mail_gateway_whatsapp/readme/CONTRIBUTORS.rst b/mail_gateway_whatsapp/readme/CONTRIBUTORS.rst index 03dce01f7e..303bc78f4a 100644 --- a/mail_gateway_whatsapp/readme/CONTRIBUTORS.rst +++ b/mail_gateway_whatsapp/readme/CONTRIBUTORS.rst @@ -2,4 +2,5 @@ * Enric Tobella * `Tecnativa `_: - * Carlos Lopez \ No newline at end of file + * Carlos Lopez +* Frank Cespedes diff --git a/mail_gateway_whatsapp/security/ir.model.access.csv b/mail_gateway_whatsapp/security/ir.model.access.csv index 347b773514..e2aa4b35bd 100644 --- a/mail_gateway_whatsapp/security/ir.model.access.csv +++ b/mail_gateway_whatsapp/security/ir.model.access.csv @@ -2,3 +2,5 @@ id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink access_whatsapp_composer,access.whatsapp.composer,model_whatsapp_composer,base.group_user,1,1,1,0 access_mail_whatsapp_template_group_system,mail_whatsapp_template_group_system,model_mail_whatsapp_template,base.group_system,1,1,1,1 access_mail_whatsapp_template_group_user,mail_whatsapp_template_group_user,model_mail_whatsapp_template,base.group_user,1,0,0,0 +access_mail_whatsapp_template_variable_group_system,mail_whatsapp_template_variable_group_system,model_mail_whatsapp_template_variable,base.group_system,1,1,1,1 +access_mail_whatsapp_template_variable_group_user,mail_whatsapp_template_variable_group_user,model_mail_whatsapp_template_variable,base.group_user,1,0,0,0 diff --git a/mail_gateway_whatsapp/static/description/index.html b/mail_gateway_whatsapp/static/description/index.html index 2ad26e609a..540facfe8c 100644 --- a/mail_gateway_whatsapp/static/description/index.html +++ b/mail_gateway_whatsapp/static/description/index.html @@ -8,11 +8,10 @@ /* :Author: David Goodger (goodger@python.org) -:Id: $Id: html4css1.css 9511 2024-01-13 09:50:07Z milde $ +:Id: $Id: html4css1.css 8954 2022-01-20 10:10:25Z milde $ :Copyright: This stylesheet has been placed in the public domain. Default cascading style sheet for the HTML output of Docutils. -Despite the name, some widely supported CSS2 features are used. See https://docutils.sourceforge.io/docs/howto/html-stylesheets.html for how to customize this style sheet. @@ -275,7 +274,7 @@ margin-left: 2em ; margin-right: 2em } -pre.code .ln { color: gray; } /* line numbers */ +pre.code .ln { color: grey; } /* line numbers */ pre.code, code { background-color: #eeeeee } pre.code .comment, code .comment { color: #5C6576 } pre.code .keyword, code .keyword { color: #3B0D06; font-weight: bold } @@ -301,7 +300,7 @@ span.pre { white-space: pre } -span.problematic, pre.problematic { +span.problematic { color: red } span.section-subtitle { @@ -476,6 +475,8 @@

Contributors

+
  • Frank Cespedes <fcespedes@nuobit.es>

    +
  • @@ -485,9 +486,7 @@

    Other credits

    Maintainers

    This module is maintained by the OCA.

    - -Odoo Community Association - +Odoo Community Association

    OCA, or the Odoo Community Association, is a nonprofit organization whose mission is to support the collaborative development of Odoo features and promote its widespread use.

    diff --git a/mail_gateway_whatsapp/views/mail_whatsapp_template_variable_views.xml b/mail_gateway_whatsapp/views/mail_whatsapp_template_variable_views.xml new file mode 100644 index 0000000000..2a79b58ac0 --- /dev/null +++ b/mail_gateway_whatsapp/views/mail_whatsapp_template_variable_views.xml @@ -0,0 +1,24 @@ + + + + + mail.whatsapp.template.variable.view.tree.readonly + mail.whatsapp.template.variable + + + + + + + + + + diff --git a/mail_gateway_whatsapp/views/mail_whatsapp_template_views.xml b/mail_gateway_whatsapp/views/mail_whatsapp_template_views.xml index d0c79a3a57..036509c1f0 100644 --- a/mail_gateway_whatsapp/views/mail_whatsapp_template_views.xml +++ b/mail_gateway_whatsapp/views/mail_whatsapp_template_views.xml @@ -68,6 +68,7 @@ >variables or buttons.
    +