group_helper/cli/commands/broadcast.go

71 lines
1.7 KiB
Go

package commands
import (
"fmt"
"git.dragon-labs.de/alphyron/group_helper/cli"
"git.dragon-labs.de/alphyron/group_helper/models"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"strconv"
"strings"
)
type broadcastCommand struct {
}
func NewBroadcastCommand() cli.Command {
return &broadcastCommand{}
}
func (b broadcastCommand) GetUsage() string {
return "/broadcast [groupid] [message]"
}
func (b broadcastCommand) GetCommand() string {
return "/broadcast"
}
func (b broadcastCommand) GetDescription() string {
return "Send a message to the given group from the bot"
}
func (b broadcastCommand) ExecuteCommand(bot *tgbotapi.BotAPI, update *tgbotapi.Update, group *models.Group) (bool, error) {
message := update.Message.Text
parts := strings.Split(message, " ")
if len(parts) <= 2 {
_, err := bot.Send(tgbotapi.NewMessage(update.Message.Chat.ID, "Wrong usage of this Command:\n"+b.GetUsage()))
return err != nil, err
}
var currentGroup *models.Group
for _, controlledGroup := range group.ControlledGroups {
if strconv.FormatInt(controlledGroup.GroupID, 10) == parts[1] {
currentGroup = controlledGroup
}
}
if currentGroup == nil {
message := fmt.Sprintf("There are no Groups to Manage")
msg := tgbotapi.NewMessage(update.Message.Chat.ID, message)
_, err := bot.Send(msg)
return false, err
}
msg := tgbotapi.NewMessage(currentGroup.GroupID, strings.Join(parts[2:], " "))
_, err := bot.Send(msg)
return err == nil, err
}
func (b broadcastCommand) AllowChatType(_ *tgbotapi.Chat) bool {
return true
}
func (b broadcastCommand) AllowMember(member *tgbotapi.ChatMember) bool {
return member.IsAdministrator() || member.IsCreator()
}
func (b broadcastCommand) AllowEveryMember() bool {
return false
}