feat: add custom webhook message
This commit is contained in:
@@ -34,6 +34,15 @@ func (sw *SendTaskInsService) ValidateDiffIns(ins models.SendTasksIns) (string,
|
||||
var Config models.InsDtalkConfig
|
||||
return "", Config
|
||||
}
|
||||
if ins.WayType == "Custom" {
|
||||
var Config models.InsCustomConfig
|
||||
err := json.Unmarshal([]byte(ins.Config), &Config)
|
||||
if err != nil {
|
||||
return "自定义webhook反序列化失败!", empty
|
||||
}
|
||||
_, Msg := app.CommonPlaygroundValid(Config)
|
||||
return Msg, Config
|
||||
}
|
||||
return "未知的渠道的config校验", empty
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/blinkbean/dingtalk"
|
||||
"message-nest/models"
|
||||
"message-nest/pkg/app"
|
||||
"message-nest/pkg/message"
|
||||
@@ -39,6 +38,12 @@ type WayDetailDTalk struct {
|
||||
Secret string `json:"secret" validate:"max=100" label:"钉钉加签秘钥"`
|
||||
}
|
||||
|
||||
// WayDetailCustom 自定义渠道
|
||||
type WayDetailCustom struct {
|
||||
Webhook string `json:"webhook" validate:"required,max=200" label:"自定义的webhook地址"`
|
||||
Body string `json:"body" validate:"max=2000" label:"自定义的请求体"`
|
||||
}
|
||||
|
||||
func (sw *SendWay) GetByID() (interface{}, error) {
|
||||
return models.GetWayByID(sw.ID)
|
||||
}
|
||||
@@ -103,6 +108,14 @@ func (sw *SendWay) ValidateDiffWay() (string, interface{}) {
|
||||
}
|
||||
_, Msg := app.CommonPlaygroundValid(dtalk)
|
||||
return Msg, dtalk
|
||||
} else if sw.Type == "Custom" {
|
||||
var custom WayDetailCustom
|
||||
err := json.Unmarshal([]byte(sw.Auth), &custom)
|
||||
if err != nil {
|
||||
return "自定义参数反序列化失败!", empty
|
||||
}
|
||||
_, Msg := app.CommonPlaygroundValid(custom)
|
||||
return Msg, custom
|
||||
}
|
||||
return fmt.Sprintf("未知的发信渠道校验: %s", sw.Type), empty
|
||||
}
|
||||
@@ -119,8 +132,24 @@ func (sw *SendWay) TestSendWay(msgObj interface{}) string {
|
||||
}
|
||||
dtalkAuth, ok := msgObj.(WayDetailDTalk)
|
||||
if ok {
|
||||
cli := dingtalk.InitDingTalkWithSecret(dtalkAuth.AccessToken, dtalkAuth.Secret)
|
||||
err := cli.SendTextMessage(testMsg + dtalkAuth.Keys)
|
||||
var cli = message.Dtalk{
|
||||
AccessToken: dtalkAuth.AccessToken,
|
||||
Secret: dtalkAuth.Secret,
|
||||
}
|
||||
_, err := cli.SendMessageText(testMsg + dtalkAuth.Keys)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("发送失败:%s", err)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
customAuth, ok := msgObj.(WayDetailCustom)
|
||||
if ok {
|
||||
var cli = message.CustomWebhook{}
|
||||
data, _ := json.Marshal(testMsg)
|
||||
dataStr := string(data)
|
||||
dataStr = strings.Trim(dataStr, "\"")
|
||||
bodyStr := strings.Replace(customAuth.Body, "TEXT", dataStr, -1)
|
||||
_, err := cli.Request(customAuth.Webhook, bodyStr)
|
||||
if err != nil {
|
||||
return fmt.Sprintf("发送失败:%s", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user