Odoo: How to send a Inbox message to Specific User?
14 September, 2021 by
Odoo: How to send a Inbox message to Specific User?
Administrator
| No comments yet

Maybe you can use the odoobot user to send the user a chat message, below is an example. They will get a notification in Odoo and will be able to see it in the discuss app.

from odoo import models, fields, api, _


class MyModel(models.Model):
    _inherit = ['mail.thread']

    def inbox_message(self):
        """
        Send user chat notification on picking validation.
        """
        
        # construct the message that is to be sent to the user
        message_text = f'<strong>Title</strong> ' \
                       f'<p>This picking has been validated!</p>'

        # odoo runbot
        odoobot_id = self.env['ir.model.data'].sudo().xmlid_to_res_id("base.partner_root")

        # find if a channel was opened for this user before
        channel = self.env['mail.channel'].sudo().search([
            ('name', '=', 'Picking Validated'),
            ('channel_partner_ids', 'in', [self.env.user.partner_id.id])
        ],
            limit=1,
        )

        if not channel:
            # create a new channel
            channel = self.env['mail.channel'].with_context(mail_create_nosubscribe=True).sudo().create({
                'channel_partner_ids': [(4, self.env.user.partner_id.id), (4, odoobot_id)],
                'public': 'private',
                'channel_type': 'chat',
                'email_send': False,
                'name': f'Picking Validated',
                'display_name': f'Picking Validated',
            })

        # send a message to the related user
        channel.sudo().message_post(
            body=message_text,
            author_id=odoobot_id,
            message_type="comment",
            subtype="mail.mt_comment",
        )

Odoo: How to send a Inbox message to Specific User?
Administrator
14 September, 2021
Share this post
Archive
Sign in to leave a comment