feat: send message rewrite

This commit is contained in:
engigu
2025-11-17 22:33:59 +08:00
parent 953f744846
commit 0639f5ec6d
2 changed files with 173 additions and 51 deletions
@@ -0,0 +1,164 @@
package send_message_service
import (
"message-nest/models"
"message-nest/service/send_way_service"
)
// 消息类型常量定义
const (
MessageTypeEmail = "Email"
MessageTypeDtalk = "Dtalk"
MessageTypeQyWeiXin = "QyWeiXin"
MessageTypeCustom = "Custom"
MessageTypeWeChatOFAccount = "WeChatOFAccount"
MessageTypeMessageNest = "MessageNest"
)
// MessageHandler 消息处理器接口
type MessageHandler interface {
// Send 发送消息
// 返回:响应内容,错误信息
Send(msgObj interface{}, ins models.SendTasksIns, typeC string, title string, content string, url string) (string, string)
// GetType 返回该处理器支持的消息类型字符串
GetType() string
}
// MessageHandlerRegistry 消息处理器注册表
type MessageHandlerRegistry struct {
handlers map[string]MessageHandler
}
// NewMessageHandlerRegistry 创建新的消息处理器注册表
func NewMessageHandlerRegistry() *MessageHandlerRegistry {
return &MessageHandlerRegistry{
handlers: make(map[string]MessageHandler),
}
}
// Register 注册消息处理器
func (r *MessageHandlerRegistry) Register(handler MessageHandler) {
r.handlers[handler.GetType()] = handler
}
// GetHandler 根据类型字符串获取对应的处理器
func (r *MessageHandlerRegistry) GetHandler(wayType string) (MessageHandler, bool) {
handler, ok := r.handlers[wayType]
return handler, ok
}
// 全局消息处理器注册表
var globalRegistry = NewMessageHandlerRegistry()
// init 初始化时注册所有处理器
func init() {
globalRegistry.Register(&EmailHandler{})
globalRegistry.Register(&DtalkHandler{})
globalRegistry.Register(&QyWeiXinHandler{})
globalRegistry.Register(&CustomHandler{})
globalRegistry.Register(&WeChatOfAccountHandler{})
globalRegistry.Register(&HostMessageHandler{})
}
// GetGlobalRegistry 获取全局注册表
func GetGlobalRegistry() *MessageHandlerRegistry {
return globalRegistry
}
// EmailHandler 邮箱消息处理器
type EmailHandler struct{}
func (h *EmailHandler) GetType() string {
return MessageTypeEmail
}
func (h *EmailHandler) Send(msgObj interface{}, ins models.SendTasksIns, typeC string, title string, content string, url string) (string, string) {
auth, ok := msgObj.(send_way_service.WayDetailEmail)
if !ok {
return "", "类型转换失败"
}
es := EmailService{}
errMsg := es.SendTaskEmail(auth, ins, typeC, title, content)
return "", errMsg
}
// DtalkHandler 钉钉消息处理器
type DtalkHandler struct{}
func (h *DtalkHandler) GetType() string {
return MessageTypeDtalk
}
func (h *DtalkHandler) Send(msgObj interface{}, ins models.SendTasksIns, typeC string, title string, content string, url string) (string, string) {
auth, ok := msgObj.(send_way_service.WayDetailDTalk)
if !ok {
return "", "类型转换失败"
}
es := DtalkService{}
return es.SendDtalkMessage(auth, ins, typeC, title, content)
}
// QyWeiXinHandler 企业微信消息处理器
type QyWeiXinHandler struct{}
func (h *QyWeiXinHandler) GetType() string {
return MessageTypeQyWeiXin
}
func (h *QyWeiXinHandler) Send(msgObj interface{}, ins models.SendTasksIns, typeC string, title string, content string, url string) (string, string) {
auth, ok := msgObj.(send_way_service.WayDetailQyWeiXin)
if !ok {
return "", "类型转换失败"
}
es := QyWeiXinService{}
return es.SendQyWeiXinMessage(auth, ins, typeC, title, content)
}
// CustomHandler 自定义webhook消息处理器
type CustomHandler struct{}
func (h *CustomHandler) GetType() string {
return MessageTypeCustom
}
func (h *CustomHandler) Send(msgObj interface{}, ins models.SendTasksIns, typeC string, title string, content string, url string) (string, string) {
auth, ok := msgObj.(send_way_service.WayDetailCustom)
if !ok {
return "", "类型转换失败"
}
cs := CustomService{}
return cs.SendCustomMessage(auth, ins, typeC, title, content)
}
// WeChatOfAccountHandler 微信公众号消息处理器
type WeChatOfAccountHandler struct{}
func (h *WeChatOfAccountHandler) GetType() string {
return MessageTypeWeChatOFAccount
}
func (h *WeChatOfAccountHandler) Send(msgObj interface{}, ins models.SendTasksIns, typeC string, title string, content string, url string) (string, string) {
auth, ok := msgObj.(send_way_service.WeChatOFAccount)
if !ok {
return "", "类型转换失败"
}
cs := WeChatOfAccountService{}
return cs.SendWeChatOfAccountMessage(auth, ins, typeC, title, content, url)
}
// HostMessageHandler 托管消息处理器
type HostMessageHandler struct{}
func (h *HostMessageHandler) GetType() string {
return MessageTypeMessageNest
}
func (h *HostMessageHandler) Send(msgObj interface{}, ins models.SendTasksIns, typeC string, title string, content string, url string) (string, string) {
auth, ok := msgObj.(send_way_service.MessageNest)
if !ok {
return "", "类型转换失败"
}
cs := HostMessageService{}
return cs.SendHostMessage(auth, ins, typeC, title, content)
}
+9 -51
View File
@@ -151,61 +151,19 @@ func (sm *SendMessageService) Send(task models.TaskIns) (string, error) {
continue
}
// 邮箱类型的实例发送
emailAuth, ok := msgObj.(send_way_service.WayDetailEmail)
if ok {
//continue
es := EmailService{}
errMsg := es.SendTaskEmail(emailAuth, ins.SendTasksIns, typeC, sm.Title, content)
sm.LogsAndStatusMark(sm.TransError(errMsg), errStrIsSuccess(errMsg))
// 使用注册的处理器发送消息
registry := GetGlobalRegistry()
handler, ok := registry.GetHandler(way.Type)
if !ok {
sm.LogsAndStatusMark(fmt.Sprintf("发送失败:未知渠道类型 %s 的发信实例: %s\n", way.Type, ins.ID), SendFail)
continue
}
// 钉钉类型的实例发送
dtalkAuth, ok := msgObj.(send_way_service.WayDetailDTalk)
if ok {
es := DtalkService{}
res, errMsg := es.SendDtalkMessage(dtalkAuth, ins.SendTasksIns, typeC, sm.Title, content)
res, errMsg := handler.Send(msgObj, ins.SendTasksIns, typeC, sm.Title, content, sm.URL)
if res != "" {
sm.LogsAndStatusMark(fmt.Sprintf("返回内容:%s", res), sm.Status)
sm.LogsAndStatusMark(sm.TransError(errMsg), errStrIsSuccess(errMsg))
continue
}
// 企业微信类型的实例发送
qywxAuth, ok := msgObj.(send_way_service.WayDetailQyWeiXin)
if ok {
es := QyWeiXinService{}
res, errMsg := es.SendQyWeiXinMessage(qywxAuth, ins.SendTasksIns, typeC, sm.Title, content)
sm.LogsAndStatusMark(fmt.Sprintf("返回内容:%s", res), sm.Status)
sm.LogsAndStatusMark(sm.TransError(errMsg), errStrIsSuccess(errMsg))
continue
}
// 自定义webhook类型的实例发送
customAuth, ok := msgObj.(send_way_service.WayDetailCustom)
if ok {
cs := CustomService{}
res, errMsg := cs.SendCustomMessage(customAuth, ins.SendTasksIns, typeC, sm.Title, content)
sm.LogsAndStatusMark(fmt.Sprintf("返回内容:%s", res), sm.Status)
sm.LogsAndStatusMark(sm.TransError(errMsg), errStrIsSuccess(errMsg))
continue
}
// 微信公众号模板消息的实例发送
wca, ok := msgObj.(send_way_service.WeChatOFAccount)
if ok {
cs := WeChatOfAccountService{}
res, errMsg := cs.SendWeChatOfAccountMessage(wca, ins.SendTasksIns, typeC, sm.Title, content, sm.URL)
sm.LogsAndStatusMark(fmt.Sprintf("返回内容:%s", res), sm.Status)
sm.LogsAndStatusMark(sm.TransError(errMsg), errStrIsSuccess(errMsg))
continue
}
// 托管消息的实例发送
mnt, ok := msgObj.(send_way_service.MessageNest)
if ok {
cs := HostMessageService{}
res, errMsg := cs.SendHostMessage(mnt, ins.SendTasksIns, typeC, sm.Title, content)
sm.LogsAndStatusMark(fmt.Sprintf("返回内容:%s", res), sm.Status)
sm.LogsAndStatusMark(sm.TransError(errMsg), errStrIsSuccess(errMsg))
continue
}
sm.LogsAndStatusMark(fmt.Sprintf("发送失败:未知渠道的发信实例: %s\n", ins.ID), SendFail)
sm.LogsAndStatusMark(sm.TransError(errMsg), errStrIsSuccess(errMsg))
}