feat: 添加tg命令study_all #8

This commit is contained in:
johlanse 2021-12-06 17:31:24 +08:00
parent 2a32b734c0
commit c27a86ac8d
1 changed files with 79 additions and 12 deletions

View File

@ -25,6 +25,7 @@ func init() {
newPlugin("/study", study) newPlugin("/study", study)
newPlugin("/get_scores", getScores) newPlugin("/get_scores", getScores)
newPlugin("/quit", quit) newPlugin("/quit", quit)
newPlugin("/study_all", studyAll)
} }
//Telegram //Telegram
@ -88,7 +89,8 @@ func (t *Telegram) Init() {
defer func() { defer func() {
err := recover() err := recover()
if err != nil { if err != nil {
log.Errorln(err)
log.Errorln("handle执行出现了不可挽回的错误")
} }
}() }()
(value.(func(bot *Telegram, args []string)))(t, strings.Split(update.Message.Text, " ")[1:]) (value.(func(bot *Telegram, args []string)))(t, strings.Split(update.Message.Text, " ")[1:])
@ -104,7 +106,8 @@ func (t *Telegram) Init() {
tgbotapi.BotCommand{Command: "get_users", Description: "获取所有cookie有效的用户"}, tgbotapi.BotCommand{Command: "get_users", Description: "获取所有cookie有效的用户"},
tgbotapi.BotCommand{Command: "study", Description: "对一个账户进行学习"}, tgbotapi.BotCommand{Command: "study", Description: "对一个账户进行学习"},
tgbotapi.BotCommand{Command: "get_scores", Description: "获取用户成绩"}, tgbotapi.BotCommand{Command: "get_scores", Description: "获取用户成绩"},
tgbotapi.BotCommand{Command: "quit", Description: "退出所有正在学习的实例"}, tgbotapi.BotCommand{Command: "quit", Description: "退出所有正在学习的实例,或者跟上实例ID退出对应实例"},
tgbotapi.BotCommand{Command: "study_all", Description: "对当前所有用户进行按顺序学习"},
)) ))
if err != nil { if err != nil {
return return
@ -130,7 +133,7 @@ func (t *Telegram) SendMsg(message string) {
} }
func login(bot *Telegram, args []string) { func login(bot *Telegram, args []string) {
log.Infoln(args) config := GetConfig()
go func() { go func() {
defer func() { defer func() {
err := recover() err := recover()
@ -142,16 +145,17 @@ func login(bot *Telegram, args []string) {
pw: nil, pw: nil,
browser: nil, browser: nil,
context: nil, context: nil,
ShowBrowser: false, ShowBrowser: config.ShowBrowser,
Push: func(kind string, message string) { Push: func(kind string, message string) {
if kind == "image" { switch {
case kind == "image":
bytes, _ := base64.StdEncoding.DecodeString(message) bytes, _ := base64.StdEncoding.DecodeString(message)
bot.SendPhoto(bytes) bot.SendPhoto(bytes)
} else if kind == "markdown" { case kind == "markdown":
newMessage := tgbotapi.NewMessage(bot.ChatId, message) newMessage := tgbotapi.NewMessage(bot.ChatId, message)
newMessage.ParseMode = tgbotapi.ModeMarkdownV2 newMessage.ParseMode = tgbotapi.ModeMarkdownV2
bot.bot.Send(newMessage) bot.bot.Send(newMessage)
} else { default:
bot.SendMsg(message) bot.SendMsg(message)
} }
}, },
@ -181,7 +185,62 @@ func getAllUser(bot *Telegram, args []string) {
bot.SendMsg(message) bot.SendMsg(message)
} }
func studyAll(bot *Telegram, args []string) {
config := GetConfig()
users, err := GetUsers()
if err != nil {
bot.SendMsg(err.Error())
return
}
if len(users) == 0 {
bot.SendMsg("未发现用户信息,请输入/login进行用户登录")
return
}
getAllUser(bot, args)
for _, user := range users {
s := func() {
core := Core{
pw: nil,
browser: nil,
context: nil,
ShowBrowser: config.ShowBrowser,
Push: func(kind string, message string) {
switch {
case kind == "image":
bytes, _ := base64.StdEncoding.DecodeString(message)
bot.SendPhoto(bytes)
case kind == "markdown":
newMessage := tgbotapi.NewMessage(bot.ChatId, message)
newMessage.ParseMode = tgbotapi.ModeMarkdownV2
_, _ = bot.bot.Send(newMessage)
default:
bot.SendMsg(message)
}
},
}
u := uuid.New().String()
bot.SendMsg("已创建运行实例:" + u)
datas.Store(u, &core)
defer datas.Delete(u)
core.Init()
defer core.Quit()
core.LearnArticle(user.Cookies)
core.LearnVideo(user.Cookies)
core.RespondDaily(user.Cookies, "daily")
core.RespondDaily(user.Cookies, "daily")
core.RespondDaily(user.Cookies, "weekly")
core.RespondDaily(user.Cookies, "special")
score, _ := GetUserScore(user.Cookies)
bot.SendMsg(fmt.Sprintf("%v当前学习总积分%v,今日得分:%v", user.Nick, score.TotalScore, score.TodayScore))
}
s()
}
}
func study(bot *Telegram, args []string) { func study(bot *Telegram, args []string) {
config := GetConfig()
users, err := GetUsers() users, err := GetUsers()
if err != nil { if err != nil {
bot.SendMsg(err.Error()) bot.SendMsg(err.Error())
@ -195,23 +254,23 @@ func study(bot *Telegram, args []string) {
bot.SendMsg("未发现用户信息,请输入/login进行用户登录") bot.SendMsg("未发现用户信息,请输入/login进行用户登录")
return return
} else { } else {
if len(args) < 0 { if 0 <= len(args) {
bot.SendMsg("存在多名用户,未输入用户序号")
return
} else {
i, err := strconv.Atoi(args[0]) i, err := strconv.Atoi(args[0])
if err != nil { if err != nil {
bot.SendMsg(err.Error()) bot.SendMsg(err.Error())
return return
} }
cookies = users[i].Cookies cookies = users[i].Cookies
} else {
bot.SendMsg("存在多名用户,未输入用户序号")
return
} }
} }
core := Core{ core := Core{
pw: nil, pw: nil,
browser: nil, browser: nil,
context: nil, context: nil,
ShowBrowser: false, ShowBrowser: config.ShowBrowser,
Push: func(kind string, message string) { Push: func(kind string, message string) {
switch { switch {
case kind == "image": case kind == "image":
@ -271,5 +330,13 @@ func quit(bot *Telegram, args []string) {
core.Quit() core.Quit()
return true return true
}) })
} else {
datas.Range(func(key, value interface{}) bool {
if key.(string) == args[0] {
core := value.(*Core)
core.Quit()
}
return true
})
} }
} }