Odoo:如何向特定用户发送收件箱消息?

问题描述

我需要在调用函数时发送收件箱消息,但我收到错误...

from odoo import models,fields,api,_

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

    
    def inBox_message(self):
        notification_ids = [(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)

它返回了这个错误An error occurred when sending an email

解决方法

也许您可以使用 odoobot 用户向用户发送聊天消息,下面是一个示例。他们将在 Odoo 中收到通知,并且可以在讨论应用中看到它。

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