diff --git a/cli/command.go b/cli/command.go new file mode 100644 index 0000000..70ca79c --- /dev/null +++ b/cli/command.go @@ -0,0 +1,13 @@ +package cli + +import tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + +type Command interface { + GetUsage() string + GetCommand() string + GetDescription() string + ExecuteCommand(*tgbotapi.BotAPI, *tgbotapi.Update) (bool, error) + AllowChatType(*tgbotapi.Chat) bool + AllowMember(*tgbotapi.ChatMember) bool + AllowEveryMember() bool +} diff --git a/cli/commands/info.go b/cli/commands/info.go new file mode 100644 index 0000000..001ab48 --- /dev/null +++ b/cli/commands/info.go @@ -0,0 +1,57 @@ +package commands + +import ( + "git.dragon-labs.de/alphyron/group_helper/cli" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" +) + +type infoCommand struct { +} + +func NewInfoCommand() cli.Command { + return &infoCommand{} +} + +func (i infoCommand) GetUsage() string { + return "/info" +} + +func (i infoCommand) GetCommand() string { + return "/info" +} + +func (i infoCommand) GetDescription() string { + return "Just some Bot information" +} + +func (i infoCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Update) (bool, error) { + infoMessage := + "General Information to this Bot\n" + + "===============================\n" + + "Developer: @Alphyron\n" + + "Version: 2.0.0\n" + + "Git: [Gitea Repository](https://git.dragon-labs.de/alphyron/group_assistant)\n" + + "===============================" + + msg := tgbotapi.NewMessage(update.Message.Chat.ID, infoMessage) + msg.ParseMode = "Markdown" + + _, err := bot.Send(msg) + + if err != nil { + return false, err + } + return true, nil +} + +func (i infoCommand) AllowChatType(*tgbotapi.Chat) bool { + return true +} + +func (i infoCommand) AllowMember(*tgbotapi.ChatMember) bool { + return i.AllowEveryMember() +} + +func (i infoCommand) AllowEveryMember() bool { + return true +} diff --git a/cli/manager.go b/cli/manager.go new file mode 100644 index 0000000..d397708 --- /dev/null +++ b/cli/manager.go @@ -0,0 +1,73 @@ +package cli + +import ( + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "strings" +) + +type CommandManager struct { + Commands []Command + Bot *tgbotapi.BotAPI +} + +func (cm CommandManager) ExecuteUpdate(update *tgbotapi.Update) (bool, error) { + if update.Message == nil { + return true, nil + } + + if update.Message.Text == "" { + return true, nil + } + + cmdStr := update.Message.Text + + if !strings.HasPrefix(cmdStr, "/") { + return true, nil + } + + var command Command + + for _, cmd := range cm.Commands { + if strings.HasPrefix(cmdStr, cmd.GetCommand()) { + command = cmd + break + } + } + + if command == nil { + return true, nil + } + + if !command.AllowChatType(update.Message.Chat) { + return true, nil + } + + if !command.AllowEveryMember() { + userconfig := tgbotapi.ChatConfigWithUser{ + ChatID: update.Message.Chat.ID, + UserID: update.Message.From.ID, + } + + member, _ := cm.Bot.GetChatMember(userconfig) + + if !command.AllowMember(&member) { + msg := tgbotapi.NewMessage(update.Message.Chat.ID, "You are not allowed to use this command.") + msg.ReplyToMessageID = update.Message.MessageID + _, err := cm.Bot.Send(msg) + if err != nil { + return false, err + } + return true, nil + } + } + + return command.ExecuteCommand(cm.Bot, update) +} + +func (cm *CommandManager) RegisterCommand(command Command) { + if cm.Commands == nil { + cm.Commands = make([]Command, 0) + } + + cm.Commands = append(cm.Commands, command) +} diff --git a/logic/service.go b/logic/service.go index aa45c29..2815cb6 100644 --- a/logic/service.go +++ b/logic/service.go @@ -1,6 +1,9 @@ package logic -import "git.dragon-labs.de/alphyron/group_helper/models" +import ( + "git.dragon-labs.de/alphyron/group_helper/models" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" +) type GroupHelperService interface { CreateGroup(*models.Group) (*models.Group, error) @@ -19,3 +22,20 @@ type GroupHelperService interface { ListGroups() ([]*models.Group, error) ListUsers() ([]*models.User, error) } + +type CommandService interface { + HelpCommand(*tgbotapi.Message) error + InfoCommand(*tgbotapi.Message) error + PlaceholderCommand(*tgbotapi.Message) error + PrintJoinMessage(*tgbotapi.Message) error + PrintLeaveMessage(*tgbotapi.Message) error + PrintKickMessage(*tgbotapi.Message) error + PrintVerifiedMessage(*tgbotapi.Message) error + PrintGroupStatus() error + UpdateJoinMessage(*tgbotapi.Message) error + UpdateLeaveMessage(*tgbotapi.Message) error + UpdateKickMessage(*tgbotapi.Message) error + UpdateVerifiedMessage(*tgbotapi.Message) error + UpdateOnlineCheck(*tgbotapi.Message) error + UpdateForbidWriting(*tgbotapi.Message) error +} diff --git a/main.go b/main.go index b9b860a..b205ab1 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,8 @@ package main import ( + "git.dragon-labs.de/alphyron/group_helper/cli" + "git.dragon-labs.de/alphyron/group_helper/cli/commands" "git.dragon-labs.de/alphyron/group_helper/config" "git.dragon-labs.de/alphyron/group_helper/logic" "git.dragon-labs.de/alphyron/group_helper/repository" @@ -42,7 +44,31 @@ func main() { log.Fatal(err) } - /* groupHelperService := */ logic.NewGroupHelperService(groupHelperRepo) + cm := InitialCommandManager(bot) + + u := tgbotapi.NewUpdate(0) + u.Timeout = 60 + updates, _ := bot.GetUpdatesChan(u) + + for update := range updates { + _, err := cm.ExecuteUpdate(&update) + + if err != nil { + log.Printf("ERROR - Command error") + log.Println(err) + } + } +} + +func InitialCommandManager(bot *tgbotapi.BotAPI) *cli.CommandManager { + cm := &cli.CommandManager{ + Commands: make([]cli.Command, 0), + Bot: bot, + } + + cm.RegisterCommand(commands.NewInfoCommand()) + + return cm }