feat: add pushplus
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type PushPlus struct {
|
||||
Token string `json:"token"`
|
||||
Topic string `json:"topic,omitempty"`
|
||||
Template string `json:"template,omitempty"`
|
||||
Channel string `json:"channel,omitempty"`
|
||||
Webhook string `json:"webhook,omitempty"`
|
||||
CallbackUrl string `json:"callbackUrl,omitempty"`
|
||||
To string `json:"to,omitempty"`
|
||||
}
|
||||
|
||||
type pushPlusData struct {
|
||||
PushPlus
|
||||
Title string `json:"title"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type pushPlusResponse struct {
|
||||
Code int `json:"code"`
|
||||
Msg string `json:"msg"`
|
||||
Data string `json:"data"`
|
||||
}
|
||||
|
||||
func (p *PushPlus) Request(title, content string) (string, error) {
|
||||
url := "https://www.pushplus.plus/send"
|
||||
|
||||
data := pushPlusData{
|
||||
PushPlus: *p,
|
||||
Title: title,
|
||||
Content: content,
|
||||
}
|
||||
|
||||
body, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
resp, err := http.Post(url, "application/json", bytes.NewBuffer(body))
|
||||
if err != nil {
|
||||
// Try old URL if first one fails or as fallback
|
||||
urlOld := "http://pushplus.hxtrip.com/send"
|
||||
resp, err = http.Post(urlOld, "application/json", bytes.NewBuffer(body))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
respBody, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var res pushPlusResponse
|
||||
if err := json.Unmarshal(respBody, &res); err != nil {
|
||||
return string(respBody), err
|
||||
}
|
||||
|
||||
if res.Code == 200 {
|
||||
return string(respBody), nil
|
||||
}
|
||||
|
||||
return string(respBody), fmt.Errorf("PushPlus error: %s (code: %d)", res.Msg, res.Code)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package channels
|
||||
|
||||
import "github.com/engigu/baihu-panel/internal/sdk/message"
|
||||
|
||||
type PushPlusChannel struct{ *BaseChannel }
|
||||
|
||||
func NewPushPlusChannel() Channel {
|
||||
return &PushPlusChannel{NewBaseChannel(ChannelPushPlus, []string{FormatTypeText, FormatTypeHTML, FormatTypeMarkdown})}
|
||||
}
|
||||
|
||||
func (c *PushPlusChannel) Send(config ChannelConfig, msg *Message) (*Result, error) {
|
||||
token := config.GetString("token")
|
||||
if token == "" {
|
||||
return SendError("pushplus config missing: token is required"), nil
|
||||
}
|
||||
|
||||
cli := message.PushPlus{
|
||||
Token: token,
|
||||
Topic: config.GetString("topic"),
|
||||
Template: config.GetString("template"),
|
||||
Channel: config.GetString("channel"),
|
||||
Webhook: config.GetString("webhook"),
|
||||
CallbackUrl: config.GetString("callback_url"),
|
||||
To: config.GetString("to"),
|
||||
}
|
||||
|
||||
res, err := cli.Request(msg.Title, msg.Text)
|
||||
if err != nil {
|
||||
return ErrorResult(string(res), err), nil
|
||||
}
|
||||
return SuccessResult(string(res)), nil
|
||||
}
|
||||
@@ -72,4 +72,5 @@ const (
|
||||
ChannelPushMe = "PushMe"
|
||||
ChannelNtfy = "Ntfy"
|
||||
ChannelGotify = "Gotify"
|
||||
ChannelPushPlus = "PushPlus"
|
||||
)
|
||||
|
||||
@@ -47,6 +47,7 @@ const (
|
||||
ChannelPushMe = channels.ChannelPushMe
|
||||
ChannelNtfy = channels.ChannelNtfy
|
||||
ChannelGotify = channels.ChannelGotify
|
||||
ChannelPushPlus = channels.ChannelPushPlus
|
||||
)
|
||||
|
||||
// 重导出辅助函数
|
||||
@@ -78,6 +79,7 @@ func init() {
|
||||
RegisterChannel(ChannelCustom, func() Channel { return channels.NewCustomChannel() })
|
||||
RegisterChannel(ChannelWeChatOFAccount, func() Channel { return channels.NewWeChatOFAccountChannel() })
|
||||
RegisterChannel(ChannelAliyunSMS, func() Channel { return channels.NewAliyunSMSChannel() })
|
||||
RegisterChannel(ChannelPushPlus, func() Channel { return channels.NewPushPlusChannel() })
|
||||
}
|
||||
|
||||
// RegisterChannel 注册自定义渠道(可用于扩展)
|
||||
|
||||
@@ -50,6 +50,7 @@ var SupportedChannelTypes = []map[string]string{
|
||||
{"type": messenger.ChannelPushMe, "label": "PushMe"},
|
||||
// {"type": messenger.ChannelWeChatOFAccount, "label": "微信公众号"},
|
||||
{"type": messenger.ChannelAliyunSMS, "label": "阿里云短信"},
|
||||
{"type": messenger.ChannelPushPlus, "label": "PushPlus"},
|
||||
}
|
||||
|
||||
// SupportedEvents 支持的事件类型
|
||||
|
||||
@@ -113,6 +113,15 @@ const channelConfigFields: Record<string, { key: string; label: string; required
|
||||
{ key: 'phone_number', label: '手机号码', required: true },
|
||||
{ key: 'template_code', label: '短信模板 CODE', required: true },
|
||||
],
|
||||
PushPlus: [
|
||||
{ key: 'token', label: 'Token', required: true, placeholder: 'PushPlus Token' },
|
||||
{ key: 'topic', label: '群组编码', required: false, placeholder: '可选,一对多推送' },
|
||||
{ key: 'template', label: '推送模板', required: false, placeholder: 'html, txt, json, markdown' },
|
||||
{ key: 'channel', label: '推送渠道', required: false, placeholder: 'wechat, dingding, feishu, mail等' },
|
||||
{ key: 'webhook', label: 'Webhook', required: false },
|
||||
{ key: 'callback_url', label: '回调地址', required: false },
|
||||
{ key: 'to', label: '好友令牌', required: false },
|
||||
],
|
||||
}
|
||||
|
||||
// 加载数据
|
||||
|
||||
Reference in New Issue
Block a user