feat: add wehchat template message push

This commit is contained in:
engigu
2024-03-05 14:42:12 +08:00
parent 32c1c8ad76
commit 4388d08109
16 changed files with 277 additions and 24 deletions
+9
View File
@@ -48,6 +48,15 @@ func (sw *SendTaskInsService) ValidateDiffIns(ins models.SendTasksIns) (string,
_, Msg := app.CommonPlaygroundValid(Config)
return Msg, Config
}
if ins.WayType == "WeChatOFAccount" {
var Config models.InsWeChatAccountConfig
err := json.Unmarshal([]byte(ins.Config), &Config)
if err != nil {
return "微信公众号发送配置反序列化失败!", empty
}
_, Msg := app.CommonPlaygroundValid(Config)
return Msg, Config
}
return "未知的渠道的config校验", empty
}
@@ -28,6 +28,7 @@ type SendMessageService struct {
Title string
Text string
HTML string
URL string
MarkDown string
CallerIp string
@@ -184,6 +185,15 @@ func (sm *SendMessageService) Send(task models.TaskIns) (string, error) {
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
}
sm.LogsAndStatusMark(fmt.Sprintf("发送失败:未知渠道的发信实例: %s\n", ins.ID), SendFail)
}
@@ -0,0 +1,42 @@
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 WeChatOfAccountService struct {
}
// SendWeChatOfAccountMessage 执行发送微信公众号模板消息
func (s *WeChatOfAccountService) SendWeChatOfAccountMessage(auth send_way_service.WeChatOFAccount, ins models.SendTasksIns, typeC string, title string, content string, url string) (string, string) {
insService := send_ins_service.SendTaskInsService{}
errStr, c := insService.ValidateDiffIns(ins)
if errStr != "" {
return errStr, ""
}
config, ok := c.(models.InsWeChatAccountConfig)
if !ok {
return "微信公众号模板消息config校验失败", ""
}
var (
err error
res string
errMsg string
)
cli := message.WeChatOFAccount{
AppID: auth.AppID,
AppSecret: auth.APPSecret,
TemplateID: auth.TempID,
ToUser: config.ToAccount,
URL: url,
}
res, err = cli.Send(title, content)
if err != nil {
errMsg = fmt.Sprintf("发送失败:%s", ins.ContentType)
}
return res, errMsg
}
+21 -11
View File
@@ -49,6 +49,13 @@ type WayDetailCustom struct {
Body string `json:"body" validate:"max=2000" label:"自定义的请求体"`
}
// WeChatOFAccount 微信公众号
type WeChatOFAccount struct {
AppID string `json:"appID" validate:"required,max=200" label:"微信公众号id"`
APPSecret string `json:"appsecret" validate:"max=2000" label:"微信公众号秘钥"`
TempID string `json:"tempid" validate:"max=2000" label:"模板消息id"`
}
func (sw *SendWay) GetByID() (interface{}, error) {
return models.GetWayByID(sw.ID)
}
@@ -155,6 +162,14 @@ func (sw *SendWay) ValidateDiffWay() (string, interface{}) {
}
_, Msg := app.CommonPlaygroundValid(custom)
return Msg, custom
} else if sw.Type == "WeChatOFAccount" {
var wca WeChatOFAccount
err := json.Unmarshal([]byte(sw.Auth), &wca)
if err != nil {
return "微信公众号反序列化失败!", empty
}
_, Msg := app.CommonPlaygroundValid(wca)
return Msg, wca
}
return fmt.Sprintf("未知的发信渠道校验: %s", sw.Type), empty
}
@@ -192,18 +207,13 @@ func (sw *SendWay) TestSendWay(msgObj interface{}) (string, string) {
}
return "", string(res)
}
customAuth, ok := msgObj.(WayDetailCustom)
_, ok = msgObj.(WeChatOFAccount)
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)
res, err := cli.Request(customAuth.Webhook, bodyStr)
if err != nil {
return fmt.Sprintf("发送失败:%s", err), string(res)
}
return "", string(res)
//var cli = message.WeChatOFAccount{
// AppID: wca.AppID,
// AppSecret: wca.APPSecret,
//}
return "微信公众号模板消息不用测试运行,请直接添加", ""
}
return fmt.Sprintf("未知的发信渠道校验: %s", sw.Type), ""
}