odoo14用odoobot给指定用户发消息
1.发送私聊消息最佳写法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | def _send_sys_message( self , user, message): """通过OdooBot给指定用户发送消息 :param user: 'res.users' 对象 :param message: str, 消息内容 """ # 获取OdooBot的partner_id odoobot_id = self .env[ 'ir.model.data' ].xmlid_to_res_id( "base.partner_root" ) # 获取OdooBot和用户的聊天频道 channel_partners = user.mapped( 'partner_id' ).ids channel = self .env[ 'mail.channel' ].sudo().search( [( 'channel_type' , '=' , 'chat' ), ( 'channel_partner_ids' , 'in' , channel_partners),( 'channel_partner_ids' , 'in' , [odoobot_id])], limit = 1 ) # 不存在则初始化聊天频道 if not channel: user.odoobot_state = 'not_initialized' channel = self .env[ 'mail.channel' ].with_user(user).init_odoobot() # 发送消息 channel.sudo().message_post( body = message, author_id = odoobot_id, message_type = "comment" , subtype_xmlid = "mail.mt_comment" , ) |
2.发送群聊消息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | @ormcache ( 'odoobot_id' ) def _get_channel( self ,odoobot_id): # find if a channel was opened for this user before channel = self .env[ 'mail.channel' ].sudo().search([ ( 'name' , '=' , '接口推送' ), ( 'channel_partner_ids' , 'in' , [ self .env.user.partner_id. id ]) ], limit = 1 , ) _logger.info(f 'find exist channer:{channel}' ) 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' : 'groups' , # 谁能关注组的活动 'channel_type' : 'channel' , # 渠道,群聊 'group_public_id' : self .env.ref( 'base.group_user' ). id , # 经授权的群组 'group_ids' : [( 6 , 0 , [ self .env.ref( 'api_log.api_log_user_group' ). id ])], # 自动订阅组 'email_send' : False , 'name' : f '接口推送' , 'description' : '用于群发接口调用相关的消息,调用日志写入接口日志表时会自动发消息通知群里' , 'display_name' : f '接口推送' , }) _logger.info(f 'create an api channel id: {channel}' ) return channel. id def _send_group_message( self , message): """ 发送群聊渠道消息 """ # 获取OdooBot的partner_id 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_id = self ._get_channel(odoobot_id) channel = self .env[ 'mail.channel' ].browse(channel_id) # 给群组发消息 channel.sudo().message_post( body = message, author_id = odoobot_id, message_type = "comment" , subtype = "mail.mt_comment" , ) |
发送群聊消息适合在系统中提前配置好群聊和群组成员,比如系统维护之类的消息推送给开发 人员,把所有开发人员拉个群,大家谁看到谁就处理了
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
2019-03-13 Kivy之常用的小知识
2019-03-13 Android SDK Manager国内无法更新的解决方案
2019-03-13 sqlite3增删改查简单封装
2019-03-13 PYTHON 对SQLITE3的简单使用
2019-03-13 kivy中bind的使用
2019-03-13 kivy中size和pos的使用