# -*- coding: utf-8 -*- from odoo import models, fields, api import logging _logger = logging.getLogger(__name__) # -*- coding: utf-8-*- import re ##过滤HTML中的标签 #将HTML中标签等信息去掉 #@param htmlstr HTML字符串. def filter_tags(htmlstr): #先过滤CDATA re_cdata=re.compile('//]*//\]\]>',re.I) #匹配CDATA re_script=re.compile('<\s*script[^>]*>[^<]*<\s*/\s*script\s*>',re.I)#Script re_style=re.compile('<\s*style[^>]*>[^<]*<\s*/\s*style\s*>',re.I)#style re_br=re.compile('')#处理换行 re_h=re.compile(']*>')#HTML标签 re_comment=re.compile('')#HTML注释 s=re_cdata.sub('',htmlstr)#去掉CDATA s=re_script.sub('',s) #去掉SCRIPT s=re_style.sub('',s)#去掉style s=re_br.sub('\n',s)#将br转换为换行 s=re_h.sub('',s) #去掉HTML 标签 s=re_comment.sub('',s)#去掉HTML注释 #去掉多余的空行 blank_line=re.compile('\n+') s=blank_line.sub('\n',s) s=replaceCharEntity(s)#替换实体 return s ##替换常用HTML字符实体. #使用正常的字符替换HTML中特殊的字符实体. #你可以添加新的实体字符到CHAR_ENTITIES中,处理更多HTML字符实体. #@param htmlstr HTML字符串. def replaceCharEntity(htmlstr): CHAR_ENTITIES={'nbsp':' ','160':' ', 'lt':'<','60':'<', 'gt':'>','62':'>', 'amp':'&','38':'&', 'quot':'"','34':'"',} re_charEntity=re.compile(r'&#?(?P\w+);') sz=re_charEntity.search(htmlstr) while sz: entity=sz.group()#entity全称,如> key=sz.group('name')#去除&;后entity,如>为gt try: htmlstr=re_charEntity.sub(CHAR_ENTITIES[key],htmlstr,1) sz=re_charEntity.search(htmlstr) except KeyError: #以空串代替 htmlstr=re_charEntity.sub('',htmlstr,1) sz=re_charEntity.search(htmlstr) return htmlstr def repalce(s,re_exp,repl_string): return re_exp.sub(repl_string,s) class MailMessage(models.Model): _inherit = 'mail.message' we_is_send = fields.Selection([('-1', '不需要发送'), ('0', '需要发送'), ('1', '已发送')], string='订订消息发送状态', default='0', index=True) we_error_msg = fields.Char('消息不发送原因') def send_we_message(self): """ 定时批量发送钉钉消息 :return: """ messages = self.search([('we_is_send', '=', '0')]) if messages and len(messages) > 0: try: messages.send_message_to_we() except Exception as ex: pass def send_message_to_we(self): """ :param agent_id: 必填,企业应用的id,整型。可在应用的设置页面查看。 :param user_ids: 成员ID列表(消息接收者,多个接收者用‘|’分隔,最多支持1000个)。 :param title: 标题,不超过128个字节,超过会自动截断。 :param description: 必填,描述,不超过512个字节,超过会自动截断 :param url: 必填,点击后跳转的链接。 :param btntxt: 按钮文字。 默认为“详情”, 不超过4个文字,超过自动截断。 :param party_ids: 部门ID列表。 :param tag_ids: 标签ID列表。 :return: """ # 获取配置信息 config = self.env['ir.config_parameter'].sudo() # 获取wechat wechat,agent_id = self.env['we.config'].sudo().get_odoo_wechat() for m in self: we_user = m.get_we_user() if we_user and len(we_user)>0: try: _logger.info('wechat:%s' % wechat) model = self.env['ir.model'].search([('model', '=', m.model)], limit=1) title = "[%s] %s" % (model.name, m.record_name or m.subject or '') description = filter_tags(m.body) if len(filter_tags(m.body)) > 0 else '有新的状态和数据变更,请注意跟进.' url = '{base_url}/web#id={id}&model={model}&view_type=form'.format( base_url=config.get_param('web.base.url'), id=m.res_id, model=m.model) _logger.info('description:%s title:%s' % (description, title)) ret = wechat.message.send_text_card(agent_id, we_user , title, description, url, btntxt='点此跟进..', party_ids='', tag_ids='') _logger.info('message_id:%s ret:%s' % (m.message_id, ret)) m.write({'we_is_send':'1','we_error_msg':''}) except Exception as ex: m.we_error_msg = '向企业微信发送消息失败! 消息编号:%s 原因:%s' % (m.message_id, ex) _logger.error('向企业微信发送消息失败! 消息编号:%s 原因:%s' % (m.message_id, ex)) else: m.write({'we_is_send': '-1', 'we_error_msg': '无法发送,没有收件人!'}) _logger.info('message_id:%s 不需要发送,没有收件人!' % m.message_id) return False