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

@ -259,4 +259,5 @@ func do(m string) {
} }
message := "学习完成:今日得分:" + strconv.Itoa(score.TodayScore) message := "学习完成:今日得分:" + strconv.Itoa(score.TodayScore)
core.Push("markdown", message) core.Push("markdown", message)
core.Push("flush", "学习完成")
} }

View File

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