Files
test/sg_wechat_enterprise/models/mail.py
2024-07-10 15:58:47 +08:00

121 lines
5.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- 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('//<!\[CDATA\[[^>]*//\]\]>',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('<br\s*?/?>')#处理换行
re_h=re.compile('</?\w+[^>]*>')#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<name>\w+);')
sz=re_charEntity.search(htmlstr)
while sz:
entity=sz.group()#entity全称如&gt;
key=sz.group('name')#去除&;后entity,如&gt;为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