-
-
Notifications
You must be signed in to change notification settings - Fork 618
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[16.0][IMP] mail_gateway_whatsapp: Add support for WhatsApp templates #1497
Draft
carlos-lopez-tecnativa
wants to merge
2
commits into
OCA:16.0
Choose a base branch
from
Tecnativa:16.0-mail_gateway_whatsapp-add-templates
base: 16.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from . import models | ||
from . import tools | ||
|
||
# from . import services | ||
from . import wizards |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
# Copyright 2024 Tecnativa - Carlos López | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
import re | ||
|
||
import requests | ||
from werkzeug.urls import url_join | ||
|
||
from odoo import api, fields, models | ||
from odoo.exceptions import UserError | ||
from odoo.tools import ustr | ||
|
||
from odoo.addons.http_routing.models.ir_http import slugify | ||
|
||
from ..tools.const import supported_languages | ||
from .mail_gateway import BASE_URL | ||
|
||
|
||
class MailWhatsAppTemplate(models.Model): | ||
_name = "mail.whatsapp.template" | ||
_description = "Mail WhatsApp template" | ||
|
||
name = fields.Char(required=True) | ||
body = fields.Text(required=True) | ||
header = fields.Char() | ||
footer = fields.Char() | ||
template_name = fields.Char( | ||
compute="_compute_template_name", store=True, copy=False | ||
) | ||
is_supported = fields.Boolean(copy=False) | ||
template_uid = fields.Char(readonly=True, copy=False) | ||
category = fields.Selection( | ||
[ | ||
("authentication", "Authentication"), | ||
("marketing", "Marketing"), | ||
("utility", "Utility"), | ||
], | ||
required=True, | ||
) | ||
state = fields.Selection( | ||
[ | ||
("draft", "Draft"), | ||
("pending", "Pending"), | ||
("approved", "Approved"), | ||
("in_appeal", "In Appeal"), | ||
("rejected", "Rejected"), | ||
("pending_deletion", "Pending Deletion"), | ||
("deleted", "Deleted"), | ||
("disabled", "Disabled"), | ||
("paused", "Paused"), | ||
("limit_exceeded", "Limit Exceeded"), | ||
("archived", "Archived"), | ||
], | ||
default="draft", | ||
required=True, | ||
) | ||
language = fields.Selection(supported_languages, required=True) | ||
gateway_id = fields.Many2one( | ||
"mail.gateway", | ||
domain=[("gateway_type", "=", "whatsapp")], | ||
required=True, | ||
ondelete="cascade", | ||
) | ||
company_id = fields.Many2one( | ||
"res.company", related="gateway_id.company_id", store=True | ||
) | ||
|
||
_sql_constraints = [ | ||
( | ||
"unique_name_gateway_id", | ||
"unique(name, language, gateway_id)", | ||
"Duplicate name is not allowed for Gateway.", | ||
) | ||
] | ||
|
||
@api.depends("name", "state", "template_uid") | ||
def _compute_template_name(self): | ||
for template in self: | ||
if not template.template_name or ( | ||
template.state == "draft" and not template.template_uid | ||
): | ||
template.template_name = re.sub( | ||
r"\W+", "_", slugify(template.name or "") | ||
) | ||
|
||
def button_back2draft(self): | ||
self.write({"state": "draft"}) | ||
|
||
def button_export_template(self): | ||
self.ensure_one() | ||
gateway = self.gateway_id | ||
template_url = url_join( | ||
BASE_URL, | ||
f"v{gateway.whatsapp_version}/{gateway.whatsapp_account_id}/message_templates", | ||
) | ||
try: | ||
payload = self._prepare_values_to_export() | ||
response = requests.post( | ||
template_url, | ||
headers={"Authorization": "Bearer %s" % gateway.token}, | ||
json=payload, | ||
timeout=10, | ||
) | ||
response.raise_for_status() | ||
json_data = response.json() | ||
self.write( | ||
{ | ||
"template_uid": json_data.get("id"), | ||
"state": json_data.get("status").lower(), | ||
"is_supported": True, | ||
} | ||
) | ||
except requests.exceptions.HTTPError as ex: | ||
msj = f"{ustr(ex)} \n{ex.response.text}" | ||
raise UserError(msj) from ex | ||
except Exception as err: | ||
raise UserError(ustr(err)) from err | ||
|
||
def _prepare_values_to_export(self): | ||
components = self._prepare_components_to_export() | ||
return { | ||
"name": self.template_name, | ||
"category": self.category.upper(), | ||
"language": self.language, | ||
"components": components, | ||
} | ||
|
||
def _prepare_components_to_export(self): | ||
components = [{"type": "BODY", "text": self.body}] | ||
if self.header: | ||
components.append( | ||
{ | ||
"type": "HEADER", | ||
"format": "text", | ||
"text": self.header, | ||
} | ||
) | ||
if self.footer: | ||
components.append( | ||
{ | ||
"type": "FOOTER", | ||
"text": self.footer, | ||
} | ||
) | ||
# TODO: add more components(buttons, location, etc) | ||
return components | ||
|
||
def button_sync_template(self): | ||
self.ensure_one() | ||
gateway = self.gateway_id | ||
template_url = url_join( | ||
BASE_URL, | ||
f"{self.template_uid}", | ||
) | ||
try: | ||
response = requests.get( | ||
template_url, | ||
headers={"Authorization": "Bearer %s" % gateway.token}, | ||
timeout=10, | ||
) | ||
response.raise_for_status() | ||
json_data = response.json() | ||
vals = self._prepare_values_to_import(gateway, json_data) | ||
self.write(vals) | ||
except Exception as err: | ||
raise UserError(str(err)) from err | ||
return { | ||
"type": "ir.actions.client", | ||
"tag": "reload", | ||
} | ||
|
||
@api.model | ||
def _prepare_values_to_import(self, gateway, json_data): | ||
vals = { | ||
"name": json_data.get("name").replace("_", " ").title(), | ||
"template_name": json_data.get("name"), | ||
"category": json_data.get("category").lower(), | ||
"language": json_data.get("language"), | ||
"state": json_data.get("status").lower(), | ||
"template_uid": json_data.get("id"), | ||
"gateway_id": gateway.id, | ||
} | ||
is_supported = True | ||
for component in json_data.get("components", []): | ||
if component["type"] == "HEADER" and component["format"] == "TEXT": | ||
vals["header"] = component["text"] | ||
elif component["type"] == "BODY": | ||
vals["body"] = component["text"] | ||
elif component["type"] == "FOOTER": | ||
vals["footer"] = component["text"] | ||
else: | ||
is_supported = False | ||
vals["is_supported"] = is_supported | ||
return vals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
* Olga Marco <olga.marco@creublanca.es> | ||
* Enric Tobella <etobella@creublanca.es> | ||
* `Tecnativa <https://www.tecnativa.com>`_: | ||
|
||
* Carlos Lopez |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this changes are the reason behind the test issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The CI has been failing for several days on all PRs/commits in V16.
The last commit is unrelated to WhatsApp templates. If you prefer, I can create a new PR for it separately. However, this commit is not linked to the tests but addresses a different case, please refer to the commit description for more details.
Let me know what you think.