2020-03-27 22:01:19 +08:00
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"fmt"
|
|
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
|
|
tb "gopkg.in/tucnak/telebot.v2"
|
2020-03-28 10:22:07 +08:00
|
|
|
|
"strings"
|
2020-03-27 22:01:19 +08:00
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2020-03-27 23:01:17 +08:00
|
|
|
|
//If Successfully return "",else return error information
|
|
|
|
|
|
func BindUser(m *tb.Message) string {
|
|
|
|
|
|
fmt.Printf("%d Begin Bind\n", m.Chat.ID)
|
2020-03-28 10:22:07 +08:00
|
|
|
|
tmp := strings.Split(m.Text, " ")
|
|
|
|
|
|
fmt.Println("alias: " + tmp[1])
|
|
|
|
|
|
if len(tmp) != 2 {
|
|
|
|
|
|
fmt.Printf("%d Bind error:Wrong Bind Format\n", m.Chat.ID)
|
|
|
|
|
|
return "授权格式错误"
|
|
|
|
|
|
}
|
|
|
|
|
|
alias := tmp[1]
|
|
|
|
|
|
code := GetURLValue(tmp[0], "code")
|
2020-03-27 22:01:19 +08:00
|
|
|
|
fmt.Println(code)
|
|
|
|
|
|
access, refresh := MSFirGetToken(code)
|
|
|
|
|
|
if refresh == "" {
|
2020-03-27 23:01:17 +08:00
|
|
|
|
fmt.Printf("%d Bind error:GetRefreshToken\n", m.Chat.ID)
|
|
|
|
|
|
return "获取RefreshToken失败"
|
2020-03-27 22:01:19 +08:00
|
|
|
|
}
|
2020-03-27 23:01:17 +08:00
|
|
|
|
|
|
|
|
|
|
//token has gotten
|
2020-03-27 22:01:19 +08:00
|
|
|
|
bot.Send(m.Chat, "Token获取成功!")
|
|
|
|
|
|
info := MSGetUserInfo(access)
|
2020-03-27 23:01:17 +08:00
|
|
|
|
fmt.Printf("TGID:%d Refresh Token: %s\n", m.Chat.ID, refresh)
|
2020-03-27 22:01:19 +08:00
|
|
|
|
if info == "" {
|
2020-03-27 23:01:17 +08:00
|
|
|
|
fmt.Printf("%d Bind error:Getinfo\n", m.Chat.ID)
|
|
|
|
|
|
return "获取用户信息错误"
|
2020-03-27 22:01:19 +08:00
|
|
|
|
}
|
2020-03-27 23:01:17 +08:00
|
|
|
|
|
2020-03-27 22:01:19 +08:00
|
|
|
|
var u MSData
|
|
|
|
|
|
u.tgId = m.Chat.ID
|
|
|
|
|
|
u.refreshToken = refresh
|
2020-03-28 17:16:23 +08:00
|
|
|
|
//TG的Data传递最高64bytes,一些msid超过了报错BUTTON_DATA_INVALID (0),采取md5
|
|
|
|
|
|
u.msId = Get16MD5Encode(gjson.Get(info, "id").String())
|
2020-03-27 22:01:19 +08:00
|
|
|
|
u.uptime = time.Now()
|
2020-03-28 10:22:07 +08:00
|
|
|
|
u.other = SetJsonValue("{}", "alias", alias)
|
2020-03-28 17:16:23 +08:00
|
|
|
|
//u.other = SetJsonValue(u.other, "sign", Get16MD5Encode(u.msId))
|
2020-03-27 23:01:17 +08:00
|
|
|
|
//MS User Is Exist
|
|
|
|
|
|
if MSUserIsExist(u.tgId, u.msId) {
|
|
|
|
|
|
fmt.Printf("%d Bind error:MSUserHasExisted\n", m.Chat.ID)
|
|
|
|
|
|
return "该ID对应的用户已经绑定过了"
|
|
|
|
|
|
}
|
|
|
|
|
|
//MS information has gotten
|
2020-03-28 17:16:23 +08:00
|
|
|
|
bot.Send(m.Chat, "MS_ID(MD5): "+u.msId+"\nuserPrincipalName: "+gjson.Get(info, "userPrincipalName").String()+"\ndisplayName: "+gjson.Get(info, "displayName").String()+"\n")
|
2020-03-27 22:01:19 +08:00
|
|
|
|
if ok, err := AddData(db, u); !ok {
|
2020-03-27 23:01:17 +08:00
|
|
|
|
fmt.Printf("%d Bind error: %s\n", m.Chat.ID, err)
|
|
|
|
|
|
return "数据库写入错误"
|
|
|
|
|
|
}
|
|
|
|
|
|
fmt.Printf("%d Bind Successfully!\n", m.Chat.ID)
|
|
|
|
|
|
return ""
|
|
|
|
|
|
}
|
2020-03-28 15:21:59 +08:00
|
|
|
|
|
|
|
|
|
|
//get bind num
|
2020-03-27 23:01:17 +08:00
|
|
|
|
func GetBindNum(tgId int64) int {
|
2020-03-28 10:22:07 +08:00
|
|
|
|
data := QueryDataByTG(db, tgId)
|
2020-03-27 23:01:17 +08:00
|
|
|
|
return len(data)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//return true => exist
|
|
|
|
|
|
func MSUserIsExist(tgId int64, msId string) bool {
|
2020-03-28 10:22:07 +08:00
|
|
|
|
data := QueryDataByTG(db, tgId)
|
2020-03-27 23:01:17 +08:00
|
|
|
|
var res MSData
|
|
|
|
|
|
for _, res = range data {
|
|
|
|
|
|
if res.msId == msId {
|
|
|
|
|
|
return true
|
|
|
|
|
|
}
|
2020-03-27 22:01:19 +08:00
|
|
|
|
}
|
2020-03-27 23:01:17 +08:00
|
|
|
|
return false
|
2020-03-27 22:01:19 +08:00
|
|
|
|
}
|
2020-03-28 15:21:59 +08:00
|
|
|
|
|
|
|
|
|
|
//SignTask
|
|
|
|
|
|
func SignTask() {
|
|
|
|
|
|
data := QueryDataAll(db)
|
|
|
|
|
|
for _, u := range data {
|
|
|
|
|
|
access := MSGetToken(u.refreshToken)
|
|
|
|
|
|
if access == "" {
|
|
|
|
|
|
fmt.Println(u.msId + "Sign ERROR:AccessTokenGet")
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
if !OutLookGetMails(access) {
|
|
|
|
|
|
fmt.Println(u.msId + "Sign ERROR:ReadMails")
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
fmt.Println(u.msId + " Sign OK!")
|
|
|
|
|
|
u.uptime = time.Now()
|
|
|
|
|
|
if ok, err := UpdateData(db, u); !ok {
|
|
|
|
|
|
fmt.Printf("%s Update Data ERROR: %s\n", u.msId, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|