diff --git a/control.go b/control.go index 18dc93b..66982b6 100644 --- a/control.go +++ b/control.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/tidwall/gjson" tb "gopkg.in/tucnak/telebot.v2" + "strconv" "strings" "time" ) @@ -72,7 +73,7 @@ func MSAppIsExist(tgId int64, clientId string) bool { data := QueryDataByTG(db, tgId) var res MSData for _, res = range data { - if res.msId == clientId { + if res.clientId == clientId { return true } } @@ -84,20 +85,37 @@ func SignTask() { fmt.Println("----Task Begin----") fmt.Println("Time:" + time.Now().Format("2006-01-02 15:04:05")) data := QueryDataAll(db) + fmt.Println("Start Sign") for _, u := range data { access := MSGetToken(u.refreshToken, u.clientId, u.clientSecret) if access == "" { - fmt.Println(u.msId + "Sign ERROR:AccessTokenGet") + fmt.Println(u.msId + " Sign ERROR:AccessTokenGet") continue } if !OutLookGetMails(access) { - fmt.Println(u.msId + "Sign ERROR:ReadMails") + fmt.Println(u.msId + " Sign ERROR:ReadMails") continue } - fmt.Println(u.msId + " Sign OK!") u.uptime = time.Now().Unix() if ok, err := UpdateData(db, u); !ok { fmt.Printf("%s Update Data ERROR: %s\n", u.msId, err) + continue + } + fmt.Println(u.msId + " Sign OK!") + UserSignOk[u.tgId]++ + } + fmt.Println("Sign End,Start Send") + var isSend map[int64]bool + isSend = make(map[int64]bool) + for _, u := range data { + if !isSend[u.tgId] { + chat, err := bot.ChatByID(strconv.FormatInt(u.tgId, 10)) + if err != nil { + fmt.Println("Send Result ERROR") + continue + } + bot.Send(chat, "签到反馈\n时间: "+time.Unix(u.uptime, 0).Format("2006-01-02 15:04:05")+"\n结果: "+strconv.Itoa(UserSignOk[u.tgId])+"/"+strconv.Itoa(GetBindNum(u.tgId))) + isSend[u.tgId] = true } } fmt.Println("----Task End----") diff --git a/go.mod b/go.mod index 6191851..13ec1d3 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module main go 1.13 require ( + github.com/fsnotify/fsnotify v1.4.7 github.com/go-sql-driver/mysql v1.5.0 github.com/goreleaser/goreleaser v0.129.0 // indirect github.com/mitchellh/gox v1.0.1 // indirect diff --git a/handle.go b/handle.go index 28b888c..9cb163f 100644 --- a/handle.go +++ b/handle.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "github.com/fsnotify/fsnotify" "github.com/spf13/viper" tb "gopkg.in/tucnak/telebot.v2" "strconv" @@ -10,23 +11,26 @@ import ( ) const ( - bStartContent string = "欢迎使用E5SubBot!\n请输入\\help查看帮助" + bStartContent string = "欢迎使用E5SubBot!" bHelpContent string = ` 命令: - /notice 查看最新公告 /my 查看已绑定账户信息 /bind 绑定新账户 /unbind 解绑账户 /help 帮助 - 详细使用方法请看:https://github.com/iyear/E5SubBot + + 源码及使用方法:https://github.com/iyear/E5SubBot ` ) var ( UserStatus map[int64]int + UserSignOk map[int64]int UserCid map[int64]string UserCSecret map[int64]string BindMaxNum int + notice string + admins []int64 ) const ( @@ -43,6 +47,13 @@ func init() { CheckErr(err) BindMaxNum = viper.GetInt("bindmax") + notice = viper.GetString("notice") + + viper.WatchConfig() + viper.OnConfigChange(func(e fsnotify.Event) { + BindMaxNum = viper.GetInt("bindmax") + notice = viper.GetString("notice") + }) UserStatus = make(map[int64]int) UserCid = make(map[int64]string) @@ -50,7 +61,7 @@ func init() { } func bStart(m *tb.Message) { bot.Send(m.Sender, bStartContent) - bNotice(m) + bHelp(m) } func bMy(m *tb.Message) { data := QueryDataByTG(db, m.Chat.ID) @@ -90,7 +101,7 @@ func bBind2(m *tb.Message) { bot.Send(m.Chat, "错误的格式") return } - fmt.Println("client_id: " + tmp[0] + " client_secret" + tmp[1]) + fmt.Println("client_id: " + tmp[0] + " client_secret: " + tmp[1]) cid := tmp[0] cse := tmp[1] bot.Send(m.Chat, "授权账户: [点击直达]("+MSGetAuthUrl(cid)+")", tb.ModeMarkdown) @@ -128,7 +139,7 @@ func bUnBindInlineBtn(c *tb.Callback) { bot.Respond(c) } func bHelp(m *tb.Message) { - bot.Send(m.Sender, bHelpContent, &tb.SendOptions{DisableWebPagePreview: false}) + bot.Send(m.Sender, bHelpContent+"\n"+notice, &tb.SendOptions{DisableWebPagePreview: false}) } func bOnText(m *tb.Message) { switch UserStatus[m.Chat.ID] { @@ -166,7 +177,3 @@ func bOnText(m *tb.Message) { } } } -func bNotice(m *tb.Message) { - viper.ReadInConfig() - bot.Send(m.Chat, viper.GetString("notice")) -} diff --git a/main.go b/main.go index b1c2039..e970aa1 100644 --- a/main.go +++ b/main.go @@ -33,6 +33,8 @@ const ( ) func init() { + //read config + fmt.Println(logo) //read config fmt.Println(logo) fmt.Println("Read Config……") @@ -92,17 +94,15 @@ func MakeHandle() { bot.Handle("/my", bMy) bot.Handle("/bind", bBind1) bot.Handle("/unbind", bUnBind) - bot.Handle("/notice", bNotice) bot.Handle("/help", bHelp) bot.Handle(tb.OnText, bOnText) } func TaskLaunch() { - fmt.Println("Begin First SignTask……") task := cron.New() - SignTask() //每三小时执行一次 - task.AddFunc("1 */3 * * *", SignTask) - // */1 * * * * + task.AddFunc("*/1 * * * *", SignTask) + // */1 * * * * 1 */3 * * * fmt.Println("Cron Task Start……") task.Start() + defer task.Stop() } diff --git a/mysql.go b/mysql.go index b8c7c9e..fa41816 100644 --- a/mysql.go +++ b/mysql.go @@ -48,11 +48,10 @@ func UpdateData(db *sql.DB, u MSData) (bool, error) { if err != nil { return false, err } - res, err := stmt.Exec(u.tgId, u.refreshToken, u.uptime, u.alias, u.clientId, u.clientSecret, u.other, u.msId) + _, err = stmt.Exec(u.tgId, u.refreshToken, u.uptime, u.alias, u.clientId, u.clientSecret, u.other, u.msId) if err != nil { return false, err } - fmt.Println("Update Data Successd:", res) return true, nil } diff --git a/outlook.go b/outlook.go index 7cb78ce..1d8ac38 100644 --- a/outlook.go +++ b/outlook.go @@ -2,7 +2,6 @@ package main import ( "fmt" - "github.com/spf13/viper" "github.com/tidwall/gjson" "io/ioutil" "net/http" @@ -17,14 +16,6 @@ const ( scope string = "openid offline_access mail.read user.read" ) -func init() { - viper.SetConfigName("config") - viper.AddConfigPath(".") - err := viper.ReadInConfig() - if err != nil { - panic(fmt.Errorf("Fatal error config file: %s \n", err)) - } -} func MSGetAuthUrl(cid string) string { return "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=" + cid + "&response_type=code&redirect_uri=" + url.QueryEscape(redirectUri) + "&response_mode=query&scope=" + url.QueryEscape(scope) }