2021-03-11 12:57:30 +08:00
|
|
|
|
package bots
|
2020-03-27 11:36:29 +08:00
|
|
|
|
|
2020-03-27 23:02:28 +08:00
|
|
|
|
import (
|
2020-04-09 15:47:48 +08:00
|
|
|
|
"encoding/json"
|
2020-04-01 15:49:17 +08:00
|
|
|
|
"github.com/fsnotify/fsnotify"
|
2021-06-14 13:07:08 +08:00
|
|
|
|
"github.com/iyear/E5SubBot/core"
|
|
|
|
|
|
"github.com/iyear/E5SubBot/util"
|
2020-03-27 23:02:28 +08:00
|
|
|
|
"github.com/spf13/viper"
|
2021-06-14 13:07:08 +08:00
|
|
|
|
"go.uber.org/zap"
|
2020-03-27 23:02:28 +08:00
|
|
|
|
tb "gopkg.in/tucnak/telebot.v2"
|
2020-04-09 15:47:48 +08:00
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
"os"
|
2020-04-07 10:56:45 +08:00
|
|
|
|
"path/filepath"
|
2020-03-27 23:02:28 +08:00
|
|
|
|
"strconv"
|
2020-03-30 22:18:09 +08:00
|
|
|
|
"strings"
|
2020-03-29 12:07:15 +08:00
|
|
|
|
"time"
|
2020-03-27 23:02:28 +08:00
|
|
|
|
)
|
2020-03-27 11:36:29 +08:00
|
|
|
|
|
2020-03-27 23:02:28 +08:00
|
|
|
|
const (
|
2020-04-07 10:56:45 +08:00
|
|
|
|
bLogBasePath string = "./log/"
|
2020-04-01 15:49:17 +08:00
|
|
|
|
bStartContent string = "欢迎使用E5SubBot!"
|
2020-03-29 12:51:39 +08:00
|
|
|
|
bHelpContent string = `
|
|
|
|
|
|
命令:
|
|
|
|
|
|
/my 查看已绑定账户信息
|
|
|
|
|
|
/bind 绑定新账户
|
|
|
|
|
|
/unbind 解绑账户
|
2021-03-13 13:15:31 +08:00
|
|
|
|
/export 导出账户信息(JSON)
|
2020-03-29 12:51:39 +08:00
|
|
|
|
/help 帮助
|
2020-04-01 15:49:17 +08:00
|
|
|
|
源码及使用方法:https://github.com/iyear/E5SubBot
|
2020-03-29 12:51:39 +08:00
|
|
|
|
`
|
2020-03-27 23:02:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
var (
|
2020-03-30 22:18:09 +08:00
|
|
|
|
UserStatus map[int64]int
|
|
|
|
|
|
UserCid map[int64]string
|
|
|
|
|
|
UserCSecret map[int64]string
|
2020-04-09 16:47:10 +08:00
|
|
|
|
ErrorTimes map[string]int //错误次数
|
2020-03-30 22:18:09 +08:00
|
|
|
|
BindMaxNum int
|
2020-04-09 16:47:10 +08:00
|
|
|
|
ErrMaxTimes int
|
2020-04-01 15:49:17 +08:00
|
|
|
|
notice string
|
2020-04-02 09:43:42 +08:00
|
|
|
|
admin []int64
|
2020-03-27 23:02:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
|
USNone = iota
|
2020-03-30 22:18:09 +08:00
|
|
|
|
USBind1
|
|
|
|
|
|
USBind2
|
2020-03-27 23:02:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
|
//read config
|
|
|
|
|
|
viper.SetConfigName("config")
|
|
|
|
|
|
viper.AddConfigPath(".")
|
|
|
|
|
|
err := viper.ReadInConfig()
|
2021-03-11 12:57:30 +08:00
|
|
|
|
util.CheckErr(err)
|
2020-03-27 23:02:28 +08:00
|
|
|
|
|
2020-04-12 09:08:06 +08:00
|
|
|
|
viper.SetDefault("errlimit", 5)
|
|
|
|
|
|
viper.SetDefault("bindmax", 5)
|
|
|
|
|
|
|
2020-03-27 23:02:28 +08:00
|
|
|
|
BindMaxNum = viper.GetInt("bindmax")
|
2020-04-09 16:47:10 +08:00
|
|
|
|
ErrMaxTimes = viper.GetInt("errlimit")
|
2020-04-01 15:49:17 +08:00
|
|
|
|
notice = viper.GetString("notice")
|
2020-04-02 09:43:42 +08:00
|
|
|
|
admin = GetAdmin()
|
2020-04-01 15:49:17 +08:00
|
|
|
|
|
|
|
|
|
|
viper.WatchConfig()
|
|
|
|
|
|
viper.OnConfigChange(func(e fsnotify.Event) {
|
|
|
|
|
|
BindMaxNum = viper.GetInt("bindmax")
|
2020-04-09 16:47:10 +08:00
|
|
|
|
ErrMaxTimes = viper.GetInt("errlimit")
|
2020-04-01 15:49:17 +08:00
|
|
|
|
notice = viper.GetString("notice")
|
2020-04-02 09:43:42 +08:00
|
|
|
|
admin = GetAdmin()
|
2020-04-01 15:49:17 +08:00
|
|
|
|
})
|
2020-03-27 23:02:28 +08:00
|
|
|
|
|
|
|
|
|
|
UserStatus = make(map[int64]int)
|
2020-03-30 22:18:09 +08:00
|
|
|
|
UserCid = make(map[int64]string)
|
|
|
|
|
|
UserCSecret = make(map[int64]string)
|
2020-04-09 16:47:10 +08:00
|
|
|
|
ErrorTimes = make(map[string]int)
|
2020-03-27 23:02:28 +08:00
|
|
|
|
}
|
2020-04-04 13:05:41 +08:00
|
|
|
|
|
2020-03-27 11:36:29 +08:00
|
|
|
|
func bStart(m *tb.Message) {
|
2020-03-27 23:02:28 +08:00
|
|
|
|
bot.Send(m.Sender, bStartContent)
|
2020-04-01 15:49:17 +08:00
|
|
|
|
bHelp(m)
|
2020-03-27 23:02:28 +08:00
|
|
|
|
}
|
2020-04-07 10:56:45 +08:00
|
|
|
|
|
2020-03-27 23:02:28 +08:00
|
|
|
|
func bMy(m *tb.Message) {
|
2021-03-12 12:54:32 +08:00
|
|
|
|
data := core.QueryDataByTG(m.Chat.ID)
|
2020-03-28 11:20:45 +08:00
|
|
|
|
var inlineKeys [][]tb.InlineButton
|
2020-03-27 23:02:28 +08:00
|
|
|
|
for _, u := range data {
|
|
|
|
|
|
inlineBtn := tb.InlineButton{
|
2021-03-11 12:57:30 +08:00
|
|
|
|
Unique: "my" + u.MsId,
|
|
|
|
|
|
Text: u.Alias,
|
|
|
|
|
|
Data: u.MsId,
|
2020-03-27 23:02:28 +08:00
|
|
|
|
}
|
2020-03-28 11:20:45 +08:00
|
|
|
|
bot.Handle(&inlineBtn, bMyInlineBtn)
|
2020-03-27 23:02:28 +08:00
|
|
|
|
inlineKeys = append(inlineKeys, []tb.InlineButton{inlineBtn})
|
|
|
|
|
|
}
|
2020-03-29 12:51:39 +08:00
|
|
|
|
bot.Send(m.Chat, "选择一个账户查看具体信息\n\n绑定数: "+strconv.Itoa(GetBindNum(m.Chat.ID))+"/"+strconv.Itoa(BindMaxNum), &tb.ReplyMarkup{InlineKeyboard: inlineKeys})
|
2020-03-27 23:02:28 +08:00
|
|
|
|
}
|
2020-03-28 11:20:45 +08:00
|
|
|
|
func bMyInlineBtn(c *tb.Callback) {
|
2021-03-12 12:54:32 +08:00
|
|
|
|
r := core.QueryDataByMS(c.Data)
|
2020-03-28 11:20:45 +08:00
|
|
|
|
u := r[0]
|
2021-03-11 12:57:30 +08:00
|
|
|
|
bot.Send(c.Message.Chat, "信息\n别名:"+u.Alias+"\nMS_ID(MD5): "+u.MsId+"\nclient_id: "+u.ClientId+"\nclient_secret: "+u.ClientSecret+"\n最近更新时间: "+time.Unix(u.Uptime, 0).Format("2006-01-02 15:04:05"))
|
2020-03-27 23:02:28 +08:00
|
|
|
|
bot.Respond(c)
|
|
|
|
|
|
}
|
2020-04-07 10:56:45 +08:00
|
|
|
|
|
2020-03-30 22:18:09 +08:00
|
|
|
|
func bBind1(m *tb.Message) {
|
2021-03-13 13:15:31 +08:00
|
|
|
|
bot.Send(m.Chat, "应用注册: [点击直达]("+core.GetMSRegisterAppUrl()+")", tb.ModeMarkdown)
|
2021-06-14 13:07:08 +08:00
|
|
|
|
bot.Send(m.Chat, "请回复client_id+空格+client_secret", &tb.ReplyMarkup{ForceReply: true})
|
2020-04-09 15:47:48 +08:00
|
|
|
|
UserStatus[m.Chat.ID] = USBind1
|
|
|
|
|
|
UserCid[m.Chat.ID] = m.Text
|
2020-03-27 23:02:28 +08:00
|
|
|
|
}
|
2020-03-30 22:18:09 +08:00
|
|
|
|
func bBind2(m *tb.Message) {
|
|
|
|
|
|
tmp := strings.Split(m.Text, " ")
|
|
|
|
|
|
if len(tmp) != 2 {
|
|
|
|
|
|
bot.Send(m.Chat, "错误的格式")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
cid := tmp[0]
|
|
|
|
|
|
cse := tmp[1]
|
2021-03-13 13:15:31 +08:00
|
|
|
|
bot.Send(m.Chat, "授权账户: [点击直达]("+core.GetMSAuthUrl(cid)+")", tb.ModeMarkdown)
|
2020-03-30 22:18:09 +08:00
|
|
|
|
_, err := bot.Send(m.Chat, "请回复http://localhost/…… + 空格 + 别名(用于管理)", &tb.ReplyMarkup{ForceReply: true})
|
2020-04-09 15:47:48 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
2020-03-30 22:18:09 +08:00
|
|
|
|
}
|
2020-04-09 15:47:48 +08:00
|
|
|
|
UserStatus[m.Chat.ID] = USBind2
|
|
|
|
|
|
UserCid[m.Chat.ID] = cid
|
|
|
|
|
|
UserCSecret[m.Chat.ID] = cse
|
2020-03-30 22:18:09 +08:00
|
|
|
|
}
|
2020-04-07 10:56:45 +08:00
|
|
|
|
|
2020-03-28 15:22:23 +08:00
|
|
|
|
func bUnBind(m *tb.Message) {
|
2021-03-12 12:54:32 +08:00
|
|
|
|
data := core.QueryDataByTG(m.Chat.ID)
|
2020-03-28 15:22:23 +08:00
|
|
|
|
var inlineKeys [][]tb.InlineButton
|
|
|
|
|
|
for _, u := range data {
|
|
|
|
|
|
inlineBtn := tb.InlineButton{
|
2021-03-11 12:57:30 +08:00
|
|
|
|
Unique: "unbind" + u.MsId,
|
|
|
|
|
|
Text: u.Alias,
|
|
|
|
|
|
Data: u.MsId,
|
2020-03-28 15:22:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
bot.Handle(&inlineBtn, bUnBindInlineBtn)
|
|
|
|
|
|
inlineKeys = append(inlineKeys, []tb.InlineButton{inlineBtn})
|
|
|
|
|
|
}
|
|
|
|
|
|
bot.Send(m.Chat, "选择一个账户将其解绑\n\n当前绑定数: "+strconv.Itoa(GetBindNum(m.Chat.ID))+"/"+strconv.Itoa(BindMaxNum), &tb.ReplyMarkup{InlineKeyboard: inlineKeys})
|
|
|
|
|
|
}
|
|
|
|
|
|
func bUnBindInlineBtn(c *tb.Callback) {
|
2021-03-12 12:54:32 +08:00
|
|
|
|
r := core.QueryDataByMS(c.Data)
|
2020-03-28 17:16:23 +08:00
|
|
|
|
u := r[0]
|
2021-06-14 13:07:08 +08:00
|
|
|
|
if ok, err := core.DelData(u.MsId); !ok {
|
|
|
|
|
|
zap.S().Errorw("failed to delete db data","error",err,"ms_id",u.MsId)
|
2020-03-28 15:22:23 +08:00
|
|
|
|
bot.Send(c.Message.Chat, "解绑失败!")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
bot.Send(c.Message.Chat, "解绑成功!")
|
|
|
|
|
|
bot.Respond(c)
|
|
|
|
|
|
}
|
2020-04-09 15:47:48 +08:00
|
|
|
|
func bExport(m *tb.Message) {
|
|
|
|
|
|
type MsMiniData struct {
|
|
|
|
|
|
Alias string
|
|
|
|
|
|
ClientId string
|
|
|
|
|
|
ClientSecret string
|
|
|
|
|
|
RefreshToken string
|
|
|
|
|
|
Other string
|
|
|
|
|
|
}
|
|
|
|
|
|
var MsMini []MsMiniData
|
2021-03-12 12:54:32 +08:00
|
|
|
|
data := core.QueryDataByTG(m.Chat.ID)
|
2020-04-09 15:47:48 +08:00
|
|
|
|
if len(data) == 0 {
|
|
|
|
|
|
bot.Send(m.Chat, "你还没有绑定过账户嗷~")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
for _, u := range data {
|
|
|
|
|
|
var ms MsMiniData
|
2021-03-11 12:57:30 +08:00
|
|
|
|
ms.RefreshToken = u.RefreshToken
|
|
|
|
|
|
ms.Alias = u.Alias
|
|
|
|
|
|
ms.ClientId = u.ClientId
|
|
|
|
|
|
ms.ClientSecret = u.ClientSecret
|
|
|
|
|
|
ms.Other = u.Other
|
2020-04-09 15:47:48 +08:00
|
|
|
|
MsMini = append(MsMini, ms)
|
|
|
|
|
|
}
|
2021-03-13 13:15:31 +08:00
|
|
|
|
//MarshalIndent json美化,/t缩进
|
2020-04-09 15:47:48 +08:00
|
|
|
|
export, err := json.MarshalIndent(MsMini, "", "\t")
|
|
|
|
|
|
if err != nil {
|
2021-06-14 13:07:08 +08:00
|
|
|
|
zap.S().Errorw("failed to marshal json","error",err)
|
2020-04-09 15:47:48 +08:00
|
|
|
|
bot.Send(m.Chat, "获取JSON失败~\n"+err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
//fmt.Println(string(export))
|
|
|
|
|
|
fileName := "./" + strconv.FormatInt(m.Chat.ID, 10) + "_export_tmp.json"
|
|
|
|
|
|
if err = ioutil.WriteFile(fileName, export, 0644); err != nil {
|
2021-06-14 13:07:08 +08:00
|
|
|
|
zap.S().Errorw("failed to write file","error",err)
|
2020-04-09 15:47:48 +08:00
|
|
|
|
bot.Send(m.Chat, "写入临时文件失败~\n"+err.Error())
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
exportFile := &tb.Document{File: tb.FromDisk(fileName), FileName: strconv.FormatInt(m.Chat.ID, 10) + ".json", MIME: "text/plain"}
|
2021-06-14 13:07:08 +08:00
|
|
|
|
bot.Send(m.Chat, exportFile)
|
2020-04-09 15:47:48 +08:00
|
|
|
|
//不遗留本地文件
|
2021-06-14 13:07:08 +08:00
|
|
|
|
if exportFile.InCloud() != true || os.Remove(fileName) != nil {
|
|
|
|
|
|
zap.S().Errorw("failed to export files")
|
2020-04-09 15:47:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-03-29 12:51:39 +08:00
|
|
|
|
func bHelp(m *tb.Message) {
|
2020-04-01 15:49:17 +08:00
|
|
|
|
bot.Send(m.Sender, bHelpContent+"\n"+notice, &tb.SendOptions{DisableWebPagePreview: false})
|
2020-03-27 23:02:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
func bOnText(m *tb.Message) {
|
|
|
|
|
|
switch UserStatus[m.Chat.ID] {
|
|
|
|
|
|
case USNone:
|
|
|
|
|
|
{
|
2020-04-07 10:56:45 +08:00
|
|
|
|
bot.Send(m.Chat, "发送/help获取帮助嗷")
|
2020-03-27 23:02:28 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2020-03-30 22:18:09 +08:00
|
|
|
|
case USBind1:
|
|
|
|
|
|
{
|
|
|
|
|
|
if !m.IsReply() {
|
|
|
|
|
|
bot.Send(m.Chat, "请通过回复方式绑定")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
bBind2(m)
|
|
|
|
|
|
}
|
|
|
|
|
|
case USBind2:
|
2020-03-27 23:02:28 +08:00
|
|
|
|
{
|
2020-03-29 17:31:35 +08:00
|
|
|
|
if !m.IsReply() {
|
|
|
|
|
|
bot.Send(m.Chat, "请通过回复方式绑定")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2020-03-27 23:02:28 +08:00
|
|
|
|
if GetBindNum(m.Chat.ID) == BindMaxNum {
|
|
|
|
|
|
bot.Send(m.Chat, "已经达到最大可绑定数")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
bot.Send(m.Chat, "正在绑定中……")
|
2020-04-08 13:42:10 +08:00
|
|
|
|
err := BindUser(m, UserCid[m.Chat.ID], UserCSecret[m.Chat.ID])
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
bot.Send(m.Chat, err.Error())
|
2020-03-27 23:02:28 +08:00
|
|
|
|
} else {
|
2020-04-08 13:42:10 +08:00
|
|
|
|
bot.Send(m.Chat, "绑定成功!")
|
2020-03-27 23:02:28 +08:00
|
|
|
|
}
|
2020-03-28 15:22:23 +08:00
|
|
|
|
UserStatus[m.Chat.ID] = USNone
|
2020-03-27 23:02:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-03-27 11:36:29 +08:00
|
|
|
|
}
|
2020-04-02 09:43:42 +08:00
|
|
|
|
func bTask(m *tb.Message) {
|
|
|
|
|
|
for _, a := range admin {
|
|
|
|
|
|
if a == m.Chat.ID {
|
|
|
|
|
|
SignTask()
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-04-07 10:56:45 +08:00
|
|
|
|
bot.Send(m.Chat, "您没有权限执行此操作~")
|
2020-04-02 09:43:42 +08:00
|
|
|
|
}
|
2020-04-09 15:54:03 +08:00
|
|
|
|
func bLog(m *tb.Message) {
|
|
|
|
|
|
flag := 0
|
|
|
|
|
|
for _, a := range admin {
|
|
|
|
|
|
if a == m.Chat.ID {
|
|
|
|
|
|
flag = 1
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if flag == 0 {
|
|
|
|
|
|
bot.Send(m.Chat, "您没有权限执行此操作~")
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
2021-03-11 12:57:30 +08:00
|
|
|
|
logs := util.GetRecentLogs(bLogBasePath, 5)
|
2020-04-09 15:54:03 +08:00
|
|
|
|
var inlineKeys [][]tb.InlineButton
|
|
|
|
|
|
for _, log := range logs {
|
|
|
|
|
|
inlineBtn := tb.InlineButton{
|
|
|
|
|
|
Unique: "log" + strings.Replace(strings.TrimSuffix(filepath.Base(log), ".log"), "-", "", -1),
|
|
|
|
|
|
Text: filepath.Base(log),
|
|
|
|
|
|
Data: filepath.Base(log),
|
|
|
|
|
|
}
|
|
|
|
|
|
bot.Handle(&inlineBtn, bLogsInlineBtn)
|
|
|
|
|
|
inlineKeys = append(inlineKeys, []tb.InlineButton{inlineBtn})
|
|
|
|
|
|
}
|
2021-06-14 13:07:08 +08:00
|
|
|
|
bot.Send(m.Chat, "选择一个日志", &tb.ReplyMarkup{InlineKeyboard: inlineKeys})
|
2020-04-09 15:54:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
func bLogsInlineBtn(c *tb.Callback) {
|
|
|
|
|
|
//fmt.Println(c.Data)
|
|
|
|
|
|
//logger.Println(bLogBasePath + c.Data + ".log")
|
|
|
|
|
|
logfile := &tb.Document{File: tb.FromDisk(bLogBasePath + c.Data), FileName: c.Data, MIME: "text/plain"}
|
|
|
|
|
|
_, err := bot.Send(c.Message.Chat, logfile)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
bot.Respond(c)
|
|
|
|
|
|
}
|