mirror of
https://github.com/simon-ding/polaris.git
synced 2026-06-06 01:57:40 +08:00
feat: add serverchan notify client
This commit is contained in:
15
pkg/notifier/clients_test.go
Normal file
15
pkg/notifier/clients_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package notifier
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestServerChan(t *testing.T) {
|
||||
s, err := NewServerChanClient(``)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
err = s.SendMsg("test")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,7 @@ func init() {
|
||||
handler.Store("dingtalk", NewDingTalkClient)
|
||||
handler.Store("telegram", NewTelegramClient)
|
||||
handler.Store("bark", NewbarkClient)
|
||||
handler.Store("serverchan", NewServerChanClient)
|
||||
}
|
||||
|
||||
func Gethandler(name string) (HandlerFunc, bool) {
|
||||
|
||||
89
pkg/notifier/serverchan.go
Normal file
89
pkg/notifier/serverchan.go
Normal file
@@ -0,0 +1,89 @@
|
||||
package notifier
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
type ServerChanConfig struct {
|
||||
Key string `json:"key"`
|
||||
}
|
||||
|
||||
func NewServerChanClient(s string) (NotificationClient, error) {
|
||||
var cfg ServerChanConfig
|
||||
if err := json.Unmarshal([]byte(s), &cfg); err != nil {
|
||||
return nil, errors.Wrap(err, "json")
|
||||
}
|
||||
|
||||
return &ServerChan{Key: cfg.Key}, nil
|
||||
}
|
||||
|
||||
type ServerChan struct {
|
||||
Key string
|
||||
}
|
||||
|
||||
func (s *ServerChan) SendMsg(msg string) error {
|
||||
return scSend("Polaris", msg, s.Key)
|
||||
}
|
||||
|
||||
func scSend(text string, desp string, key string) error {
|
||||
data := url.Values{}
|
||||
data.Set("text", text)
|
||||
data.Set("desp", desp)
|
||||
|
||||
// 根据 sendkey 是否以 "sctp" 开头决定 API 的 URL
|
||||
var apiUrl string
|
||||
if strings.HasPrefix(key, "sctp") {
|
||||
// 使用正则表达式提取数字部分
|
||||
re := regexp.MustCompile(`sctp(\d+)t`)
|
||||
matches := re.FindStringSubmatch(key)
|
||||
if len(matches) > 1 {
|
||||
num := matches[1]
|
||||
apiUrl = fmt.Sprintf("https://%s.push.ft07.com/send/%s.send", num, key)
|
||||
} else {
|
||||
return errors.New("invalid sendkey format for sctp")
|
||||
}
|
||||
} else {
|
||||
apiUrl = fmt.Sprintf("https://sctapi.ftqq.com/%s.send", key)
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("POST", apiUrl, strings.NewReader(data.Encode()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
d, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var r response
|
||||
if err := json.Unmarshal(d, &r); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if r.Code != 0 {
|
||||
return errors.New(r.Message)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type response struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
}
|
||||
Reference in New Issue
Block a user