第十八章 消息总线
odoo内部集成了IM功能,即可以通过消息的方式即时通知其他用户,完成用户协作. 本章将详细介绍odoo内部的消息机制.
mail.thread 对象
message_post
如果我们想要在代码中给某个用户发送消息, 那么我们可以使用message_post方法. 我们先来看message_post方法的定义:
@api.returns('mail.message', lambda value: value.id)
def message_post(self, *,
body='', subject=None, message_type='notification',
email_from=None, author_id=None, parent_id=False,
subtype_id=False, subtype=None, partner_ids=None, channel_ids=None,
attachments=None, attachment_ids=None,
add_sign=True, record_name=False,
**kwargs):
""" Post a new message in an existing thread, returning the new
mail.message ID.
:param str body: body of the message, usually raw HTML that will
be sanitized
:param str subject: subject of the message
:param str message_type: see mail_message.message_type field. Can be anything but
user_notification, reserved for message_notify
:param int parent_id: handle thread formation
:param int subtype_id: subtype_id of the message, mainly use fore
followers mechanism
:param int subtype: xmlid that will be used to compute subtype_id
if subtype_id is not given.
:param list(int) partner_ids: partner_ids to notify
:param list(int) channel_ids: channel_ids to notify
:param list(tuple(str,str), tuple(str,str, dict) or int) attachments : list of attachment tuples in the form
``(name,content)`` or ``(name,content, info)``, where content is NOT base64 encoded
:param list id attachment_ids: list of existing attachement to link to this message
-Should only be setted by chatter
-Attachement object attached to mail.compose.message(0) will be attached
to the related document.
Extra keyword arguments will be used as default column values for the
new mail.message record.
:return int: ID of newly created mail.message
"""
从方法定义上面,我们可以看到,message_post方法将在已经存在的消息线程中将body中的内容发送给目标用户,并返回消息的id.
举个例子,假设我们想要在某条业务线中,当业务指定了用户接受任务时,给该用户发送消息,那么我们就可以使用message_post方法将组织好的消息文本发送给该用户:
msg = "你有新的测试单,请及时完成"
self.message_post(body=msg, partner_ids=partner_id.ids)
- body: 经过转义后的HTML代码
- partner_ids: 接收消息的合作伙伴id列表
- subtype_id: 消息的子类型,用于预先关注机制