push plus推送减少推送次数,当数据达到十条后再一起推送

This commit is contained in:
johlanse 2022-05-14 12:11:55 +08:00
parent d12dad7ae8
commit 08e49b4ad4
2 changed files with 22 additions and 7 deletions

View File

@ -213,8 +213,8 @@ func do(m string) {
}
}
//switch {
//case len(users) < 1:
// switch {
// case len(users) < 1:
// log.Infoln("未检测到有效用户信息,将采用登录模式")
// u, err := core.L(config.Retry.Times)
// if err != nil {
@ -259,4 +259,5 @@ func do(m string) {
}
message := "学习完成:今日得分:" + strconv.Itoa(score.TodayScore)
core.Push("markdown", message)
core.Push("flush", "学习完成")
}

View File

@ -2,6 +2,7 @@ package push
import (
"fmt"
"strings"
"github.com/guonaihong/gout"
log "github.com/sirupsen/logrus"
@ -12,14 +13,13 @@ type PushPlus struct {
}
func (p *PushPlus) Init() func(kind, message string) {
return func(kind, message string) {
if kind == "image" {
message = fmt.Sprintf("![](%v)", "data:image/png;base64,"+message)
}
var datas []string
send := func(data string) {
err := gout.POST("http://www.pushplus.plus/send").SetJSON(gout.H{
"token": p.Token,
"title": "study_xxqg",
"content": message,
"content": data,
"template": "markdown",
"channel": "wechat",
}).Do()
@ -28,4 +28,18 @@ func (p *PushPlus) Init() func(kind, message string) {
return
}
}
return func(kind, message string) {
if kind == "image" {
message = fmt.Sprintf("![](%v)", "data:image/png;base64,"+message)
send(message)
} else if kind == "flush" {
send(strings.Join(datas, "\n"))
} else {
datas = append(datas, message)
if len(datas) > 10 {
send(strings.Join(datas, "\n"))
}
}
}
}