E5BotForSQLite/control.go

194 lines
5.5 KiB
Go
Raw Normal View History

2020-03-27 22:01:19 +08:00
package main
import (
"fmt"
"github.com/spf13/viper"
2020-03-27 22:01:19 +08:00
"github.com/tidwall/gjson"
tb "gopkg.in/tucnak/telebot.v2"
2020-04-03 13:19:53 +08:00
"io"
"log"
"os"
"strconv"
2020-03-28 10:22:07 +08:00
"strings"
2020-03-27 22:01:19 +08:00
"time"
)
2020-04-01 20:06:20 +08:00
var SignOk map[int64]int
2020-03-27 23:01:17 +08:00
//If Successfully return "",else return error information
2020-03-30 22:18:09 +08:00
func BindUser(m *tb.Message, cid, cse string) string {
2020-04-03 13:19:53 +08:00
logger.Printf("%d Begin Bind\n", m.Chat.ID)
2020-03-28 10:22:07 +08:00
tmp := strings.Split(m.Text, " ")
if len(tmp) != 2 {
2020-04-03 13:19:53 +08:00
logger.Printf("%d Bind error:Wrong Bind Format\n", m.Chat.ID)
2020-03-28 10:22:07 +08:00
return "授权格式错误"
}
2020-04-03 13:19:53 +08:00
logger.Println("alias: " + tmp[1])
2020-03-28 10:22:07 +08:00
alias := tmp[1]
code := GetURLValue(tmp[0], "code")
2020-03-31 09:14:27 +08:00
//fmt.Println(code)
2020-03-30 22:18:09 +08:00
access, refresh := MSFirGetToken(code, cid, cse)
2020-03-27 22:01:19 +08:00
if refresh == "" {
2020-04-03 13:19:53 +08:00
logger.Printf("%d Bind error:GetRefreshToken\n", m.Chat.ID)
2020-03-27 23:01:17 +08:00
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-31 09:14:27 +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-04-03 13:19:53 +08:00
logger.Printf("%d Bind error:Getinfo\n", m.Chat.ID)
2020-03-27 23:01:17 +08:00
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
//TG的Data传递最高64bytes,一些msid超过了报错BUTTON_DATA_INVALID (0)采取md5
u.msId = Get16MD5Encode(gjson.Get(info, "id").String())
2020-03-29 12:07:15 +08:00
u.uptime = time.Now().Unix()
2020-04-03 13:19:53 +08:00
logger.Println(u.uptime)
2020-03-30 22:18:09 +08:00
u.alias = alias
u.clientId = cid
u.clientSecret = cse
u.other = ""
2020-03-27 23:01:17 +08:00
//MS User Is Exist
2020-03-30 22:18:09 +08:00
if MSAppIsExist(u.tgId, u.clientId) {
2020-04-03 13:19:53 +08:00
logger.Printf("%d Bind error:MSUserHasExisted\n", m.Chat.ID)
2020-03-30 22:18:09 +08:00
return "该应用已经绑定过了,无需重复绑定"
2020-03-27 23:01:17 +08:00
}
//MS information has gotten
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-04-03 13:19:53 +08:00
logger.Printf("%d Bind error: %s\n", m.Chat.ID, err)
2020-03-27 23:01:17 +08:00
return "数据库写入错误"
}
2020-04-03 13:19:53 +08:00
logger.Printf("%d Bind Successfully!\n", m.Chat.ID)
2020-03-27 23:01:17 +08:00
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
2020-03-30 22:18:09 +08:00
func MSAppIsExist(tgId int64, clientId 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.clientId == clientId {
2020-03-27 23:01:17 +08:00
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() {
2020-04-01 20:06:20 +08:00
var SignOk map[int64]int
var SignErr []string
var num, signOk int
2020-04-01 20:06:20 +08:00
SignOk = make(map[int64]int)
2020-03-29 12:51:39 +08:00
fmt.Println("----Task Begin----")
fmt.Println("Time:" + time.Now().Format("2006-01-02 15:04:05"))
2020-03-28 15:21:59 +08:00
data := QueryDataAll(db)
num = len(data)
fmt.Println("Start Sign")
2020-03-28 15:21:59 +08:00
for _, u := range data {
e := ""
2020-04-03 08:30:53 +08:00
pre := "您的账户: " + u.alias + "\n在任务执行时出现了错误!\n错误:"
2020-03-30 22:18:09 +08:00
access := MSGetToken(u.refreshToken, u.clientId, u.clientSecret)
chat, _ := bot.ChatByID(strconv.FormatInt(u.tgId, 10))
2020-04-03 08:30:53 +08:00
//生成解绑按钮
var inlineKeys [][]tb.InlineButton
UnBindBtn := tb.InlineButton{Unique: "un" + u.msId, Text: "点击解绑该账户", Data: u.msId}
bot.Handle(&UnBindBtn, bUnBindInlineBtn)
inlineKeys = append(inlineKeys, []tb.InlineButton{UnBindBtn})
tmpBtn := &tb.ReplyMarkup{InlineKeyboard: inlineKeys}
se := u.msId + " ( @" + chat.Username + " )"
2020-03-28 15:21:59 +08:00
if access == "" {
e = "Sign ERROR:GetAccessToken"
2020-04-03 13:19:53 +08:00
logger.Println(u.msId + e)
2020-04-03 08:30:53 +08:00
bot.Send(chat, pre+e, tmpBtn)
SignErr = append(SignErr, se)
2020-03-28 15:21:59 +08:00
continue
}
if !OutLookGetMails(access) {
e = "Sign ERROR:ReadMails"
2020-04-03 13:19:53 +08:00
logger.Println(u.msId + " Sign ERROR:ReadMails")
2020-04-03 08:30:53 +08:00
bot.Send(chat, pre+e, tmpBtn)
SignErr = append(SignErr, se)
2020-03-28 15:21:59 +08:00
continue
}
2020-03-29 12:07:15 +08:00
u.uptime = time.Now().Unix()
2020-03-28 15:21:59 +08:00
if ok, err := UpdateData(db, u); !ok {
e = "Update Data ERROR:"
2020-04-03 13:19:53 +08:00
logger.Printf("%s Update Data ERROR: %s\n", u.msId, err)
2020-04-03 08:30:53 +08:00
bot.Send(chat, pre+e, tmpBtn)
SignErr = append(SignErr, se)
continue
}
fmt.Println(u.msId + " Sign OK!")
2020-04-01 20:06:20 +08:00
SignOk[u.tgId]++
signOk++
}
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 {
2020-04-03 13:19:53 +08:00
logger.Println("Send Result ERROR", err)
continue
}
2020-04-03 08:30:53 +08:00
//静默发送,过多消息很烦
2020-04-03 13:19:53 +08:00
_, err = bot.Send(chat, "任务反馈\n时间: "+time.Now().Format("2006-01-02 15:04:05")+"\n结果: "+strconv.Itoa(SignOk[u.tgId])+"/"+strconv.Itoa(GetBindNum(u.tgId)), &tb.SendOptions{DisableNotification: true})
if err != nil {
logger.Println(err)
}
isSend[u.tgId] = true
2020-03-28 15:21:59 +08:00
}
}
//管理员任务反馈
var ErrUser string
for _, eu := range SignErr {
ErrUser = ErrUser + eu + "\n"
}
for _, a := range admin {
chat, _ := bot.ChatByID(strconv.FormatInt(a, 10))
bot.Send(chat, "任务反馈(管理员)\n完成时间: "+time.Now().Format("2006-01-02 15:04:05")+"\n结果: "+strconv.Itoa(signOk)+"/"+strconv.Itoa(num)+"\n错误账户msid:\n"+ErrUser)
}
2020-03-29 12:51:39 +08:00
fmt.Println("----Task End----")
2020-03-28 15:21:59 +08:00
}
func GetAdmin() []int64 {
var result []int64
admins := strings.Split(viper.GetString("admin"), ",")
for _, v := range admins {
id, _ := strconv.ParseInt(v, 10, 64)
result = append(result, id)
}
return result
}
2020-04-03 13:19:53 +08:00
func InitLogger() {
if !PathExists("./log/") {
os.Mkdir("./log/", 0773)
}
path := "./log/" + time.Now().Format("2006-01-02") + ".log"
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0773)
if err != nil {
logger.Println(err)
}
writers := []io.Writer{
f,
os.Stdout}
faoWriter := io.MultiWriter(writers...)
logger = log.New(faoWriter, "【E5Sub】", log.Ldate|log.Ltime|log.Lshortfile)
}