添加自定义消息推送

This commit is contained in:
johlanse 2022-09-03 20:01:51 +08:00
parent f2dfb8421e
commit eca573fe05
5 changed files with 82 additions and 1 deletions

View File

@ -94,6 +94,11 @@ type Config struct {
// 热重载
HotReload bool `json:"hot_reload" yaml:"hot_reload" mapstructure:"hot_reload"`
// 自定义消息推送
CustomMessage string `json:"custom_message" yaml:"custom_message" mapstructure:"custom_message"`
CustomCron string `json:"custom_cron" yaml:"custom_cron" mapstructure:"custom_cron"`
version string `mapstructure:"version"`
}

View File

@ -126,6 +126,11 @@ github_proxy: https://github.com
# 是否开启配置文件热重载 ,在修改配置文件并保存后会自动重启
hot_reload: true
# 自定义消息推送,会在定时任务执行之前推送信息
custom_message: ""
# 自定义消息推送定时
custom_cron: ""
weekly_min_score:
ji_guang_push:

View File

@ -380,7 +380,6 @@ func (c *Core) RespondDaily(user *model.User, model string) {
err := FillBlank(page, tips)
if err != nil {
log.Errorln("填空题答题失败" + err.Error())
return
}
case strings.Contains(categoryText, "多选题"):

13
main.go
View File

@ -174,6 +174,19 @@ func main() {
}
getPush := push.GetPush(config)
getPush("", "flush", "学习强国助手已上线")
if config.CustomCron != "" {
c2 := cron.New()
_, err := c2.AddFunc(config.CustomCron, func() {
getPush("all", "flush", config.CustomMessage)
})
if err != nil {
log.Errorln("添加自定义定时消息推送错误" + err.Error())
return
}
c2.Run()
}
model.SetPush(getPush)
if !config.TG.Enable && config.Cron == "" && !config.Wechat.Enable {
log.Infoln("已采用普通学习模式")

View File

@ -159,6 +159,26 @@ func initWechat() {
(value.(WechatHandler))(r.FromUserName)
}()
})
wx.HandleFunc("text", func(wx *mp.WeiXin, w http.ResponseWriter, r *request.WeiXinRequest, timestamp, nonce string) {
if r.FromUserName != conf.GetConfig().Wechat.SuperOpenID {
log.Infoln("收到了微信文本消息,但不是管理员")
return
}
msg := strings.SplitN(r.Content, " ", 3)
if len(msg) < 3 {
return
} else {
if msg[0] == "发送" {
if msg[1] == "all" || msg[1] == "所有" {
sendMsg("all", msg[2])
} else {
sendMsg(msg[1], msg[2])
}
}
}
})
}
func handleGetOpenID(id string) {
@ -211,6 +231,45 @@ func handleRestart(id string) {
// @param message
//
func sendMsg(id, message string) {
if id == "all" {
userList, err := wx.GetUserList("")
if err != nil {
log.Errorln("获取关注列表错误")
return
}
url := ""
color := ""
if strings.Contains(message, "$$$") {
splits := strings.Split(message, "$$$")
message = splits[0]
url = splits[1]
if len(splits) == 3 {
color = splits[2]
}
}
for _, user := range userList.Data.OpenId {
m := map[string]interface{}{
"data": map[string]string{
"value": message,
},
}
data, _ := json.Marshal(m)
_, err = wx.SendTemplateMessage(&mp.TemplateMessage{
ToUser: user,
TemplateId: conf.GetConfig().Wechat.NormalTempID,
URL: url,
TopColor: color,
RawJSONData: data,
})
if err != nil {
log.Errorln("向用户" + user + "推送消息错误")
continue
}
}
}
// 登录消息单独采用模板发送
if strings.Contains(message, "login.xuexi.cn") {
_, err := wx.SendTemplateMessage(&mp.TemplateMessage{