Skip to content

Commit

Permalink
bindmax,notice配置项改为热更新
Browse files Browse the repository at this point in the history
增加用户反馈
修复数据库配置读取错误
去除/notice命令合并至/help
  • Loading branch information
iyear committed Apr 1, 2020
1 parent b3a0125 commit a739ee4
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 30 deletions.
26 changes: 22 additions & 4 deletions control.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/tidwall/gjson"
tb "gopkg.in/tucnak/telebot.v2"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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----")
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 17 additions & 10 deletions handle.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
tb "gopkg.in/tucnak/telebot.v2"
"strconv"
Expand All @@ -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 (
Expand All @@ -43,14 +47,21 @@ 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)
UserCSecret = make(map[int64]string)
}
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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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] {
Expand Down Expand Up @@ -166,7 +177,3 @@ func bOnText(m *tb.Message) {
}
}
}
func bNotice(m *tb.Message) {
viper.ReadInConfig()
bot.Send(m.Chat, viper.GetString("notice"))
}
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const (
)

func init() {
//read config
fmt.Println(logo)
//read config
fmt.Println(logo)
fmt.Println("Read Config……")
Expand Down Expand Up @@ -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()
}
3 changes: 1 addition & 2 deletions mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
9 changes: 0 additions & 9 deletions outlook.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"github.com/spf13/viper"
"github.com/tidwall/gjson"
"io/ioutil"
"net/http"
Expand All @@ -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)
}
Expand Down

0 comments on commit a739ee4

Please sign in to comment.