Skip to content

[19.0][MIG] mail_gateway_whatsapp: Migration to 19.0 - #321

Open
CarlosRoca13 wants to merge 41 commits into
OCA:19.0from
Tecnativa:19.0-mig-mail_gateway_whatsapp
Open

CarlosRoca13 wants to merge 41 commits into
OCA:19.0from
Tecnativa:19.0-mig-mail_gateway_whatsapp

Conversation

@CarlosRoca13

Copy link
Copy Markdown
Contributor

cc @Tecnativa

This PR is superseding #203 to resolve comments. The migration work is respected

ping @pedrobaeza @etobella @carlos-lopez-tecnativa

DucTruongKomit and others added 15 commits October 27, 2026 17:06
The webhook route is public, so the transaction default environment is
the public user. Using with_user() was not enough: when the ORM writes
a relation to a model that forbids sudo commands (like res.partner), it
checks the access with the transaction user. Incoming messages from a
new contact then failed with an AccessError when webhook_user_id was a
regular internal user. Use request.update_env() instead, so the
transaction runs as the webhook user.
The webhook user processes the incoming messages, so it needs the
access of a gateway user (e.g. to link the authors to their partner).
Restrict the selection to users of the Gateway / User group and
document it.
Allow to send documents, images and so on
add mark on message
Improve security
Pass logic to components
Add tests
Manage change of version of Facebook API
Currently translated at 4.1% (4 of 96 strings)

Translation: social-16.0/social-16.0-mail_gateway_whatsapp
Translate-URL: https://translation.odoo-community.org/projects/social-16-0/social-16-0-mail_gateway_whatsapp/es/
…e company wants to initiate a conversation with a customer, a template must be used; otherwise, messages will not be sent. This also applies when the last conversation with the customer is older than 24 hours.

- Enabled downloading templates from META
- Added the ability to create templates directly in Odoo.

Note: Currently, templates with variables or buttons are not supported.
…ated to the current user

- Use OdooBot for webhooks instead of the current user, following the same logic as Odoo.
- Avoid automatically adding the current user as a member of a new gateway to prevent notifications for all messages. This is now an explicit configuration.
Currently translated at 100.0% (210 of 210 strings)

Translation: social-16.0/social-16.0-mail_gateway_whatsapp
Translate-URL: https://translation.odoo-community.org/projects/social-16-0/social-16-0-mail_gateway_whatsapp/it/
@CarlosRoca13
CarlosRoca13 force-pushed the 19.0-mig-mail_gateway_whatsapp branch from 115d379 to 812566b Compare October 27, 2026 16:07
@OCA-git-bot OCA-git-bot added series:19.0 mod:mail_gateway Module mail_gateway mod:mail_gateway_whatsapp Module mail_gateway_whatsapp labels Oct 27, 2026
@pedrobaeza

Copy link
Copy Markdown
Member

/ocabot migration mail_gateway_whatsapp

Does this contains the latest commits from 19 (the security one)? Please also squash together the commits from the same translator.

@OCA-git-bot OCA-git-bot added this to the 19.0 milestone Oct 27, 2026
@OCA-git-bot OCA-git-bot mentioned this pull request Oct 27, 2026
23 of 39 tasks
chienandalu and others added 8 commits October 27, 2026 17:25
…s on failures

When a message fails for any reason, the POST request that creates the message in META does not contain any status.
However, META notifies the status via webhook.
This commit handles this notification, records the error, and notifies the user.
…nt to the API In the previous PR OCA/social#1717 we handled the notification via webhook when Meta returns an error. However, when the error occurs in the communication with Meta, the message is displayed as delivered. After this commit, the message is displayed as error when applicable.

The failure details are only written when failure_type = 'unknown'. See:
https://github.com/OCA/social/blob/695efd6fa3401614118c6a52f58453b40a9f0842/mail_gateway/models/mail_message.py#L114 So, in exception cases, we write this field to ensure it is processed correctly.
You may want to send messages through the WhatsApp gateway in your
server actions the same you are able to send mails in the linked
documents.

This also allows to select this kind of server action on automation
rules (base_automation).

TT57123
You may have several contacts with the same mobile number.
mymage and others added 16 commits October 27, 2026 17:25
Currently translated at 100.0% (254 of 254 strings)

Translation: social-18.0/social-18.0-mail_gateway_whatsapp
Translate-URL: https://translation.odoo-community.org/projects/social-18-0/social-18-0-mail_gateway_whatsapp/it/
…th that mark, discuss will show an audio player instead of a download button. For reference, this is where the "voice" key is detected and the attachment is processed as such:

https://github.com/odoo/odoo/blob/3a54f1e4b706d6732d8dc16bc206f5fc4d6ab923/addons/mail/models/discuss/ir_attachment.py#L29-L32
Currently translated at 100.0% (254 of 254 strings)

Translation: social-18.0/social-18.0-mail_gateway_whatsapp
Translate-URL: https://translation.odoo-community.org/projects/social-18-0/social-18-0-mail_gateway_whatsapp/es/
When a message from whatsapp arrives and it's a response from a message sent from Odoo, set the parent_id field.
… body. Currently, attachment captions are disregarded.
Currently translated at 100.0% (254 of 254 strings)

Translation: social-18.0/social-18.0-mail_gateway_whatsapp
Translate-URL: https://translation.odoo-community.org/projects/social-18-0/social-18-0-mail_gateway_whatsapp/es/
Currently translated at 100.0% (255 of 255 strings)

Translation: social-18.0/social-18.0-mail_gateway_whatsapp
Translate-URL: https://translation.odoo-community.org/projects/social-18-0/social-18-0-mail_gateway_whatsapp/it/
…record

Nothing enforces uniqueness on (gateway_id, gateway_token), so two webhooks
delivered in parallel for the same unknown number each create their own
record. The unbounded searches in _get_author then return both, and every
later message of that correspondent fails on ensure_one(): the webhook
answers 500 and the delivery is lost.

limit=1 bounds both searches. The default _order is id, so the oldest record
wins.
Incoming text was passed raw to message_post. mail_thread only escapes a str
body, and escape() does not turn newlines into markup, so a multi-line message
was archived as a single block and its URLs were not clickable. The assembled
body (text plus media captions) now goes through plaintext2html before both
message_post calls of _process_update, mirroring the outgoing path where the
body is sent as html2plaintext(body).

The same root cause affected the location branch: the anchor was built as a
str, so escape() rendered it as literal HTML instead of a link. It is now built
as Markup, with % interpolation so the payload coordinates stay escaped.

A test covers a multi-line incoming message carrying a location: line breaks
rendered, blank line kept as a paragraph break, URL turned into a link,
markup-looking text left literal, and the location anchor kept as a link.
WhatsApp clients mark up text with *bold*, _italic_, ~strikethrough~ and
```monospace```. Incoming, those markers were archived as literal characters.
Outgoing, html2plaintext sent <strong> as *bold*, which WhatsApp happens to
read as bold, but <em> as /italic/, which it shows as is, and dropped the
other tags.

Both ways now use the same table. Incoming, the markers are turned into html
tags after plaintext2html, leaving the tags and links it produced alone.
Outgoing, the formatting tags are turned back into markers before
html2plaintext.

Tests cover an incoming message carrying markers and a link whose url holds an
underscore, and an outgoing message carrying bold and italic.
…late

In 19.0 the Message model has notification_ids instead of notifications, so the template raised an error for every message in Discuss.
@CarlosRoca13
CarlosRoca13 force-pushed the 19.0-mig-mail_gateway_whatsapp branch from 812566b to afa5fc2 Compare October 27, 2026 16:26
Port of OCA/social#1937.

The webhook is processed as the webhook user, which may not be an
administrator. Read the gateway and search the partner gateway channels
with sudo so that a regular gateway user can process the webhook.

Tests now use a gateway user as webhook user and fail if the HTTP
requests do not succeed.
@CarlosRoca13
CarlosRoca13 force-pushed the 19.0-mig-mail_gateway_whatsapp branch from afa5fc2 to 38688c5 Compare October 27, 2026 16:27
@CarlosRoca13

CarlosRoca13 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor Author

Yes, they were added But I didn't remember to separate them in a different commit. Now it should be all correct

@pedrobaeza

Copy link
Copy Markdown
Member

Does this include @vgavinas' comments?

@pedrobaeza
pedrobaeza requested a review from etobella October 27, 2026 16:40
Comment thread mail_gateway_whatsapp/readme/CONFIGURATION.md Outdated
@vgavinas

Copy link
Copy Markdown

Tested #321 at 38688c5 on Odoo 19.0 CE with a live WhatsApp Cloud API test number (mail_gateway and mail_gateway_whatsapp both from this branch):

  • Incoming text, image and voice work; the first message from a new number creates the channel without errors.
  • With webhook_user_id set to a regular internal user (admin, not base.user_root) there is no 403 any more, so the "process webhooks as the webhook user" fix works.
  • Outgoing text and PDF are delivered, and the wamid is stored on the notification.
  • message.xml now uses notification_ids. I couldn't check it in Discuss itself, because Discuss is still empty due to [19.0] mail_gateway: Discuss shows no messages (thread action conditions re-render the owner) #319 (separate mail_gateway issue).

Still open, maybe worth a note in the description: delivery statuses (delivered / read) from the webhook are not stored. The notification stays "sent" after the message has been read on the phone.

So my comments from #203 are covered, except the delivery statuses.

@CarlosRoca13

Copy link
Copy Markdown
Contributor Author

Hi @vgavinas

Can you check if it is covered now?

Thanks

@vgavinas

Copy link
Copy Markdown

Re-tested at ac6a407 with the live WhatsApp Cloud API test number: yes, it's covered. An outgoing message got whatsapp_status = delivered a few seconds after sending, and read after I opened it on the phone (notification_status stays sent). Incoming text still works as before.

I couldn't check the new status display in Discuss itself, because Discuss is still empty due to #319.

Thanks!

@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 tested it locally, but I found some issues and some missing features. I think some of them could be handled in mail_gateway, but I'm commenting here because I tested it with this module.

When a regular user tries to open or leave a gateway, I get this AccessError:

Image

In the v19 migration of the mail_gateway module, mail.resend.message was removed because Odoo removed this wizard. However, when a message failed, this wizard was where the user could get more information about the error:
c0254e9#diff-a652fc8b86e436182d90818bcbaf15fb7f82af72a206ceb7bb0c77d74b8c86edL5

V18
Image

V19
Image

I expected to see more information when a message fails, something like this:

Image

The error is saved in mail.notification:

Image

Comment on lines +140 to +144
<field
name="field_name"
widget="DynamicModelFieldSelectorChar"
required="1"
options="{'model': 'model'}"

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.

This widget doesn't exist anymore. Please update it:

Suggested change
<field
name="field_name"
widget="DynamicModelFieldSelectorChar"
required="1"
options="{'model': 'model'}"
<field
name="field_name"
widget="field_selector"
required="1"
options="{'model': 'model', 'allow_properties': False}"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mod:mail_gateway_whatsapp Module mail_gateway_whatsapp mod:mail_gateway Module mail_gateway series:19.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.