watcher/pkg/pusher/anPush.go
2024-04-10 17:36:56 +08:00

35 lines
800 B
Go

package pusher
import (
"fmt"
"github.com/go-resty/resty/v2"
"haitao_watcher/pkg/options"
"strings"
)
type AnPush struct {
opt *options.AnPushOption
client *resty.Client
}
func NewAnPush(opt *options.AnPushOption) *AnPush {
return &AnPush{
opt: opt,
client: resty.New().SetHeader("Content-Type", "application/x-www-form-urlencoded"),
}
}
type msgResponse struct {
Errcode int `json:"errcode"`
Errmsg string `json:"errmsg"`
Msgid uint64 `json:"msgid"`
}
func (a *AnPush) Push(title, content string) error {
r := a.client.R().SetBody(strings.NewReader(fmt.Sprintf("title=%s&content=%s&channel=%s", title, content, a.opt.Channel)))
var result msgResponse
r.SetResult(&result)
_, err := r.Post(fmt.Sprintf("https://api.anpush.com/push/%s", a.opt.Token))
return err
}