feat: support dingtalk push
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package send_message_service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"message-nest/models"
|
||||
"message-nest/pkg/message"
|
||||
"message-nest/service/send_ins_service"
|
||||
"message-nest/service/send_way_service"
|
||||
)
|
||||
|
||||
type DtalkService struct {
|
||||
}
|
||||
|
||||
// SendDtalkMessage 执行发送钉钉
|
||||
func (s *DtalkService) SendDtalkMessage(auth send_way_service.WayDetailDTalk, ins models.SendTasksIns, typeC string, title string, content string) string {
|
||||
insService := send_ins_service.SendTaskInsService{}
|
||||
errStr, c := insService.ValidateDiffIns(ins)
|
||||
if errStr != "" {
|
||||
return errStr
|
||||
}
|
||||
_, ok := c.(models.InsDtalkConfig)
|
||||
if !ok {
|
||||
return "钉钉config校验失败"
|
||||
}
|
||||
|
||||
errMsg := ""
|
||||
cli := message.Dtalk{
|
||||
AccessToken: auth.AccessToken,
|
||||
Secret: auth.Secret,
|
||||
}
|
||||
if typeC == "text" {
|
||||
_, err := cli.SendMessageText(content)
|
||||
if err != nil {
|
||||
errMsg = fmt.Sprintf("发送失败:%s", ins.ContentType)
|
||||
}
|
||||
} else if typeC == "markdown" {
|
||||
_, err := cli.SendMessageMarkdown(title, content)
|
||||
if err != nil {
|
||||
errMsg = fmt.Sprintf("发送失败:%s", ins.ContentType)
|
||||
}
|
||||
} else {
|
||||
errMsg = fmt.Sprintf("未知的钉钉发送内容类型:%s", ins.ContentType)
|
||||
}
|
||||
return errMsg
|
||||
}
|
||||
@@ -12,7 +12,7 @@ type EmailService struct {
|
||||
}
|
||||
|
||||
// SendTaskEmail 执行发送邮件
|
||||
func (s *EmailService) SendTaskEmail(auth send_way_service.WayDetailEmail, ins models.SendTasksIns, typeC string, content string) string {
|
||||
func (s *EmailService) SendTaskEmail(auth send_way_service.WayDetailEmail, ins models.SendTasksIns, typeC string, title string, content string) string {
|
||||
insService := send_ins_service.SendTaskInsService{}
|
||||
errStr, c := insService.ValidateDiffIns(ins)
|
||||
if errStr != "" {
|
||||
@@ -27,9 +27,9 @@ func (s *EmailService) SendTaskEmail(auth send_way_service.WayDetailEmail, ins m
|
||||
errMsg := ""
|
||||
emailer.Init(auth.Server, auth.Port, auth.Account, auth.Passwd)
|
||||
if typeC == "text" {
|
||||
errMsg = emailer.SendTextMessage(config.ToAccount, config.Title, content)
|
||||
errMsg = emailer.SendTextMessage(config.ToAccount, title, content)
|
||||
} else if typeC == "html" {
|
||||
errMsg = emailer.SendHtmlMessage(config.ToAccount, config.Title, content)
|
||||
errMsg = emailer.SendHtmlMessage(config.ToAccount, title, content)
|
||||
} else {
|
||||
errMsg = fmt.Sprintf("未知的邮件发送内容类型:%s", ins.ContentType)
|
||||
}
|
||||
|
||||
@@ -23,6 +23,7 @@ func errStrIsSuccess(errStr string) int {
|
||||
|
||||
type SendMessageService struct {
|
||||
TaskID string
|
||||
Title string
|
||||
Text string
|
||||
HTML string
|
||||
MarkDown string
|
||||
@@ -46,6 +47,7 @@ func (sm *SendMessageService) Send() string {
|
||||
errStr := ""
|
||||
|
||||
sm.LogsAndStatusMark(fmt.Sprintf("开始任务[%s]的发送", sm.TaskID), sm.Status)
|
||||
sm.LogsAndStatusMark(fmt.Sprintf("发送标题:%s \n", sm.Title), sm.Status)
|
||||
|
||||
sendTaskService := send_task_service.SendTaskService{
|
||||
ID: sm.TaskID,
|
||||
@@ -93,13 +95,21 @@ func (sm *SendMessageService) Send() string {
|
||||
// 邮箱类型的实例发送
|
||||
emailAuth, ok := msgObj.(send_way_service.WayDetailEmail)
|
||||
if ok {
|
||||
//continue
|
||||
es := EmailService{}
|
||||
errMsg := es.SendTaskEmail(emailAuth, ins.SendTasksIns, typeC, content)
|
||||
errMsg := es.SendTaskEmail(emailAuth, ins.SendTasksIns, typeC, sm.Title, content)
|
||||
sm.LogsAndStatusMark(sm.TransError(errMsg), errStrIsSuccess(errMsg))
|
||||
continue
|
||||
}
|
||||
|
||||
sm.LogsAndStatusMark(fmt.Sprintf("未知渠道的发信实例: %s", ins.ID), sm.Status)
|
||||
// 钉钉类型的实例发送
|
||||
dtalkAuth, ok := msgObj.(send_way_service.WayDetailDTalk)
|
||||
if ok {
|
||||
es := DtalkService{}
|
||||
errMsg := es.SendDtalkMessage(dtalkAuth, ins.SendTasksIns, typeC, sm.Title, content)
|
||||
sm.LogsAndStatusMark(sm.TransError(errMsg), errStrIsSuccess(errMsg))
|
||||
continue
|
||||
}
|
||||
sm.LogsAndStatusMark(fmt.Sprintf("发送失败:未知渠道的发信实例: %s\n", ins.ID), SendFail)
|
||||
|
||||
}
|
||||
|
||||
@@ -165,6 +175,7 @@ func (sm *SendMessageService) GetSendMsg(ins models.SendTasksIns) (string, strin
|
||||
logging.Logger.Error("text节点数据为空!")
|
||||
return "text", ""
|
||||
} else {
|
||||
logging.Logger.Error(fmt.Sprintf("没有找到%s对应的消息,使用text消息替代!", ins.ContentType))
|
||||
return "text", content
|
||||
}
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user