Skip to content

Commit 356d50f

Browse files
pedrobaezacelm1990
authored andcommitted
[IMP] mail_gateway_whatsapp: Add support for messages in server actions
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
1 parent 2919bc6 commit 356d50f

6 files changed

Lines changed: 142 additions & 28 deletions

File tree

mail_gateway_whatsapp/README.rst

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
.. image:: https://odoo-community.org/readme-banner-image
2-
:target: https://odoo-community.org/get-involved?utm_source=readme
3-
:alt: Odoo Community Association
4-
51
=====================
62
Mail Whatsapp Gateway
73
=====================
@@ -17,7 +13,7 @@ Mail Whatsapp Gateway
1713
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
1814
:target: https://odoo-community.org/page/development-status
1915
:alt: Beta
20-
.. |badge2| image:: https://img.shields.io/badge/license-AGPL--3-blue.png
16+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
2117
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
2218
:alt: License: AGPL-3
2319
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fsocial-lightgray.png?logo=github

mail_gateway_whatsapp/__manifest__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33

44
{
55
"name": "Mail Whatsapp Gateway",
6-
"summary": """
7-
Set a gateway for whatsapp""",
6+
"summary": "Set a gateway for WhatsApp",
87
"version": "18.0.1.0.0",
98
"license": "AGPL-3",
109
"author": "Creu Blanca, Dixmit, Odoo Community Association (OCA)",
@@ -14,6 +13,7 @@
1413
"data": [
1514
"security/security.xml",
1615
"security/ir.model.access.csv",
16+
"views/ir_actions_server_views.xml",
1717
"wizards/whatsapp_composer.xml",
1818
"wizards/mail_compose_gateway_message.xml",
1919
"views/mail_whatsapp_template_views.xml",

mail_gateway_whatsapp/models/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from . import ir_actions_server
12
from . import mail_gateway
23
from . import mail_thread
34
from . import mail_gateway_whatsapp
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Copyright 2025 Tecnativa - Pedro M. Baeza
2+
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
3+
import markupsafe
4+
5+
from odoo import api, exceptions, fields, models
6+
7+
8+
class IrServerAction(models.Model):
9+
_inherit = "ir.actions.server"
10+
11+
state = fields.Selection(
12+
selection_add=[("whatsapp", "WhatsApp message")],
13+
ondelete={"whatsapp": "cascade"},
14+
)
15+
whatsapp_gateway_id = fields.Many2one(
16+
comodel_name="mail.gateway", domain=[("gateway_type", "=", "whatsapp")]
17+
)
18+
whatsapp_partner = fields.Char()
19+
whatsapp_template_id = fields.Many2one(
20+
comodel_name="mail.whatsapp.template",
21+
domain="[('gateway_id', '=', whatsapp_gateway_id),('model_id', '=', model_id)]",
22+
)
23+
24+
@api.depends("state")
25+
def _compute_available_model_ids(self):
26+
gateway_based = self.filtered(lambda action: action.state == "whatsapp")
27+
if gateway_based:
28+
mail_models = self.env["ir.model"].search(
29+
[("is_mail_thread", "=", True), ("transient", "=", False)]
30+
)
31+
gateway_based.available_model_ids = mail_models.ids
32+
return super(
33+
IrServerAction, self - gateway_based
34+
)._compute_available_model_ids()
35+
36+
@api.constrains("state", "model_id")
37+
def _check_whatsapp_model_coherency(self):
38+
for action in self:
39+
if action.state == "whatsapp" and (
40+
action.model_id.transient or not action.model_id.is_mail_thread
41+
):
42+
raise exceptions.ValidationError(
43+
self.env._(
44+
"Sending WhatsApp message can only be done on a non transient "
45+
"mail.thread model"
46+
)
47+
)
48+
49+
def _run_action_whatsapp_multi(self, eval_context=None):
50+
# Method called by upstream in ir.actions~_get_runner() using naming convention
51+
context = self.env.context
52+
if (
53+
not self.whatsapp_template_id
54+
or (not context.get("active_ids") and not context.get("active_id"))
55+
or self._is_recompute()
56+
):
57+
return False
58+
res_ids = context.get("active_ids", [context.get("active_id")])
59+
for record in self.env[self.model_id.model].browse(res_ids):
60+
partner_id = int(
61+
self.env["mail.render.mixin"]._render_template(
62+
self.whatsapp_partner, record._name, [record.id]
63+
)[record.id]
64+
)
65+
partner = self.env["res.partner"].browse(partner_id)
66+
partner._whatsapp_get_channel("mobile", self.whatsapp_gateway_id)
67+
gateway_channel = partner.gateway_channel_ids.filtered(
68+
lambda x: x.gateway_id == self.whatsapp_gateway_id
69+
)
70+
body = markupsafe.Markup(
71+
self.whatsapp_template_id.with_context(
72+
default_res_id=record.id
73+
).render_body_message()
74+
)
75+
# pylint: disable=translation-required
76+
record.with_context(
77+
whatsapp_template_id=self.whatsapp_template_id.id, res_id=record.id
78+
).message_post(
79+
body=body,
80+
subtype_xmlid="mail.mt_comment",
81+
message_type="comment",
82+
gateway_notifications=[
83+
{
84+
"channel_type": "gateway",
85+
"gateway_channel_id": gateway_channel.id,
86+
"partner_id": partner_id,
87+
}
88+
],
89+
)
90+
return False

mail_gateway_whatsapp/static/description/index.html

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
55
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
6-
<title>README.rst</title>
6+
<title>Mail Whatsapp Gateway</title>
77
<style type="text/css">
88

99
/*
@@ -360,21 +360,16 @@
360360
</style>
361361
</head>
362362
<body>
363-
<div class="document">
363+
<div class="document" id="mail-whatsapp-gateway">
364+
<h1 class="title">Mail Whatsapp Gateway</h1>
364365

365-
366-
<a class="reference external image-reference" href="https://odoo-community.org/get-involved?utm_source=readme">
367-
<img alt="Odoo Community Association" src="https://odoo-community.org/readme-banner-image" />
368-
</a>
369-
<div class="section" id="mail-whatsapp-gateway">
370-
<h1>Mail Whatsapp Gateway</h1>
371366
<!-- !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
372367
!! This file is generated by oca-gen-addon-readme !!
373368
!! changes will be overwritten. !!
374369
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
375370
!! source digest: sha256:a7bdf09868559f4a3a765f239ee66aec81889e5a9f1a29249cfd54f418a52143
376371
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
377-
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/license-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/social/tree/18.0/mail_gateway_whatsapp"><img alt="OCA/social" src="https://img.shields.io/badge/github-OCA%2Fsocial-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/social-18-0/social-18-0-mail_gateway_whatsapp"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/social&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
372+
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/social/tree/18.0/mail_gateway_whatsapp"><img alt="OCA/social" src="https://img.shields.io/badge/github-OCA%2Fsocial-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/social-18-0/social-18-0-mail_gateway_whatsapp"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/social&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
378373
<p>This module allows to respond whatsapp chats.</p>
379374
<p>This way, a group of users can respond customers or any other set of
380375
partners in an integrated way.</p>
@@ -399,9 +394,9 @@ <h1>Mail Whatsapp Gateway</h1>
399394
</ul>
400395
</div>
401396
<div class="section" id="configuration">
402-
<h2><a class="toc-backref" href="#toc-entry-1">Configuration</a></h2>
397+
<h1><a class="toc-backref" href="#toc-entry-1">Configuration</a></h1>
403398
<div class="section" id="first-steps">
404-
<h3><a class="toc-backref" href="#toc-entry-2">First steps</a></h3>
399+
<h2><a class="toc-backref" href="#toc-entry-2">First steps</a></h2>
405400
<p>You need to create a WhatsApp Business Account (WABA), a Meta App and
406401
define a phone number. You can follow this
407402
<a class="reference external" href="https://developers.facebook.com/micro_site/url/?click_from_context_menu=true&amp;country=ES&amp;destination=https%3A%2F%2Fwww.facebook.com%2Fbusiness%2Fhelp%2F2087193751603668&amp;event_type=click&amp;last_nav_impression_id=0m3TRxrxOlly1eRmB&amp;max_percent_page_viewed=22&amp;max_viewport_height_px=1326&amp;max_viewport_width_px=2560&amp;orig_http_referrer=https%3A%2F%2Fdevelopers.facebook.com%2Fdocs%2Fwhatsapp%2Fcloud-api%2Fget-started-for-bsps%3Flocale%3Den_US&amp;orig_request_uri=https%3A%2F%2Fdevelopers.facebook.com%2Fajax%2Fpagelet%2Fgeneric.php%2FDeveloperNotificationsPayloadPagelet%3Ffb_dtsg_ag%3D--sanitized--%26data%3D%257B%2522businessUserID%2522%253Anull%252C%2522cursor%2522%253Anull%252C%2522length%2522%253A15%252C%2522clientRequestID%2522%253A%2522js_k6%2522%257D%26__usid%3D6-Trd7hi4itpm%253APrd7ifiub2tvy%253A0-Ard7g9twdm0p1-RV%253D6%253AF%253D%26locale%3Den_US%26jazoest%3D24920&amp;region=emea&amp;scrolled=false&amp;session_id=1jLoVJNU6iVMaw3ml&amp;site=developers">steps</a>.</p>
@@ -410,7 +405,7 @@ <h3><a class="toc-backref" href="#toc-entry-2">First steps</a></h3>
410405
<p>In order to make the webhook accessible, the system must be public.</p>
411406
</div>
412407
<div class="section" id="configure-the-gateway">
413-
<h3><a class="toc-backref" href="#toc-entry-3">Configure the gateway</a></h3>
408+
<h2><a class="toc-backref" href="#toc-entry-3">Configure the gateway</a></h2>
414409
<p>Once you have created the Meta App, you need to add the gateway and
415410
webhook. In order to make it you must follow this steps:</p>
416411
<ul class="simple">
@@ -443,40 +438,40 @@ <h3><a class="toc-backref" href="#toc-entry-3">Configure the gateway</a></h3>
443438
</div>
444439
</div>
445440
<div class="section" id="usage">
446-
<h2><a class="toc-backref" href="#toc-entry-4">Usage</a></h2>
441+
<h1><a class="toc-backref" href="#toc-entry-4">Usage</a></h1>
447442
<ol class="arabic simple">
448443
<li>Access Gateway</li>
449444
<li>Wait until someone starts a conversation.</li>
450445
<li>Now you will be able to respond and receive messages to this person.</li>
451446
</ol>
452447
</div>
453448
<div class="section" id="known-issues-roadmap">
454-
<h2><a class="toc-backref" href="#toc-entry-5">Known issues / Roadmap</a></h2>
449+
<h1><a class="toc-backref" href="#toc-entry-5">Known issues / Roadmap</a></h1>
455450
<p><strong>WhatsApp templates</strong></p>
456451
<ul class="simple">
457452
<li>Add support for Variables</li>
458453
<li>Add support for Buttons</li>
459454
</ul>
460455
</div>
461456
<div class="section" id="bug-tracker">
462-
<h2><a class="toc-backref" href="#toc-entry-6">Bug Tracker</a></h2>
457+
<h1><a class="toc-backref" href="#toc-entry-6">Bug Tracker</a></h1>
463458
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/social/issues">GitHub Issues</a>.
464459
In case of trouble, please check there if your issue has already been reported.
465460
If you spotted it first, help us to smash it by providing a detailed and welcomed
466461
<a class="reference external" href="https://github.com/OCA/social/issues/new?body=module:%20mail_gateway_whatsapp%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
467462
<p>Do not contact contributors directly about support or help with technical issues.</p>
468463
</div>
469464
<div class="section" id="credits">
470-
<h2><a class="toc-backref" href="#toc-entry-7">Credits</a></h2>
465+
<h1><a class="toc-backref" href="#toc-entry-7">Credits</a></h1>
471466
<div class="section" id="authors">
472-
<h3><a class="toc-backref" href="#toc-entry-8">Authors</a></h3>
467+
<h2><a class="toc-backref" href="#toc-entry-8">Authors</a></h2>
473468
<ul class="simple">
474469
<li>Creu Blanca</li>
475470
<li>Dixmit</li>
476471
</ul>
477472
</div>
478473
<div class="section" id="contributors">
479-
<h3><a class="toc-backref" href="#toc-entry-9">Contributors</a></h3>
474+
<h2><a class="toc-backref" href="#toc-entry-9">Contributors</a></h2>
480475
<ul>
481476
<li><p class="first">Olga Marco &lt;<a class="reference external" href="mailto:olga.marco&#64;creublanca.es">olga.marco&#64;creublanca.es</a>&gt;</p>
482477
</li>
@@ -492,12 +487,12 @@ <h3><a class="toc-backref" href="#toc-entry-9">Contributors</a></h3>
492487
</ul>
493488
</div>
494489
<div class="section" id="other-credits">
495-
<h3><a class="toc-backref" href="#toc-entry-10">Other credits</a></h3>
490+
<h2><a class="toc-backref" href="#toc-entry-10">Other credits</a></h2>
496491
<p>This work has been funded by AEOdoo (Asociación Española de Odoo -
497492
<a class="reference external" href="https://www.aeodoo.org">https://www.aeodoo.org</a>)</p>
498493
</div>
499494
<div class="section" id="maintainers">
500-
<h3><a class="toc-backref" href="#toc-entry-11">Maintainers</a></h3>
495+
<h2><a class="toc-backref" href="#toc-entry-11">Maintainers</a></h2>
501496
<p>This module is maintained by the OCA.</p>
502497
<a class="reference external image-reference" href="https://odoo-community.org">
503498
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
@@ -510,6 +505,5 @@ <h3><a class="toc-backref" href="#toc-entry-11">Maintainers</a></h3>
510505
</div>
511506
</div>
512507
</div>
513-
</div>
514508
</body>
515509
</html>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<odoo>
3+
<record id="ir_actions_server_view_form" model="ir.ui.view">
4+
<field
5+
name="name"
6+
>ir.actions.server.view.form.inherit.mail_gateway_whatsapp</field>
7+
<field name="model">ir.actions.server</field>
8+
<field name="inherit_id" ref="base.view_server_action_form" />
9+
<field name="arch" type="xml">
10+
<xpath expr="//field[@name='link_field_id']" position="after">
11+
<field
12+
name="whatsapp_gateway_id"
13+
string="Gateway"
14+
invisible="state != 'whatsapp'"
15+
required="state == 'whatsapp'"
16+
/>
17+
<field
18+
name="whatsapp_partner"
19+
placeholder="Insert here the Jinja expression to get the partner ID"
20+
string="Partner"
21+
invisible="state != 'whatsapp'"
22+
required="state == 'whatsapp'"
23+
/>
24+
<field
25+
name="whatsapp_template_id"
26+
string="WhatsApp template"
27+
invisible="state != 'whatsapp'"
28+
required="state == 'whatsapp'"
29+
/>
30+
</xpath>
31+
</field>
32+
</record>
33+
</odoo>

0 commit comments

Comments
 (0)