feat: support dingtalk push
This commit is contained in:
@@ -44,6 +44,7 @@ func ClearLogs() {
|
||||
|
||||
// 启动注册清除任务定时任务
|
||||
func (cs *CronService) InitLogsCronRun() {
|
||||
// 注册任务
|
||||
setting, err := models.GetSettingByKey(constant.LogsCleanSectionName, constant.LogsCleanCronKeyName)
|
||||
if err != nil {
|
||||
logging.Logger.Error(fmt.Sprintf("获取日志的cron失败,原因:%s", err))
|
||||
@@ -52,6 +53,12 @@ func (cs *CronService) InitLogsCronRun() {
|
||||
Schedule: setting.Value,
|
||||
Job: ClearLogs,
|
||||
})
|
||||
|
||||
// 添加任务
|
||||
err = models.AddSendTaskWithID("日志定时清除", constant.CleanLogsTaskId, "admin")
|
||||
if err != nil {
|
||||
logging.Logger.Error(fmt.Sprintf("添加日志定时清除任务失败,原因:%s", err))
|
||||
}
|
||||
}
|
||||
|
||||
// 更新清除任务定时任务
|
||||
|
||||
@@ -30,6 +30,10 @@ func (sw *SendTaskInsService) ValidateDiffIns(ins models.SendTasksIns) (string,
|
||||
_, Msg := app.CommonPlaygroundValid(emailConfig)
|
||||
return Msg, emailConfig
|
||||
}
|
||||
if ins.WayType == "Dtalk" {
|
||||
var Config models.InsDtalkConfig
|
||||
return "", Config
|
||||
}
|
||||
return "未知的渠道的config校验", empty
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/blinkbean/dingtalk"
|
||||
"message-nest/models"
|
||||
"message-nest/pkg/app"
|
||||
"message-nest/pkg/message"
|
||||
@@ -33,7 +34,9 @@ type WayDetailEmail struct {
|
||||
|
||||
// WayDetailDTalk 钉钉渠道明细字段
|
||||
type WayDetailDTalk struct {
|
||||
WebhookUrl string `validate:"required,url" label:"钉钉webhookUrl地址"`
|
||||
AccessToken string `json:"access_token" validate:"required,max=100" label:"钉钉access_token"`
|
||||
Keys string `json:"keys" validate:"max=200" label:"钉钉关键字"`
|
||||
Secret string `json:"secret" validate:"max=100" label:"钉钉加签秘钥"`
|
||||
}
|
||||
|
||||
func (sw *SendWay) GetByID() (interface{}, error) {
|
||||
@@ -106,12 +109,22 @@ func (sw *SendWay) ValidateDiffWay() (string, interface{}) {
|
||||
|
||||
// TestSendWay 尝试带发信测试连通性
|
||||
func (sw *SendWay) TestSendWay(msgObj interface{}) string {
|
||||
testMsg := "This is a test message from message-nest."
|
||||
emailAuth, ok := msgObj.(WayDetailEmail)
|
||||
if ok {
|
||||
var emailer message.EmailMessage
|
||||
emailer.Init(emailAuth.Server, 465, emailAuth.Account, emailAuth.Passwd)
|
||||
errMsg := emailer.SendTextMessage(emailAuth.Account, "test", "This is a test email from message-nest.")
|
||||
errMsg := emailer.SendTextMessage(emailAuth.Account, "test email", testMsg)
|
||||
return errMsg
|
||||
}
|
||||
dtalkAuth, ok := msgObj.(WayDetailDTalk)
|
||||
if ok {
|
||||
cli := dingtalk.InitDingTalkWithSecret(dtalkAuth.AccessToken, dtalkAuth.Secret)
|
||||
err := cli.SendTextMessage(testMsg + dtalkAuth.Keys)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("发送失败:%s", err)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf("未知的发信渠道校验: %s", sw.Type)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user