feat: approve button
This commit is contained in:
28
services/tgbot/approve.go
Normal file
28
services/tgbot/approve.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/defs"
|
||||
"tg-mc/models"
|
||||
"tg-mc/services/mc"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func ApproveHandler(update tgbotapi.Update, cmd defs.Command) {
|
||||
u, err := models.GetUserByTGID(update.CallbackQuery.From.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
mc.GetAuthcator().Auth(u)
|
||||
callback := tgbotapi.NewCallback(update.CallbackQuery.ID, "已授权")
|
||||
if _, err := conf.Bot.Request(callback); err != nil {
|
||||
logrus.Panic(err)
|
||||
}
|
||||
conf.Bot.Send(tgbotapi.NewDeleteMessage(update.CallbackQuery.Message.Chat.ID,
|
||||
update.CallbackQuery.Message.MessageID))
|
||||
conf.Bot.Send(tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID,
|
||||
fmt.Sprintf("已授权☑️: %s 登录MC", u.MCName)))
|
||||
}
|
29
services/tgbot/bind.go
Normal file
29
services/tgbot/bind.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/models"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func BindHandler(m *tgbotapi.Message, i interface{}) {
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
err := models.CreateUser(&models.User{
|
||||
TGID: m.From.ID,
|
||||
MCName: m.CommandArguments(),
|
||||
Status: 1,
|
||||
})
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, "绑定失败, err: "+err.Error())
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
|
||||
msg := tgbotapi.NewMessage(m.Chat.ID,
|
||||
fmt.Sprintf("绑定成功,你的MCID是%v", m.CommandArguments()))
|
||||
conf.Bot.Send(msg)
|
||||
return
|
||||
}
|
@@ -1,22 +1,31 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/models"
|
||||
"tg-mc/services/utils"
|
||||
commonUtils "tg-mc/utils"
|
||||
"tg-mc/defs"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/samber/lo"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func Run(sendFunc func(string)) {
|
||||
var funcHandlers = map[string]func(*tgbotapi.Message, interface{}){
|
||||
"talk": TalkHandler,
|
||||
"list": ListHandler,
|
||||
"bind": BindHandler,
|
||||
"unbind": UnbindHandler,
|
||||
"get": GetHandler,
|
||||
"set": SetHandler,
|
||||
}
|
||||
|
||||
var callBackHandlers = map[string]func(tgbotapi.Update, defs.Command){
|
||||
defs.CMD_APPROVE: ApproveHandler,
|
||||
defs.CMD_REJECT: RejectHandler,
|
||||
}
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
|
||||
api := conf.GetBotSettings().BotAPI
|
||||
@@ -43,161 +52,33 @@ func Run(sendFunc func(string)) {
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
|
||||
conf.Bot.Debug = false
|
||||
|
||||
log.Printf("Authorized on account %s", conf.Bot.Self.UserName)
|
||||
}
|
||||
|
||||
func Run(sendFunc func(string)) {
|
||||
u := tgbotapi.NewUpdate(0)
|
||||
u.Timeout = 60
|
||||
updates := conf.Bot.GetUpdatesChan(u)
|
||||
|
||||
for update := range updates {
|
||||
if update.Message != nil {
|
||||
if update.CallbackQuery != nil {
|
||||
go func(update tgbotapi.Update) {
|
||||
logrus.Infof("[%s] %s", update.CallbackQuery.From.UserName, update.CallbackQuery.Data)
|
||||
cmd, err := defs.NewCommandFromJSON(update.CallbackQuery.Data)
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
return
|
||||
}
|
||||
if handler, ok := callBackHandlers[cmd.Command]; ok {
|
||||
handler(update, *cmd)
|
||||
}
|
||||
}(update)
|
||||
} else if update.Message != nil {
|
||||
go func(m *tgbotapi.Message) {
|
||||
logrus.Infof("[%s] %s", m.From.UserName, m.Text)
|
||||
if m.Command() == "talk" {
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
m := fmt.Sprintf("%v: %v", m.From.UserName, m.CommandArguments())
|
||||
sendFunc(m)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("send message error")
|
||||
}
|
||||
return
|
||||
}
|
||||
if m.Command() == "list" {
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, utils.GetAlivePlayer())
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
if m.Command() == "bind" {
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
err := models.CreateUser(&models.User{
|
||||
TGID: m.From.ID,
|
||||
MCName: m.CommandArguments(),
|
||||
Status: 1,
|
||||
})
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, "绑定失败, err: "+err.Error())
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
m := tgbotapi.NewMessage(m.Chat.ID,
|
||||
fmt.Sprintf("绑定成功,你的MCID是%v", m.CommandArguments()))
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
if m.Command() == "unbind" {
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
u, err := models.GetUserByTGID(m.From.ID)
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, "你还没有绑定")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
err = u.Delete(m.From.ID)
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, "解绑失败")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, "解绑成功")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
if m.Command() == "get" {
|
||||
if !utils.IsAdmin(m) &&
|
||||
len(m.CommandArguments()) != 0 {
|
||||
tm := tgbotapi.NewMessage(m.Chat.ID, "您不是管理员,没有该权限")
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
} else if utils.IsAdmin(m) &&
|
||||
len(m.CommandArguments()) != 0 {
|
||||
a := commonUtils.GetArgs(m.CommandArguments())
|
||||
if len(a) != 2 {
|
||||
tm := tgbotapi.NewMessage(m.Chat.ID, "参数错误,样例:\n```\n/get <tgid|username> <value>\n```")
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
if a[0] == "tgid" {
|
||||
tgid, err := strconv.ParseInt(a[1], 10, 64)
|
||||
if err != nil {
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, "ID错误,应该为int64"))
|
||||
return
|
||||
}
|
||||
u, err := models.GetUserByTGID(tgid)
|
||||
if err != nil {
|
||||
tm := tgbotapi.NewMessage(m.Chat.ID, fmt.Sprintf("查询出错,err:\n```\n%+v\n```", err))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
tm := tgbotapi.NewMessage(m.Chat.ID, fmt.Sprintf("用户信息:\n```\n%+v\n```", u))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
}
|
||||
if a[0] == "username" {
|
||||
u, err := models.GetUserByMCName(a[1])
|
||||
if err != nil {
|
||||
tm := tgbotapi.NewMessage(m.Chat.ID, fmt.Sprintf("查询出错,err:\n```\n%+v\n```", err))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
tm := tgbotapi.NewMessage(m.Chat.ID, fmt.Sprintf("用户信息:\n```\n%+v\n```", u))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
}
|
||||
return
|
||||
}
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
u, err := models.GetUserByTGID(m.From.ID)
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, "你还没有绑定")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
m := tgbotapi.NewMessage(m.Chat.ID, fmt.Sprintf("你的MCID是%v", u.MCName))
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
if m.Command() == "set" {
|
||||
if !utils.IsAdmin(m) {
|
||||
tm := tgbotapi.NewMessage(m.Chat.ID, "您不是管理员,没有该权限")
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
a := commonUtils.GetArgs(m.CommandArguments())
|
||||
if len(a) != 3 {
|
||||
tm := tgbotapi.NewMessage(m.Chat.ID, "参数错误,样例:\n```\n/set username tgid status\n```")
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
tgid, err := strconv.ParseInt(a[1], 10, 64)
|
||||
if err != nil {
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, "ID错误,应该为int64"))
|
||||
return
|
||||
}
|
||||
status, err := strconv.ParseInt(a[2], 10, 64)
|
||||
if err != nil || !lo.Contains([]int64{0, 1, 2}, status) {
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, "Status错误,应该为0(Pending),1(Normal),2(Banned)"))
|
||||
return
|
||||
}
|
||||
if err := models.CreateUser(&models.User{
|
||||
TGID: tgid,
|
||||
MCName: a[0],
|
||||
Status: int(status),
|
||||
}); err != nil {
|
||||
tm := tgbotapi.NewMessage(m.Chat.ID, fmt.Sprintf("创建用户错误,err:\n```\n%+v\n```", err))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, "设置用户成功"))
|
||||
return
|
||||
if handler, ok := funcHandlers[m.Command()]; ok {
|
||||
handler(m, sendFunc)
|
||||
}
|
||||
}(update.Message)
|
||||
}
|
||||
|
71
services/tgbot/get.go
Normal file
71
services/tgbot/get.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/models"
|
||||
"tg-mc/services/utils"
|
||||
commonUtils "tg-mc/utils"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func GetHandler(msg *tgbotapi.Message, i interface{}) {
|
||||
if !utils.IsAdmin(msg) &&
|
||||
len(msg.CommandArguments()) != 0 {
|
||||
tm := tgbotapi.NewMessage(msg.Chat.ID, "您不是管理员,没有该权限")
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
} else if utils.IsAdmin(msg) &&
|
||||
len(msg.CommandArguments()) != 0 {
|
||||
a := commonUtils.GetArgs(msg.CommandArguments())
|
||||
if len(a) != 2 {
|
||||
tm := tgbotapi.NewMessage(msg.Chat.ID, "参数错误,样例:\n```\n/get <tgid|username> <value>\n```")
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
if a[0] == "tgid" {
|
||||
tgid, err := strconv.ParseInt(a[1], 10, 64)
|
||||
if err != nil {
|
||||
conf.Bot.Send(tgbotapi.NewMessage(msg.Chat.ID, "ID错误,应该为int64"))
|
||||
return
|
||||
}
|
||||
u, err := models.GetUserByTGID(tgid)
|
||||
if err != nil {
|
||||
tm := tgbotapi.NewMessage(msg.Chat.ID, fmt.Sprintf("查询出错,err:\n```\n%+v\n```", err))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
tm := tgbotapi.NewMessage(msg.Chat.ID, fmt.Sprintf("用户信息:\n```\n%+v\n```", u))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
}
|
||||
if a[0] == "username" {
|
||||
u, err := models.GetUserByMCName(a[1])
|
||||
if err != nil {
|
||||
tm := tgbotapi.NewMessage(msg.Chat.ID, fmt.Sprintf("查询出错,err:\n```\n%+v\n```", err))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
tm := tgbotapi.NewMessage(msg.Chat.ID, fmt.Sprintf("用户信息:\n```\n%+v\n```", u))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
}
|
||||
return
|
||||
}
|
||||
logrus.Infof("id is %d", msg.Chat.ID)
|
||||
u, err := models.GetUserByTGID(msg.From.ID)
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(msg.Chat.ID, "你还没有绑定")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
m := tgbotapi.NewMessage(msg.Chat.ID, fmt.Sprintf("你的MCID是%v", u.MCName))
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
15
services/tgbot/list.go
Normal file
15
services/tgbot/list.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"tg-mc/conf"
|
||||
"tg-mc/services/utils"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func ListHandler(m *tgbotapi.Message, i interface{}) {
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
msg := tgbotapi.NewMessage(m.Chat.ID, utils.GetAlivePlayer())
|
||||
conf.Bot.Send(msg)
|
||||
}
|
26
services/tgbot/reject.go
Normal file
26
services/tgbot/reject.go
Normal file
@@ -0,0 +1,26 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/defs"
|
||||
"tg-mc/models"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func RejectHandler(update tgbotapi.Update, cmd defs.Command) {
|
||||
u, err := models.GetUserByTGID(update.CallbackQuery.From.ID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
callback := tgbotapi.NewCallback(update.CallbackQuery.ID, "已拒绝")
|
||||
if _, err := conf.Bot.Request(callback); err != nil {
|
||||
logrus.Panic(err)
|
||||
}
|
||||
conf.Bot.Send(tgbotapi.NewDeleteMessage(update.CallbackQuery.Message.Chat.ID,
|
||||
update.CallbackQuery.Message.MessageID))
|
||||
conf.Bot.Send(tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID,
|
||||
fmt.Sprintf("已拒绝❌: %s 登录MC", u.MCName)))
|
||||
}
|
50
services/tgbot/set.go
Normal file
50
services/tgbot/set.go
Normal file
@@ -0,0 +1,50 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"tg-mc/conf"
|
||||
"tg-mc/models"
|
||||
"tg-mc/services/utils"
|
||||
commonUtils "tg-mc/utils"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func SetHandler(m *tgbotapi.Message, i interface{}) {
|
||||
if !utils.IsAdmin(m) {
|
||||
tm := tgbotapi.NewMessage(m.Chat.ID, "您不是管理员,没有该权限")
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
a := commonUtils.GetArgs(m.CommandArguments())
|
||||
if len(a) != 3 {
|
||||
tm := tgbotapi.NewMessage(m.Chat.ID, "参数错误,样例:\n```\n/set username tgid status\n```")
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
tgid, err := strconv.ParseInt(a[1], 10, 64)
|
||||
if err != nil {
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, "ID错误,应该为int64"))
|
||||
return
|
||||
}
|
||||
status, err := strconv.ParseInt(a[2], 10, 64)
|
||||
if err != nil || !lo.Contains([]int64{0, 1, 2}, status) {
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, "Status错误,应该为0(Pending),1(Normal),2(Banned)"))
|
||||
return
|
||||
}
|
||||
if err := models.CreateUser(&models.User{
|
||||
TGID: tgid,
|
||||
MCName: a[0],
|
||||
Status: int(status),
|
||||
}); err != nil {
|
||||
tm := tgbotapi.NewMessage(m.Chat.ID, fmt.Sprintf("创建用户错误,err:\n```\n%+v\n```", err))
|
||||
tm.ParseMode = "Markdown"
|
||||
conf.Bot.Send(tm)
|
||||
return
|
||||
}
|
||||
conf.Bot.Send(tgbotapi.NewMessage(m.Chat.ID, "设置用户成功"))
|
||||
return
|
||||
}
|
15
services/tgbot/talk.go
Normal file
15
services/tgbot/talk.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func TalkHandler(m *tgbotapi.Message, i interface{}) {
|
||||
sendFunc := i.(func(string))
|
||||
logrus.Infof("id is %d", m.Chat.ID)
|
||||
msg := fmt.Sprintf("%v: %v", m.From.UserName, m.CommandArguments())
|
||||
sendFunc(msg)
|
||||
}
|
28
services/tgbot/unbind.go
Normal file
28
services/tgbot/unbind.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"tg-mc/conf"
|
||||
"tg-mc/models"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func UnbindHandler(msg *tgbotapi.Message, i interface{}) {
|
||||
logrus.Infof("id is %d", msg.Chat.ID)
|
||||
u, err := models.GetUserByTGID(msg.From.ID)
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(msg.Chat.ID, "你还没有绑定")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
err = u.Delete(msg.From.ID)
|
||||
if err != nil {
|
||||
m := tgbotapi.NewMessage(msg.Chat.ID, "解绑失败")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
||||
m := tgbotapi.NewMessage(msg.Chat.ID, "解绑成功")
|
||||
conf.Bot.Send(m)
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user