Odoo 12:如何向特定用户发送收件箱消息?
Odoo 12: How to send a Inbox message to Specific User?
调用此函数时需要发送收件箱消息,但我收到错误...
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)
它返回了这个错误:
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",
)
不幸的是,上述(与 odoo 15 兼容的一些改编)不起作用。由于无法计算 Odoobot 的头像,它会抛出错误。当我将 odoobot_id 更改为具有头像的用户 ID 时,它有效
调用此函数时需要发送收件箱消息,但我收到错误...
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)
它返回了这个错误:
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",
)
不幸的是,上述(与 odoo 15 兼容的一些改编)不起作用。由于无法计算 Odoobot 的头像,它会抛出错误。当我将 odoobot_id 更改为具有头像的用户 ID 时,它有效