✨ Implement command manager and create first command
This commit is contained in:
parent
a08defa167
commit
0f6ee5893c
13
cli/command.go
Normal file
13
cli/command.go
Normal file
@ -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
|
||||||
|
}
|
57
cli/commands/info.go
Normal file
57
cli/commands/info.go
Normal file
@ -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
|
||||||
|
}
|
73
cli/manager.go
Normal file
73
cli/manager.go
Normal file
@ -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)
|
||||||
|
}
|
@ -1,6 +1,9 @@
|
|||||||
package logic
|
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 {
|
type GroupHelperService interface {
|
||||||
CreateGroup(*models.Group) (*models.Group, error)
|
CreateGroup(*models.Group) (*models.Group, error)
|
||||||
@ -19,3 +22,20 @@ type GroupHelperService interface {
|
|||||||
ListGroups() ([]*models.Group, error)
|
ListGroups() ([]*models.Group, error)
|
||||||
ListUsers() ([]*models.User, 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
|
||||||
|
}
|
||||||
|
28
main.go
28
main.go
@ -1,6 +1,8 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
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/config"
|
||||||
"git.dragon-labs.de/alphyron/group_helper/logic"
|
"git.dragon-labs.de/alphyron/group_helper/logic"
|
||||||
"git.dragon-labs.de/alphyron/group_helper/repository"
|
"git.dragon-labs.de/alphyron/group_helper/repository"
|
||||||
@ -42,7 +44,31 @@ func main() {
|
|||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* groupHelperService := */
|
|
||||||
logic.NewGroupHelperService(groupHelperRepo)
|
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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user