odoo, 私信聊天

    @api.model
    def init_odoobot(self):
        if self.env.user.odoobot_state in [False, 'not_initialized']:
            odoobot_id = self.env['ir.model.data']._xmlid_to_res_id("base.partner_root")
            channel_info = self.channel_get([odoobot_id])
            channel = self.browse(channel_info['id'])
            message = _("Hello,<br/>Odoo's chat helps employees collaborate efficiently. I'm here to help you discover its features.<br/><b>Try to send me an emoji</b> <span class=\"o_odoobot_command\">:)</span>")
            channel.sudo().message_post(body=message, author_id=odoobot_id, message_type="comment", subtype_xmlid="mail.mt_comment")
            self.env.user.odoobot_state = 'onboarding_emoji'
            return channel

使用 频道的mail.channe 的  init_odoobot  来创建或者获取和一个用户的 对话频道, with_user 参数指定需要对话的用户

如下

 

def wkf_notice_node_users(self, node):
    #_logger.info('=========wkf_notice_node_users===============%s' % node)
    channel_obj = self.env['mail.channel']
    users = node.event_users
    if node.event_users_eval_str:
        users |= safe_eval(node.event_users_eval_str, {'self': self})

    for u in users:
        print('=====wkf_notice_node_users======', u)
        msg = "%s waiting you to approve: <a href='%s'>%s</a>" % (self._description, self.wkf_get_open_url(), self.name_get()[0][1])
        channel = channel_obj.search([('channel_partner_ids', '=', u.partner_id.id), ('channel_type', '=', 'chat'), ('public', '=', 'private')] , limit=1) # 搜索用户的私信频道, 注意,私信频道实际就是 [用户和 odooboot]2个人的私人对话, 由于odooboot 是固定的,所以只需要搜索一u.partner_id.id 一个即可,
        print('=====wkf_notice_node_users 2======', channel)
        if not channel: # 如果不存在,把用户设为没初始话,调用init_odoobot 方法,生成 用户的私信频道
            u.update({'odoobot_state': 'not_initialized'})
            channel = channel_obj.with_user(u).init_odoobot()

        #使用私信频道,发送送至消息
        channel.message_post(body=msg, message_type='notification', subtype_xmlid='mail.mt_comment')

 

posted on 2023-01-09 19:48  Odoo在中国  阅读(259)  评论(0编辑  收藏  举报

导航