2

I need to send a inbox message when I call this function, but I just get error ...

from odoo import models, fields, api, _

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

    
    def inbox_message(self):
        notification_ids = [(0, 0, {
            'res_partner_id': self.user_id.partner_id.id,
            'notification_type': 'inbox'
        })]
        self.message_post(body='This picking has been validated!', message_type="notification",
                          subtype="mail.mt_comment", author_id=self.env.user.partner_id.id,
                          notification_ids=notification_ids)

And it's returning this error: An error occurred when sending an email

Keiko Mori
  • 157
  • 2
  • 15

2 Answers2

3

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",
        )


0

Unfortunately the above (with some adaptations to be compatible with odoo 15) is not working. It throws an error due to not being able to compute the avatar of Odoobot. When I change odoobot_id to be the ID of a user with an avatar, it works

FredNunes
  • 35
  • 8