diff --git a/cli/commands/help.go b/cli/commands/help.go index cacfcaa..9c94989 100644 --- a/cli/commands/help.go +++ b/cli/commands/help.go @@ -40,10 +40,7 @@ func (h helpCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Updat _, err := bot.Send(msg) - if err != nil { - return false, err - } - return true, nil + return err == nil, nil } func (h helpCommand) AllowChatType(chat *tgbotapi.Chat) bool { diff --git a/cli/commands/info.go b/cli/commands/info.go index 001ab48..1f63a71 100644 --- a/cli/commands/info.go +++ b/cli/commands/info.go @@ -38,10 +38,7 @@ func (i infoCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Updat _, err := bot.Send(msg) - if err != nil { - return false, err - } - return true, nil + return err == nil, nil } func (i infoCommand) AllowChatType(*tgbotapi.Chat) bool { diff --git a/cli/commands/joinmessage.go b/cli/commands/joinmessage.go new file mode 100644 index 0000000..2901c5f --- /dev/null +++ b/cli/commands/joinmessage.go @@ -0,0 +1,90 @@ +package commands + +import ( + "git.dragon-labs.de/alphyron/group_helper/cli" + "git.dragon-labs.de/alphyron/group_helper/logic" + tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" + "strings" +) + +type joinMessage struct { + groupHelperService logic.GroupHelperService +} + +func NewJoinMessage(groupHelperService logic.GroupHelperService) cli.Command { + return &joinMessage{ + groupHelperService: groupHelperService, + } +} + +func (j joinMessage) GetUsage() string { + return "/join (set/get) [message]" +} + +func (j joinMessage) GetCommand() string { + return "/join" +} + +func (j joinMessage) GetDescription() string { + return "Get or set the join message" +} + +func (j joinMessage) ExecuteCommand(api *tgbotapi.BotAPI, update *tgbotapi.Update) (bool, error) { + message := update.Message.Text + parts := strings.Split(message, " ") + + if len(parts) == 0 { + _, err := api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Wrong usage of this Command:\n"+j.GetUsage())) + return err != nil, err + } + + args := strings.Split(message, " ") + group, err := j.groupHelperService.GetGroupByID(update.Message.Chat.ID) + + if err != nil { + return false, err + } + + switch args[1] { + case "get": + msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserJoinMessage) + api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Current JoinMessage (decoded)")) + _, err = api.Send(msg) + return err != nil, err + case "set": + if len(parts) <= 2 { + _, err := api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Wrong usage of this Command:\n"+j.GetUsage())) + return err != nil, err + } + + newJoinMessage := strings.Join(args[2:], " ") + group.UserJoinMessage = newJoinMessage + group, err = j.groupHelperService.UpdateGroup(group) + + if err != nil { + return false, err + } + + //TODO Create a replace for placeholder + msg := tgbotapi.NewMessage(update.Message.Chat.ID, group.UserJoinMessage) + msg.ParseMode = "Markdown" + api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Example JoinMessage")) + _, err = api.Send(msg) + return err != nil, err + default: + _, err := api.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Wrong usage of this Command:\n"+j.GetUsage())) + return err != nil, err + } +} + +func (j joinMessage) AllowChatType(chat *tgbotapi.Chat) bool { + return chat.IsGroup() || chat.IsSuperGroup() +} + +func (j joinMessage) AllowMember(member *tgbotapi.ChatMember) bool { + return member.IsAdministrator() || member.IsCreator() +} + +func (j joinMessage) AllowEveryMember() bool { + return false +} diff --git a/cli/manager.go b/cli/manager.go index d397708..279748c 100644 --- a/cli/manager.go +++ b/cli/manager.go @@ -54,10 +54,8 @@ func (cm CommandManager) ExecuteUpdate(update *tgbotapi.Update) (bool, error) { 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 err == nil, nil } } diff --git a/logic/grouphelperservice.go b/logic/grouphelperservice.go index 46fb9ee..535b70c 100644 --- a/logic/grouphelperservice.go +++ b/logic/grouphelperservice.go @@ -31,7 +31,7 @@ func (g groupHelperService) UpdateGroup(group *models.Group) (*models.Group, err return nil, errors.New("you need set the group id to update an existing group") } - return g.UpdateGroup(group) + return g.groupHelperRepo.UpdateGroup(group) } func (g groupHelperService) GetGroupByID(groupID int64) (*models.Group, error) { diff --git a/logic/service.go b/logic/service.go index 2815cb6..68d418a 100644 --- a/logic/service.go +++ b/logic/service.go @@ -2,7 +2,6 @@ package logic import ( "git.dragon-labs.de/alphyron/group_helper/models" - tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" ) type GroupHelperService interface { @@ -22,20 +21,3 @@ 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 d08546e..133534f 100644 --- a/main.go +++ b/main.go @@ -44,9 +44,16 @@ func main() { log.Fatal(err) } - logic.NewGroupHelperService(groupHelperRepo) + err = groupHelperRepo.InitRepository() - cm := InitialCommandManager(bot) + if err != nil { + log.Println("Problem while initial the Database") + log.Fatal(err) + } + + service := logic.NewGroupHelperService(groupHelperRepo) + + cm := InitialCommandManager(bot, service) u := tgbotapi.NewUpdate(0) u.Timeout = 60 @@ -62,7 +69,7 @@ func main() { } } -func InitialCommandManager(bot *tgbotapi.BotAPI) *cli.CommandManager { +func InitialCommandManager(bot *tgbotapi.BotAPI, service logic.GroupHelperService) *cli.CommandManager { cm := &cli.CommandManager{ Commands: make([]cli.Command, 0), Bot: bot, @@ -70,6 +77,7 @@ func InitialCommandManager(bot *tgbotapi.BotAPI) *cli.CommandManager { cm.RegisterCommand(commands.NewInfoCommand()) cm.RegisterCommand(commands.NewHelpCommand(cm)) + cm.RegisterCommand(commands.NewJoinMessage(service)) return cm } diff --git a/repository/sql/grouphelprepository.go b/repository/sql/grouphelprepository.go index 2346f14..ee59858 100644 --- a/repository/sql/grouphelprepository.go +++ b/repository/sql/grouphelprepository.go @@ -89,12 +89,17 @@ func (g groupHelperRepository) UpdateGroup(group *models.Group) (*models.Group, func (g groupHelperRepository) GetGroupByID(id int64) (*models.Group, error) { var group models.Group - err := g.Conn.Where("group_id = ?", id).First(&group).Error - if err != nil { - return nil, errors.New("problem while getting the group by his group id - " + err.Error()) + defaultGroup := &models.Group{} + defaultGroup.FillDefaultValues() + defaultGroup.GroupID = id + + err := g.Conn.Where("group_id = ?", id).First(&group) + + if err.RecordNotFound() { + return g.CreateGroup(defaultGroup) } - return &group, nil + return &group, err.Error } func (g groupHelperRepository) GetGroupDatabaseSize(group *models.Group) (int, error) {