This commit is contained in:
iyear 2020-03-29 12:51:39 +08:00
parent d0bccbde81
commit b3998a90af
8 changed files with 60 additions and 23 deletions

View File

@ -79,6 +79,8 @@ func MSUserIsExist(tgId int64, msId string) bool {
//SignTask
func SignTask() {
fmt.Println("----Task Begin----")
fmt.Println("Time:" + time.Now().Format("2006-01-02 15:04:05"))
data := QueryDataAll(db)
for _, u := range data {
access := MSGetToken(u.refreshToken)
@ -96,4 +98,5 @@ func SignTask() {
fmt.Printf("%s Update Data ERROR: %s\n", u.msId, err)
}
}
fmt.Println("----Task End----")
}

1
go.mod
View File

@ -4,6 +4,7 @@ go 1.13
require (
github.com/go-sql-driver/mysql v1.5.0
github.com/mitchellh/gox v1.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/robfig/cron/v3 v3.0.0
github.com/spf13/viper v1.6.2

6
go.sum
View File

@ -39,6 +39,8 @@ github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoA
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/hashicorp/go-version v1.0.0 h1:21MVWPKDphxa7ineQQTrCU5brh7OuVVAzGOCnnCPtE8=
github.com/hashicorp/go-version v1.0.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
@ -56,6 +58,10 @@ github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP
github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U=
github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mitchellh/gox v1.0.1 h1:x0jD3dcHk9a9xPSDN6YEL4xL6Qz0dvNYm8yZqui5chI=
github.com/mitchellh/gox v1.0.1/go.mod h1:ED6BioOGXMswlXa2zxfh/xdd5QhwYliBFn9V18Ap4z4=
github.com/mitchellh/iochan v1.0.0 h1:C+X3KsSTLFVBr/tK1eYN/vs4rJcvsiLU338UhYPJWeY=
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
github.com/mitchellh/mapstructure v1.1.2 h1:fmNYVwqnSfB9mZU6OS2O6GsXM+wcskZDuKQzvN1EDeE=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=

View File

@ -9,7 +9,15 @@ import (
)
const (
bStartContent string = "欢迎使用E5SubBot!\n请输入命令以启用Bot"
bStartContent string = "欢迎使用E5SubBot!\n请输入\\help查看帮助"
bHelpContent string = `
命令
/my 查看已绑定账户信息
/bind 绑定新账户
/unbind 解绑账户
/help 帮助
详细使用方法请看https://github.com/iyear/E5SubBot
`
)
var (
@ -40,10 +48,8 @@ func bStart(m *tb.Message) {
}
func bMy(m *tb.Message) {
data := QueryDataByTG(db, m.Chat.ID)
fmt.Println(data)
var inlineKeys [][]tb.InlineButton
for _, u := range data {
fmt.Println(u)
inlineBtn := tb.InlineButton{
Unique: "my" + u.msId,
Text: u.other,
@ -52,11 +58,9 @@ func bMy(m *tb.Message) {
bot.Handle(&inlineBtn, bMyInlineBtn)
inlineKeys = append(inlineKeys, []tb.InlineButton{inlineBtn})
}
_, err := bot.Send(m.Chat, "选择一个账户查看具体信息\n\n绑定数: "+strconv.Itoa(GetBindNum(m.Chat.ID))+"/"+strconv.Itoa(BindMaxNum), &tb.ReplyMarkup{InlineKeyboard: inlineKeys})
fmt.Println(err)
bot.Send(m.Chat, "选择一个账户查看具体信息\n\n绑定数: "+strconv.Itoa(GetBindNum(m.Chat.ID))+"/"+strconv.Itoa(BindMaxNum), &tb.ReplyMarkup{InlineKeyboard: inlineKeys})
}
func bMyInlineBtn(c *tb.Callback) {
fmt.Println(c.Data)
r := QueryDataByMS(db, c.Data)
u := r[0]
bot.Send(c.Message.Chat, "信息\n别名"+u.other+"\nMS_ID(MD5): "+u.msId+"\n最近更新时间: "+time.Unix(u.uptime, 0).Format("2006-01-02 15:04:05"))
@ -87,7 +91,6 @@ func bUnBind(m *tb.Message) {
bot.Send(m.Chat, "选择一个账户将其解绑\n\n当前绑定数: "+strconv.Itoa(GetBindNum(m.Chat.ID))+"/"+strconv.Itoa(BindMaxNum), &tb.ReplyMarkup{InlineKeyboard: inlineKeys})
}
func bUnBindInlineBtn(c *tb.Callback) {
fmt.Println(c.Data)
r := QueryDataByMS(db, c.Data)
u := r[0]
if ok, _ := DelData(db, u.msId); !ok {
@ -99,8 +102,8 @@ func bUnBindInlineBtn(c *tb.Callback) {
bot.Send(c.Message.Chat, "解绑成功!")
bot.Respond(c)
}
func bAbout(m *tb.Message) {
bot.Send(m.Sender, bStartContent)
func bHelp(m *tb.Message) {
bot.Send(m.Sender, bHelpContent, &tb.SendOptions{DisableWebPagePreview: false})
}
func bOnText(m *tb.Message) {
switch UserStatus[m.Chat.ID] {

27
main.go
View File

@ -8,6 +8,7 @@ import (
"golang.org/x/net/proxy"
tb "gopkg.in/tucnak/telebot.v2"
"net/http"
"strconv"
"time"
)
@ -20,11 +21,23 @@ var (
const (
dbDriverName = "mysql"
logo = `
______ _____ _ ____ _
| ____|| ____| | | | _ \ | |
| |__ | |__ _ _ | |__ | |_) | ___ | |_
| __| |___ \ | | | || '_ \ | _ < / _ \ | __|
| |____ ___) || |_| || |_) || |_) || (_) || |_
|______||____/ \__,_||_.__/ |____/ \___/ \__|
`
//dbName = "./data.db"
)
func init() {
//read config
fmt.Println(logo)
fmt.Println("Read Config……")
viper.SetConfigName("config")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
@ -32,6 +45,7 @@ func init() {
BotToken = viper.GetString("bot_token")
Socks5 = viper.GetString("socks5")
//set bot
fmt.Println("Bot Settings……")
botsettings := tb.Settings{
Token: BotToken,
Poller: &tb.LongPoller{Timeout: 10 * time.Second},
@ -48,8 +62,11 @@ func init() {
}
//create bot
bot, err = tb.NewBot(botsettings)
CheckErr(err)
if err != nil {
fmt.Println("Create Bot ERROR!")
return
}
fmt.Println("Bot: " + strconv.Itoa(bot.Me.ID) + " " + bot.Me.Username)
}
func main() {
BotStart()
@ -57,22 +74,26 @@ func main() {
func BotStart() {
MakeHandle()
TaskLaunch()
fmt.Println("Bot Start……")
bot.Start()
}
func MakeHandle() {
fmt.Println("Make Handle……")
bot.Handle("/start", bStart)
bot.Handle("/my", bMy)
bot.Handle("/bind", bBind)
bot.Handle("/unbind", bUnBind)
bot.Handle("/about", bAbout)
bot.Handle("/help", bHelp)
bot.Handle(tb.OnText, bOnText)
//bot.Handle(tb.InlineButton{Unique: ""})
}
func TaskLaunch() {
fmt.Println("Begin First SignTask……")
task := cron.New()
SignTask()
//每三小时执行一次
task.AddFunc("1 */3 * * *", SignTask)
// */1 * * * *
fmt.Println("Cron Task Start……")
task.Start()
}

View File

@ -30,8 +30,11 @@ func init() {
path := strings.Join([]string{user, ":", pwd, "@tcp(", host, ":", port, ")/", database, "?charset=utf8"}, "")
//fmt.Println(path)
db, err = sql.Open(dbDriverName, path)
fmt.Println(db.Ping())
CheckErr(err)
if !CheckErr(err) {
fmt.Println("Connect MySQL ERROR:")
return
}
fmt.Println("Connect MySQL Success!")
CreateTB(db)
}

View File

@ -55,7 +55,7 @@ func MSFirGetToken(code string) (access string, refresh string) {
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Fatal error ", err.Error())
fmt.Println("Fatal error ")
}
if gjson.Get(string(content), "token_type").String() == "Bearer" {
return gjson.Get(string(content), "access_token").String(), gjson.Get(string(content), "refresh_token").String()
@ -76,15 +76,15 @@ func MSGetToken(refreshtoken string) (access string) {
r.Form.Add("refresh_token", refreshtoken)
r.Form.Add("redirect_uri", redirectUri)
body := strings.NewReader(r.Form.Encode())
fmt.Println(body)
//fmt.Println(body)
req, err := http.NewRequest("POST", MsApiUrl+"/common/oauth2/v2.0/token", body)
resp, err := client.Do(req)
defer resp.Body.Close()
content, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("Fatal error ", err.Error())
fmt.Println("Fatal error ")
}
fmt.Println(string(content))
//fmt.Println(string(content))
//fmt.Println(gjson.Get(string(content), "access_token").String())
if gjson.Get(string(content), "token_type").String() == "Bearer" {
return gjson.Get(string(content), "access_token").String()
@ -100,7 +100,7 @@ func MSGetUserInfo(accesstoken string) (json string) {
//r.Header.Set("Host","graph.microsoft.com")
req, err := http.NewRequest("GET", MsGraUrl+"/v1.0/me", nil)
if err != nil {
fmt.Println("MSGetUserInfo ERROR ", err.Error())
fmt.Println("MSGetUserInfo ERROR ")
return ""
}
req.Header.Set("Authorization", accesstoken)
@ -119,15 +119,14 @@ func OutLookGetMails(accesstoken string) bool {
//r.Header.Set("Host","graph.microsoft.com")
req, err := http.NewRequest("GET", MsGraUrl+"/v1.0/me/messages", nil)
if err != nil {
fmt.Println("MSGetMils ERROR ", err.Error())
fmt.Println("MSGetMils ERROR ")
return false
}
req.Header.Set("Authorization", accesstoken)
resp, _ := client.Do(req)
defer resp.Body.Close()
content, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(content))
//fmt.Println(string(content))
//这里的.需要转义,否则会按路径的方式解析
if gjson.Get(string(content), "@odata\\.context").String() != "" {
return true

View File

@ -10,10 +10,11 @@ import (
"strings"
)
//true=>no error
func CheckErr(err error) bool {
if err != nil {
log.Println(err)
fmt.Println("error: ", err.Error())
fmt.Println("ERROR")
panic(err)
return false
}