增加详细的错误反馈
This commit is contained in:
parent
fa5712dc77
commit
d90cd58051
49
control.go
49
control.go
@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"github.com/tidwall/gjson"
|
"github.com/tidwall/gjson"
|
||||||
@ -16,30 +17,30 @@ import (
|
|||||||
var SignOk map[int64]int
|
var SignOk map[int64]int
|
||||||
|
|
||||||
//If Successfully return "",else return error information
|
//If Successfully return "",else return error information
|
||||||
func BindUser(m *tb.Message, cid, cse string) string {
|
func BindUser(m *tb.Message, cid, cse string) error {
|
||||||
logger.Printf("%d Begin Bind\n", m.Chat.ID)
|
logger.Printf("%d Begin Bind\n", m.Chat.ID)
|
||||||
tmp := strings.Split(m.Text, " ")
|
tmp := strings.Split(m.Text, " ")
|
||||||
if len(tmp) != 2 {
|
if len(tmp) != 2 {
|
||||||
logger.Printf("%d Bind error:Wrong Bind Format\n", m.Chat.ID)
|
logger.Printf("%d Bind error:Wrong Bind Format\n", m.Chat.ID)
|
||||||
return "授权格式错误"
|
return errors.New("绑定格式错误")
|
||||||
}
|
}
|
||||||
logger.Println("alias: " + tmp[1])
|
logger.Println("alias: " + tmp[1])
|
||||||
alias := tmp[1]
|
alias := tmp[1]
|
||||||
code := GetURLValue(tmp[0], "code")
|
code := GetURLValue(tmp[0], "code")
|
||||||
//fmt.Println(code)
|
//fmt.Println(code)
|
||||||
access, refresh := MSFirGetToken(code, cid, cse)
|
access, refresh, err := MSFirGetToken(code, cid, cse)
|
||||||
if refresh == "" {
|
if err != nil {
|
||||||
logger.Printf("%d Bind error:GetRefreshToken\n", m.Chat.ID)
|
logger.Printf("%d Bind error:GetRefreshToken %s \n", m.Chat.ID, err.Error())
|
||||||
return "获取RefreshToken失败"
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
//token has gotten
|
//token has gotten
|
||||||
bot.Send(m.Chat, "Token获取成功!")
|
bot.Send(m.Chat, "Token获取成功!")
|
||||||
info := MSGetUserInfo(access)
|
info, err := MSGetUserInfo(access)
|
||||||
//fmt.Printf("TGID:%d Refresh Token: %s\n", m.Chat.ID, refresh)
|
//fmt.Printf("TGID:%d Refresh Token: %s\n", m.Chat.ID, refresh)
|
||||||
if info == "" {
|
if err != nil {
|
||||||
logger.Printf("%d Bind error:Getinfo\n", m.Chat.ID)
|
logger.Printf("%d Bind error:Getinfo %s \n", m.Chat.ID, err.Error())
|
||||||
return "获取用户信息错误"
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var u MSData
|
var u MSData
|
||||||
@ -56,16 +57,16 @@ func BindUser(m *tb.Message, cid, cse string) string {
|
|||||||
//MS User Is Exist
|
//MS User Is Exist
|
||||||
if MSAppIsExist(u.tgId, u.clientId) {
|
if MSAppIsExist(u.tgId, u.clientId) {
|
||||||
logger.Printf("%d Bind error:MSUserHasExisted\n", m.Chat.ID)
|
logger.Printf("%d Bind error:MSUserHasExisted\n", m.Chat.ID)
|
||||||
return "该应用已经绑定过了,无需重复绑定"
|
return errors.New("该应用已经绑定过了,无需重复绑定")
|
||||||
}
|
}
|
||||||
//MS information has gotten
|
//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")
|
bot.Send(m.Chat, "MS_ID(MD5): "+u.msId+"\nuserPrincipalName: "+gjson.Get(info, "userPrincipalName").String()+"\ndisplayName: "+gjson.Get(info, "displayName").String()+"\n")
|
||||||
if ok, err := AddData(db, u); !ok {
|
if ok, err := AddData(db, u); !ok {
|
||||||
logger.Printf("%d Bind error: %s\n", m.Chat.ID, err)
|
logger.Printf("%d Bind error: %s\n", m.Chat.ID, err)
|
||||||
return "数据库写入错误"
|
return err
|
||||||
}
|
}
|
||||||
logger.Printf("%d Bind Successfully!\n", m.Chat.ID)
|
logger.Printf("%d Bind Successfully!\n", m.Chat.ID)
|
||||||
return ""
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//get bind num
|
//get bind num
|
||||||
@ -98,9 +99,8 @@ func SignTask() {
|
|||||||
num = len(data)
|
num = len(data)
|
||||||
fmt.Println("Start Sign")
|
fmt.Println("Start Sign")
|
||||||
for _, u := range data {
|
for _, u := range data {
|
||||||
e := ""
|
|
||||||
pre := "您的账户: " + u.alias + "\n在任务执行时出现了错误!\n错误:"
|
pre := "您的账户: " + u.alias + "\n在任务执行时出现了错误!\n错误:"
|
||||||
access := MSGetToken(u.refreshToken, u.clientId, u.clientSecret)
|
access, err := MSGetToken(u.refreshToken, u.clientId, u.clientSecret)
|
||||||
chat, _ := bot.ChatByID(strconv.FormatInt(u.tgId, 10))
|
chat, _ := bot.ChatByID(strconv.FormatInt(u.tgId, 10))
|
||||||
//生成解绑按钮
|
//生成解绑按钮
|
||||||
var inlineKeys [][]tb.InlineButton
|
var inlineKeys [][]tb.InlineButton
|
||||||
@ -110,25 +110,22 @@ func SignTask() {
|
|||||||
tmpBtn := &tb.ReplyMarkup{InlineKeyboard: inlineKeys}
|
tmpBtn := &tb.ReplyMarkup{InlineKeyboard: inlineKeys}
|
||||||
|
|
||||||
se := u.msId + " ( @" + chat.Username + " )"
|
se := u.msId + " ( @" + chat.Username + " )"
|
||||||
if access == "" {
|
if err != nil {
|
||||||
e = "Sign ERROR:GetAccessToken"
|
logger.Println(u.msId+" ", err)
|
||||||
logger.Println(u.msId + e)
|
bot.Send(chat, pre+err.Error(), tmpBtn)
|
||||||
bot.Send(chat, pre+e, tmpBtn)
|
|
||||||
SignErr = append(SignErr, se)
|
SignErr = append(SignErr, se)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if !OutLookGetMails(access) {
|
if ok, err := OutLookGetMails(access); !ok {
|
||||||
e = "Sign ERROR:ReadMails"
|
logger.Println(u.msId+" ", err)
|
||||||
logger.Println(u.msId + " Sign ERROR:ReadMails")
|
bot.Send(chat, pre+err.Error(), tmpBtn)
|
||||||
bot.Send(chat, pre+e, tmpBtn)
|
|
||||||
SignErr = append(SignErr, se)
|
SignErr = append(SignErr, se)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
u.uptime = time.Now().Unix()
|
u.uptime = time.Now().Unix()
|
||||||
if ok, err := UpdateData(db, u); !ok {
|
if ok, err := UpdateData(db, u); !ok {
|
||||||
e = "Update Data ERROR:"
|
logger.Println(u.msId+" ", err)
|
||||||
logger.Printf("%s Update Data ERROR: %s\n", u.msId, err)
|
bot.Send(chat, pre+err.Error(), tmpBtn)
|
||||||
bot.Send(chat, pre+e, tmpBtn)
|
|
||||||
SignErr = append(SignErr, se)
|
SignErr = append(SignErr, se)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@ -212,11 +212,11 @@ func bOnText(m *tb.Message) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
bot.Send(m.Chat, "正在绑定中……")
|
bot.Send(m.Chat, "正在绑定中……")
|
||||||
info := BindUser(m, UserCid[m.Chat.ID], UserCSecret[m.Chat.ID])
|
err := BindUser(m, UserCid[m.Chat.ID], UserCSecret[m.Chat.ID])
|
||||||
if info == "" {
|
if err != nil {
|
||||||
bot.Send(m.Chat, "绑定成功!")
|
bot.Send(m.Chat, err.Error())
|
||||||
} else {
|
} else {
|
||||||
bot.Send(m.Chat, info)
|
bot.Send(m.Chat, "绑定成功!")
|
||||||
}
|
}
|
||||||
UserStatus[m.Chat.ID] = USNone
|
UserStatus[m.Chat.ID] = USNone
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user