E5BotForSQLite/bots/handle.go

281 lines
6.8 KiB
Go
Raw Normal View History

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 (
"encoding/json"
2021-06-14 23:17:11 +08:00
"fmt"
"github.com/iyear/E5SubBot/config"
"github.com/iyear/E5SubBot/model"
2021-06-14 13:07:08 +08:00
"github.com/iyear/E5SubBot/util"
"go.uber.org/zap"
2020-03-27 23:02:28 +08:00
tb "gopkg.in/tucnak/telebot.v2"
"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
var (
2021-06-14 23:17:11 +08:00
UserStatus map[int64]int
UserClientId map[int64]string
UserClientSecret map[int64]string
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() {
UserStatus = make(map[int64]int)
2021-06-14 23:17:11 +08:00
UserClientId = make(map[int64]string)
UserClientSecret = make(map[int64]string)
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) {
2021-06-14 23:17:11 +08:00
bot.Send(m.Sender, config.WelcomeContent)
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-06-14 23:17:11 +08:00
var data []*model.Client
model.DB.Where("tg_id = ?", m.Chat.ID).Find(&data)
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-06-15 18:44:51 +08:00
Unique: "my" + strconv.Itoa(u.ID),
2021-03-11 12:57:30 +08:00
Text: u.Alias,
2021-06-15 18:44:51 +08:00
Data: strconv.Itoa(u.ID),
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})
}
2021-06-14 23:17:11 +08:00
bot.Send(m.Chat,
fmt.Sprintf("选择一个账户查看具体信息\n\n绑定数: %d/%d", GetBindNum(m.Chat.ID), config.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-06-14 23:17:11 +08:00
var u *model.Client
2021-06-15 18:44:51 +08:00
model.DB.Where("id = ?", c.Data).First(&u)
2021-06-14 23:17:11 +08:00
bot.Send(c.Message.Chat,
2021-06-15 19:04:26 +08:00
fmt.Sprintf("信息\n别名%s\nms_id: %s\nclient_id: %s\nclient_secret: %s\n最近更新时间: %s",
2021-06-14 23:17:11 +08:00
u.Alias,
u.MsId,
u.ClientId,
u.ClientSecret,
2021-06-15 19:04:26 +08:00
time.Unix(u.Uptime, 0).Format("2006-01-02 15:04:05"),
),
2021-06-14 23:17:11 +08:00
)
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-06-14 23:17:11 +08:00
bot.Send(m.Chat,
fmt.Sprintf("应用注册: [点击直达](%s)", model.GetMSRegisterAppUrl()),
2021-06-15 19:04:26 +08:00
tb.ModeMarkdown,
)
2021-06-14 23:17:11 +08:00
bot.Send(m.Chat,
2021-06-15 19:04:26 +08:00
"请回复 `client_id(空格)client_secret`",
&tb.SendOptions{ParseMode: tb.ModeMarkdown,
ReplyMarkup: &tb.ReplyMarkup{ForceReply: true}},
)
UserStatus[m.Chat.ID] = USBind1
2021-06-14 23:17:11 +08:00
UserClientId[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
}
2021-06-14 23:17:11 +08:00
ClientId := tmp[0]
ClientSecret := tmp[1]
bot.Send(m.Chat,
2021-06-15 19:04:26 +08:00
"授权账户: [点击直达]("+model.GetMSAuthUrl(ClientId)+")",
tb.ModeMarkdown,
)
bot.Send(m.Chat,
"请回复`http://localhost/……(空格)别名`(用于管理)",
&tb.SendOptions{ParseMode: tb.ModeMarkdown,
ReplyMarkup: &tb.ReplyMarkup{ForceReply: true},
},
)
UserStatus[m.Chat.ID] = USBind2
2021-06-14 23:17:11 +08:00
UserClientId[m.Chat.ID] = ClientId
UserClientSecret[m.Chat.ID] = ClientSecret
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-06-14 23:17:11 +08:00
var data []*model.Client
model.DB.Where("tg_id = ?", m.Chat.ID).Find(&data)
2020-03-28 15:22:23 +08:00
var inlineKeys [][]tb.InlineButton
2021-06-14 23:17:11 +08:00
2020-03-28 15:22:23 +08:00
for _, u := range data {
inlineBtn := tb.InlineButton{
2021-06-15 18:44:51 +08:00
Unique: "unbind" + strconv.Itoa(u.ID),
2021-03-11 12:57:30 +08:00
Text: u.Alias,
2021-06-15 18:44:51 +08:00
Data: strconv.Itoa(u.ID),
2020-03-28 15:22:23 +08:00
}
bot.Handle(&inlineBtn, bUnBindInlineBtn)
inlineKeys = append(inlineKeys, []tb.InlineButton{inlineBtn})
}
2021-06-14 23:17:11 +08:00
bot.Send(m.Chat,
fmt.Sprintf("选择一个账户将其解绑\n\n当前绑定数: %d/%d", GetBindNum(m.Chat.ID), config.BindMaxNum),
&tb.ReplyMarkup{InlineKeyboard: inlineKeys},
)
2020-03-28 15:22:23 +08:00
}
func bUnBindInlineBtn(c *tb.Callback) {
2021-06-15 18:44:51 +08:00
if result := model.DB.Where("id = ?", c.Data).Delete(&model.Client{}); result.Error != nil {
2021-06-14 23:17:11 +08:00
zap.S().Errorw("failed to delete db data",
"error", result.Error,
2021-06-15 18:44:51 +08:00
"id", c.Data,
2021-06-14 23:17:11 +08:00
)
2020-03-28 15:22:23 +08:00
bot.Send(c.Message.Chat, "解绑失败!")
return
}
bot.Send(c.Message.Chat, "解绑成功!")
bot.Respond(c)
}
func bExport(m *tb.Message) {
2021-06-14 23:17:11 +08:00
type ClientExport struct {
Alias string
ClientId string
ClientSecret string
RefreshToken string
Other string
}
2021-06-14 23:17:11 +08:00
var exports []ClientExport
var data []*model.Client
model.DB.Where("tg_id = ?", m.Chat.ID).Find(&data)
if len(data) == 0 {
bot.Send(m.Chat, "你还没有绑定过账户嗷~")
return
}
for _, u := range data {
2021-06-14 23:17:11 +08:00
var cExport = ClientExport{
Alias: u.Alias,
ClientId: u.ClientId,
ClientSecret: u.ClientSecret,
RefreshToken: u.RefreshToken,
Other: u.Other,
}
exports = append(exports, cExport)
}
2021-06-14 23:17:11 +08:00
export, err := json.MarshalIndent(exports, "", "\t")
if err != nil {
2021-06-14 23:17:11 +08:00
zap.S().Errorw("failed to marshal json",
"error", err)
bot.Send(m.Chat, fmt.Sprintf("获取JSON失败!\n\nERROR: %s", err.Error()))
return
}
2021-06-14 23:17:11 +08:00
fileName := fmt.Sprintf("./%d_export_tmp.json", m.Chat.ID)
if err = ioutil.WriteFile(fileName, export, 0644); err != nil {
2021-06-14 23:17:11 +08:00
zap.S().Errorw("failed to write file",
"error", err)
bot.Send(m.Chat, "写入临时文件失败~\n"+err.Error())
return
}
2021-06-14 23:17:11 +08:00
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)
2021-06-18 19:36:13 +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-03-29 12:51:39 +08:00
func bHelp(m *tb.Message) {
2021-06-14 23:17:11 +08:00
bot.Send(
m.Sender,
config.HelpContent+"\n"+config.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:
{
2021-06-15 19:04:26 +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
}
2021-06-14 23:17:11 +08:00
if GetBindNum(m.Chat.ID) == config.BindMaxNum {
2020-03-27 23:02:28 +08:00
bot.Send(m.Chat, "已经达到最大可绑定数")
return
}
bot.Send(m.Chat, "正在绑定中……")
2021-06-14 23:17:11 +08:00
err := BindUser(m, UserClientId[m.Chat.ID], UserClientSecret[m.Chat.ID])
2020-04-08 13:42:10 +08:00
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) {
2021-06-14 23:17:11 +08:00
for _, a := range config.Admins {
2020-04-02 09:43:42 +08:00
if a == m.Chat.ID {
2021-06-15 18:27:28 +08:00
SignTask()
2020-04-02 09:43:42 +08:00
return
}
}
2021-06-14 23:17:11 +08:00
bot.Send(m.Chat, "只有Bot管理员才有权限执行此操作")
2020-04-02 09:43:42 +08:00
}
2020-04-09 15:54:03 +08:00
func bLog(m *tb.Message) {
flag := 0
2021-06-14 23:17:11 +08:00
for _, a := range config.Admins {
2020-04-09 15:54:03 +08:00
if a == m.Chat.ID {
flag = 1
}
}
if flag == 0 {
2021-06-14 23:17:11 +08:00
bot.Send(m.Chat, "只有Bot管理员才有权限执行此操作")
2020-04-09 15:54:03 +08:00
return
}
2021-06-14 23:17:11 +08:00
logs := util.GetRecentLogs(config.LogBasePath, 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 23:17:11 +08:00
bot.Send(m.Chat, "请选择日志", &tb.ReplyMarkup{InlineKeyboard: inlineKeys})
2020-04-09 15:54:03 +08:00
}
func bLogsInlineBtn(c *tb.Callback) {
2021-06-14 23:17:11 +08:00
logfile := &tb.Document{
File: tb.FromDisk(config.LogBasePath + c.Data),
FileName: c.Data,
MIME: "text/plain",
2020-04-09 15:54:03 +08:00
}
2021-06-14 23:17:11 +08:00
bot.Send(c.Message.Chat, logfile)
2020-04-09 15:54:03 +08:00
bot.Respond(c)
}