feat: add custom webhook message

This commit is contained in:
engigu
2024-01-07 00:25:35 +08:00
parent e4a1b16e78
commit cf77fac836
18 changed files with 180 additions and 22 deletions
@@ -0,0 +1,31 @@
package send_message_service
import (
"encoding/json"
"fmt"
"message-nest/models"
"message-nest/pkg/message"
"message-nest/service/send_way_service"
"strings"
)
type CustomService struct {
}
// SendCustomMessage 执行发送钉钉
func (s *CustomService) SendCustomMessage(auth send_way_service.WayDetailCustom, ins models.SendTasksIns, typeC string, title string, content string) string {
errMsg := ""
cli := message.CustomWebhook{}
data, _ := json.Marshal(content)
dataStr := string(data)
dataStr = strings.Trim(dataStr, "\"")
bodyStr := strings.Replace(auth.Body, "TEXT", dataStr, -1)
_, err := cli.Request(auth.Webhook, bodyStr)
if err != nil {
errMsg = fmt.Sprintf("发送失败:%s", err)
}
return errMsg
}
+10 -2
View File
@@ -47,12 +47,12 @@ 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,
}
task, err := sendTaskService.GetTaskWithIns()
sm.LogsAndStatusMark(fmt.Sprintf("任务名称:%s", task.Name), sm.Status)
sm.LogsAndStatusMark(fmt.Sprintf("发送标题:%s \n", sm.Title), sm.Status)
if err != nil {
errStr = fmt.Sprintf("任务[%s]不存在!退出发送!", sm.TaskID)
sm.LogsAndStatusMark(errStr, SendFail)
@@ -109,6 +109,14 @@ func (sm *SendMessageService) Send() string {
sm.LogsAndStatusMark(sm.TransError(errMsg), errStrIsSuccess(errMsg))
continue
}
// 自定义webhook类型的实例发送
customAuth, ok := msgObj.(send_way_service.WayDetailCustom)
if ok {
cs := CustomService{}
errMsg := cs.SendCustomMessage(customAuth, ins.SendTasksIns, typeC, sm.Title, content)
sm.LogsAndStatusMark(sm.TransError(errMsg), errStrIsSuccess(errMsg))
continue
}
sm.LogsAndStatusMark(fmt.Sprintf("发送失败:未知渠道的发信实例: %s\n", ins.ID), SendFail)
}