organise better message handling

This commit is contained in:
Akulij 2025-03-27 21:59:04 +08:00
parent 94894b2c88
commit 8a937724c1

View File

@ -12,12 +12,12 @@ import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
) )
var adminCommands = map[string]func(BotController, tgbotapi.Update){ var adminCommands = map[string]func(BotController, tgbotapi.Update, User){
"/secret": handleMessage, // activate admin mode via /secret `AdminPass` "/secret": handleDefaultMessage, // activate admin mode via /secret `AdminPass`
"/panel": handleMessage, // open bot settings "/panel": handleDefaultMessage, // open bot settings
"/usermode": handleMessage, // temporarly disable admin mode to test ui "/usermode": handleDefaultMessage, // temporarly disable admin mode to test ui
"/id": handleMessage, // to check id of chat "/id": handleDefaultMessage, // to check id of chat
"/setchannelid": handleMessage, // just type it in channel which one is supposed to be lined with bot "/setchannelid": handleDefaultMessage, // just type it in channel which one is supposed to be lined with bot
} }
func main() { func main() {
@ -30,7 +30,16 @@ func main() {
func ProcessUpdate(bc BotController, update tgbotapi.Update) { func ProcessUpdate(bc BotController, update tgbotapi.Update) {
if update.Message != nil { if update.Message != nil {
handleMessage(bc, update) var UserID = update.Message.From.ID
user := bc.GetUser(UserID)
bc.LogMessage(update)
text := update.Message.Text
if strings.HasPrefix(text, "/") {
handleCommand(bc, update, user)
} else {
handleDefaultMessage(bc, update, user)
}
} else if update.CallbackQuery != nil { } else if update.CallbackQuery != nil {
handleCallbackQuery(bc, update) handleCallbackQuery(bc, update)
} else if update.ChannelPost != nil { } else if update.ChannelPost != nil {
@ -38,32 +47,26 @@ func ProcessUpdate(bc BotController, update tgbotapi.Update) {
} }
} }
func handleMessage(bc BotController, update tgbotapi.Update) { func handleCommand(bc BotController, update tgbotapi.Update, user User) {
var UserID = update.Message.From.ID msg := update.Message
user := bc.GetUser(UserID)
bc.LogMessage(update)
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text) log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
log.Printf("[Entities] %s", update.Message.Entities) log.Printf("[Entities] %s", update.Message.Entities)
log.Printf("[COMMAND] %s", update.Message.Command()) log.Printf("[COMMAND] %s", update.Message.Command())
possibleCommand := strings.Split(update.Message.Text, " ")[0] command := "/" + msg.Command() // if it is not a command, then it will simply be "/"
args := strings.Split(update.Message.Text, " ")[1:] if user.IsAdmin() {
log.Printf("Args: %s", args) f, exists := adminCommands[command] // f is a function that handles specified command
if exists {
f(bc, update)
return
}
}
switch { // commands for non-admins
case possibleCommand == "/start": switch command {
case "/start":
handleStartCommand(bc, update, user) handleStartCommand(bc, update, user)
case possibleCommand == "/id" && user.IsAdmin():
bc.bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, strconv.FormatInt(update.Message.Chat.ID, 10)))
case possibleCommand == "/secret" && len(args) > 0 && args[0] == bc.cfg.AdminPass:
handleSecretCommand(bc, update, user)
case possibleCommand == "/panel" && user.IsAdmin():
handlePanelCommand(bc, update, user)
case possibleCommand == "/usermode" && user.IsEffectiveAdmin():
handleUserModeCommand(bc, update, user)
default:
handleDefaultMessage(bc, update, user)
} }
} }
@ -107,16 +110,6 @@ func handleStartCommand(bc BotController, update tgbotapi.Update, user User) {
tgbotapi.NewInlineKeyboardButtonData(bc.GetBotContent("leave_ticket_button"), "leave_ticket_button"), tgbotapi.NewInlineKeyboardButtonData(bc.GetBotContent("leave_ticket_button"), "leave_ticket_button"),
), ),
) )
if user.IsAdmin() {
kbd = tgbotapi.NewInlineKeyboardMarkup(
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData(bc.GetBotContent("leave_ticket_button"), "leave_ticket_button"),
),
tgbotapi.NewInlineKeyboardRow(
tgbotapi.NewInlineKeyboardButtonData("Panel", "panel"),
),
)
}
img, err := bc.GetBotContentVerbose("preview_image") img, err := bc.GetBotContentVerbose("preview_image")
if err != nil || img == "" { if err != nil || img == "" {
@ -133,10 +126,12 @@ func handleStartCommand(bc BotController, update tgbotapi.Update, user User) {
} }
func handleSecretCommand(bc BotController, update tgbotapi.Update, user User) { func handleSecretCommand(bc BotController, update tgbotapi.Update, user User) {
bc.db.Model(&user).Update("state", "start") if update.Message.CommandArguments() == bc.cfg.AdminPass || user.IsAdmin() {
bc.db.Model(&user).Update("RoleBitmask", user.RoleBitmask|0b11) // set real admin ID (0b1) and effective admin toggle (0b10) bc.db.Model(&user).Update("state", "start")
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "You are admin now!") bc.db.Model(&user).Update("RoleBitmask", user.RoleBitmask|0b11) // set real admin ID (0b1) and effective admin toggle (0b10)
bc.bot.Send(msg) msg := tgbotapi.NewMessage(update.Message.Chat.ID, "You are admin now!")
bc.bot.Send(msg)
}
} }
func handlePanelCommand(bc BotController, update tgbotapi.Update, user User) { func handlePanelCommand(bc BotController, update tgbotapi.Update, user User) {