feat: add push message - wxpusher #98

This commit is contained in:
duorameng
2026-05-11 19:24:04 +08:00
parent 0dad9dd541
commit 3897bfc281
6 changed files with 150 additions and 0 deletions
+1
View File
@@ -74,4 +74,5 @@ const (
ChannelGotify = "Gotify"
ChannelPushPlus = "PushPlus"
ChannelVoceChat = "VoceChat"
ChannelWxPusher = "WxPusher"
)
@@ -0,0 +1,78 @@
package channels
import (
"fmt"
"github.com/engigu/baihu-panel/internal/sdk/message"
"strconv"
"strings"
)
type WxPusherChannel struct{ *BaseChannel }
func NewWxPusherChannel() Channel {
return &WxPusherChannel{NewBaseChannel(ChannelWxPusher, []string{FormatTypeText})}
}
func (c *WxPusherChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
appToken := config.GetString("app_token")
if appToken == "" {
return SendError("wxpusher config missing: app_token is required"), nil
}
uidsStr := config.GetString("uids")
topicIdsStr := config.GetString("topic_ids")
verifyPayTypeStr := config.GetString("verify_pay_type")
if uidsStr == "" && topicIdsStr == "" {
return SendError("wxpusher config missing: uids or topic_ids is required"), nil
}
var uids []string
if uidsStr != "" {
uids = strings.Split(uidsStr, ",")
for i := range uids {
uids[i] = strings.TrimSpace(uids[i])
}
}
var topicIds []int
if topicIdsStr != "" {
ids := strings.Split(topicIdsStr, ",")
for _, idStr := range ids {
idStr = strings.TrimSpace(idStr)
if id, err := strconv.Atoi(idStr); err == nil {
topicIds = append(topicIds, id)
}
}
}
verifyPayType := 0
if verifyPayTypeStr != "" {
if v, err := strconv.Atoi(verifyPayTypeStr); err == nil {
verifyPayType = v
}
}
_, formattedContent := c.FormatContent(msg)
// 如果有标题,将标题和内容合并
content := formattedContent
if msg.Title != "" {
content = fmt.Sprintf("%s\n\n%s", msg.Title, formattedContent)
}
cli := message.WxPusher{
AppToken: appToken,
Content: content,
ContentType: 1, // 仅支持文字
Uids: uids,
TopicIds: topicIds,
VerifyPayType: verifyPayType,
}
res, err := cli.Send()
if err != nil {
return ErrorResult(res, err), nil
}
return SuccessResult(res), nil
}
+2
View File
@@ -49,6 +49,7 @@ const (
ChannelGotify = channels.ChannelGotify
ChannelPushPlus = channels.ChannelPushPlus
ChannelVoceChat = channels.ChannelVoceChat
ChannelWxPusher = channels.ChannelWxPusher
)
// 重导出辅助函数
@@ -82,6 +83,7 @@ func init() {
RegisterChannel(ChannelAliyunSMS, func() Channel { return channels.NewAliyunSMSChannel() })
RegisterChannel(ChannelPushPlus, func() Channel { return channels.NewPushPlusChannel() })
RegisterChannel(ChannelVoceChat, func() Channel { return channels.NewVoceChatChannel() })
RegisterChannel(ChannelWxPusher, func() Channel { return channels.NewWxPusherChannel() })
}
// RegisterChannel 注册自定义渠道(可用于扩展)