From aa3c066e9b6ec08efc59376be4acb778468f3588 Mon Sep 17 00:00:00 2001 From: woiza Date: Tue, 19 Mar 2024 21:14:27 +0100 Subject: [PATCH] support groups (#19) --- README.md | 2 +- pkg/bot/add.go | 12 ++++---- pkg/bot/bot.go | 60 ++++++++++++++++++------------------- pkg/bot/command.go | 21 +++++++------ pkg/bot/delete.go | 16 +++++----- pkg/bot/libraryfiltered.go | 8 ++--- pkg/bot/librarymenu.go | 2 +- pkg/bot/librarymovieedit.go | 6 ++-- pkg/config/config.go | 4 +-- 9 files changed, 65 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 11138fa..b5a19cf 100644 --- a/README.md +++ b/README.md @@ -77,7 +77,7 @@ services: restart: always environment: - RBOT_TELEGRAM_BOT_TOKEN=1460...:AAHlBW_mabVg... - - RBOT_BOT_ALLOWED_USERIDS=12345,98765,45678 # Telegram user ID(s) + - RBOT_BOT_ALLOWED_USERIDS=123,987,-567 # Telegram user ID(s), Group IDs are negative - RBOT_BOT_MAX_ITEMS=10 # pagination - RBOT_RADARR_PROTOCOL=http # http or https - RBOT_RADARR_PORT=7878 diff --git a/pkg/bot/add.go b/pkg/bot/add.go index 4e344b3..602ca8b 100644 --- a/pkg/bot/add.go +++ b/pkg/bot/add.go @@ -29,8 +29,8 @@ const ( AddMovieColMon = "ADDMOVIE_COLMON" ) -func (b *Bot) processAddCommand(update tgbotapi.Update, userID int64, r *radarr.Radarr) { - msg := tgbotapi.NewMessage(userID, "Handling add movie command... please wait") +func (b *Bot) processAddCommand(update tgbotapi.Update, chatID int64, r *radarr.Radarr) { + msg := tgbotapi.NewMessage(chatID, "Handling add movie command... please wait") message, _ := b.sendMessage(msg) command := userAddMovie{ chatID: message.Chat.ID, @@ -44,7 +44,7 @@ func (b *Bot) processAddCommand(update tgbotapi.Update, userID int64, r *radarr. } searchResults, err := r.Lookup(criteria) if err != nil { - msg := tgbotapi.NewMessage(userID, err.Error()) + msg := tgbotapi.NewMessage(chatID, err.Error()) b.sendMessage(msg) return } @@ -70,18 +70,18 @@ func (b *Bot) processAddCommand(update tgbotapi.Update, userID int64, r *radarr. } func (b *Bot) addMovie(update tgbotapi.Update) bool { - userID, err := b.getUserID(update) + chatID, err := b.getChatID(update) if err != nil { fmt.Printf("Cannot add movie: %v", err) return false } - command, exists := b.getAddMovieState(userID) + command, exists := b.getAddMovieState(chatID) if !exists { return false } switch update.CallbackQuery.Data { case AddMovieYes: - b.setActiveCommand(userID, AddMovieCommand) + b.setActiveCommand(chatID, AddMovieCommand) return b.handleAddMovieYes(update, command) case AddMovieGoBack: b.setAddMovieState(command.chatID, command) diff --git a/pkg/bot/bot.go b/pkg/bot/bot.go index 6e4c91d..1aba9fd 100644 --- a/pkg/bot/bot.go +++ b/pkg/bot/bot.go @@ -130,19 +130,19 @@ func (b *Bot) HandleUpdates(updates <-chan tgbotapi.Update) { } func (b *Bot) HandleUpdate(update tgbotapi.Update) { - userID, err := b.getUserID(update) + chatID, err := b.getChatID(update) if err != nil { fmt.Printf("Cannot handle update: %v", err) return } - if update.Message != nil && !b.Config.AllowedUserIDs[userID] { + if update.Message != nil && !b.Config.AllowedChatIDs[chatID] { msg := tgbotapi.NewMessage(update.Message.Chat.ID, "Access denied. You are not authorized.") b.sendMessage(msg) return } - activeCommand, _ := b.getActiveCommand(userID) + activeCommand, _ := b.getActiveCommand(chatID) if update.CallbackQuery != nil { switch activeCommand { @@ -191,7 +191,7 @@ func (b *Bot) HandleUpdate(update tgbotapi.Update) { } func (b *Bot) clearState(update tgbotapi.Update) { - userID, err := b.getUserID(update) + chatID, err := b.getChatID(update) if err != nil { fmt.Printf("Cannot clear state: %v", err) return @@ -201,88 +201,88 @@ func (b *Bot) clearState(update tgbotapi.Update) { b.muActiveCommand.Lock() defer b.muActiveCommand.Unlock() - delete(b.ActiveCommand, userID) + delete(b.ActiveCommand, chatID) b.muAddMovieStates.Lock() defer b.muAddMovieStates.Unlock() - delete(b.AddMovieStates, userID) + delete(b.AddMovieStates, chatID) b.muDeleteMovieStates.Lock() defer b.muDeleteMovieStates.Unlock() - delete(b.DeleteMovieStates, userID) + delete(b.DeleteMovieStates, chatID) b.muLibraryStates.Lock() defer b.muLibraryStates.Unlock() - delete(b.LibraryStates, userID) + delete(b.LibraryStates, chatID) } -func (b *Bot) getUserID(update tgbotapi.Update) (int64, error) { - var userID int64 +func (b *Bot) getChatID(update tgbotapi.Update) (int64, error) { + var chatID int64 if update.Message != nil { - userID = update.Message.From.ID + chatID = update.Message.Chat.ID } if update.CallbackQuery != nil { - userID = update.CallbackQuery.From.ID + chatID = update.CallbackQuery.Message.Chat.ID } - if userID == 0 { + if chatID == 0 { return 0, fmt.Errorf("no user ID found in Message and CallbackQuery") } - return userID, nil + return chatID, nil } -func (b *Bot) getActiveCommand(userID int64) (string, bool) { +func (b *Bot) getActiveCommand(chatID int64) (string, bool) { b.muActiveCommand.Lock() defer b.muActiveCommand.Unlock() - cmd, exists := b.ActiveCommand[userID] + cmd, exists := b.ActiveCommand[chatID] return cmd, exists } -func (b *Bot) setActiveCommand(userID int64, command string) { +func (b *Bot) setActiveCommand(chatID int64, command string) { b.muActiveCommand.Lock() defer b.muActiveCommand.Unlock() - b.ActiveCommand[userID] = command + b.ActiveCommand[chatID] = command } -func (b *Bot) getAddMovieState(userID int64) (*userAddMovie, bool) { +func (b *Bot) getAddMovieState(chatID int64) (*userAddMovie, bool) { b.muAddMovieStates.Lock() defer b.muAddMovieStates.Unlock() - state, exists := b.AddMovieStates[userID] + state, exists := b.AddMovieStates[chatID] return state, exists } -func (b *Bot) setAddMovieState(userID int64, state *userAddMovie) { +func (b *Bot) setAddMovieState(chatID int64, state *userAddMovie) { b.muAddMovieStates.Lock() defer b.muAddMovieStates.Unlock() - b.AddMovieStates[userID] = state + b.AddMovieStates[chatID] = state } -func (b *Bot) getDeleteMovieState(userID int64) (*userDeleteMovie, bool) { +func (b *Bot) getDeleteMovieState(chatID int64) (*userDeleteMovie, bool) { b.muDeleteMovieStates.Lock() defer b.muDeleteMovieStates.Unlock() - state, exists := b.DeleteMovieStates[userID] + state, exists := b.DeleteMovieStates[chatID] return state, exists } -func (b *Bot) setDeleteMovieState(userID int64, state *userDeleteMovie) { +func (b *Bot) setDeleteMovieState(chatID int64, state *userDeleteMovie) { b.muDeleteMovieStates.Lock() defer b.muDeleteMovieStates.Unlock() - b.DeleteMovieStates[userID] = state + b.DeleteMovieStates[chatID] = state } -func (b *Bot) getLibraryState(userID int64) (*userLibrary, bool) { +func (b *Bot) getLibraryState(chatID int64) (*userLibrary, bool) { b.muLibraryStates.Lock() defer b.muLibraryStates.Unlock() - state, exists := b.LibraryStates[userID] + state, exists := b.LibraryStates[chatID] return state, exists } -func (b *Bot) setLibraryState(userID int64, state *userLibrary) { +func (b *Bot) setLibraryState(chatID int64, state *userLibrary) { b.muLibraryStates.Lock() defer b.muLibraryStates.Unlock() - b.LibraryStates[userID] = state + b.LibraryStates[chatID] = state } func (b *Bot) sendMessage(msg tgbotapi.Chattable) (tgbotapi.Message, error) { diff --git a/pkg/bot/command.go b/pkg/bot/command.go index f9fbd90..eeb82db 100644 --- a/pkg/bot/command.go +++ b/pkg/bot/command.go @@ -12,27 +12,27 @@ import ( func (b *Bot) handleCommand(update tgbotapi.Update, r *radarr.Radarr) { - userID, err := b.getUserID(update) + chatID, err := b.getChatID(update) if err != nil { fmt.Printf("Cannot handle command: %v", err) return } - msg := tgbotapi.NewMessage(update.Message.From.ID, "") + msg := tgbotapi.NewMessage(chatID, "") switch update.Message.Command() { case "q", "query", "add", "Q", "Query", "Add": - b.setActiveCommand(userID, AddMovieCommand) - b.processAddCommand(update, userID, r) + b.setActiveCommand(chatID, AddMovieCommand) + b.processAddCommand(update, chatID, r) case "movies", "library", "l": - b.setActiveCommand(userID, LibraryMenuCommand) - b.processLibraryCommand(update, userID, r) + b.setActiveCommand(chatID, LibraryMenuCommand) + b.processLibraryCommand(update, chatID, r) case "delete", "remove", "Delete", "Remove", "d": - b.setActiveCommand(userID, DeleteMovieCommand) - b.processDeleteCommand(update, userID, r) + b.setActiveCommand(chatID, DeleteMovieCommand) + b.processDeleteCommand(update, chatID, r) case "clear", "cancel", "stop": b.clearState(update) @@ -149,12 +149,11 @@ func (b *Bot) handleCommand(update tgbotapi.Update, r *radarr.Radarr) { b.sendMessage(msg) break } - message := prettyPrint(status) - msg := tgbotapi.NewMessage(update.Message.Chat.ID, message) + msg.Text = prettyPrint(status) b.sendMessage(msg) case "getid", "id": - msg := tgbotapi.NewMessage(update.Message.Chat.ID, fmt.Sprintf("Your user ID: %d", userID)) + msg.Text = fmt.Sprintf("Your user ID: %d", chatID) b.sendMessage(msg) default: diff --git a/pkg/bot/delete.go b/pkg/bot/delete.go index 2e658d1..4dfec35 100644 --- a/pkg/bot/delete.go +++ b/pkg/bot/delete.go @@ -24,13 +24,13 @@ const ( DeleteMovieLastPage = "DELETE_MOVIE_LAST_PAGE" ) -func (b *Bot) processDeleteCommand(update tgbotapi.Update, userID int64, r *radarr.Radarr) { - msg := tgbotapi.NewMessage(userID, "Handling delete command... please wait") +func (b *Bot) processDeleteCommand(update tgbotapi.Update, chatID int64, r *radarr.Radarr) { + msg := tgbotapi.NewMessage(chatID, "Handling delete command... please wait") message, _ := b.sendMessage(msg) movies, err := r.GetMovie(0) if err != nil { - msg := tgbotapi.NewMessage(userID, err.Error()) + msg := tgbotapi.NewMessage(chatID, err.Error()) b.sendMessage(msg) return } @@ -49,7 +49,7 @@ func (b *Bot) processDeleteCommand(update tgbotapi.Update, userID int64, r *rada command.moviesForSelection = movies command.chatID = message.Chat.ID command.messageID = message.MessageID - b.setDeleteMovieState(userID, &command) + b.setDeleteMovieState(chatID, &command) criteria := update.Message.CommandArguments() // no search criteria --> show complete library and return @@ -60,23 +60,23 @@ func (b *Bot) processDeleteCommand(update tgbotapi.Update, userID int64, r *rada searchResults, err := r.Lookup(criteria) if err != nil { - msg := tgbotapi.NewMessage(userID, err.Error()) + msg := tgbotapi.NewMessage(chatID, err.Error()) b.sendMessage(msg) return } - b.setDeleteMovieState(userID, &command) + b.setDeleteMovieState(chatID, &command) b.handleDeleteSearchResults(searchResults, &command) } func (b *Bot) deleteMovie(update tgbotapi.Update) bool { - userID, err := b.getUserID(update) + chatID, err := b.getChatID(update) if err != nil { fmt.Printf("Cannot delete movie: %v", err) return false } - command, exists := b.getDeleteMovieState(userID) + command, exists := b.getDeleteMovieState(chatID) if !exists { return false } diff --git a/pkg/bot/libraryfiltered.go b/pkg/bot/libraryfiltered.go index 5bc17da..2a0ccfc 100644 --- a/pkg/bot/libraryfiltered.go +++ b/pkg/bot/libraryfiltered.go @@ -36,13 +36,13 @@ const ( ) func (b *Bot) libraryFiltered(update tgbotapi.Update) bool { - userID, err := b.getUserID(update) + chatID, err := b.getChatID(update) if err != nil { fmt.Printf("Cannot manage library: %v", err) return false } - command, exists := b.getLibraryState(userID) + command, exists := b.getLibraryState(chatID) if !exists { return false } @@ -67,12 +67,12 @@ func (b *Bot) libraryFiltered(update tgbotapi.Update) bool { return b.showLibraryMenuFiltered(command) case LibraryMovieGoBack: command.movie = nil - b.setActiveCommand(userID, LibraryFilteredActive) + b.setActiveCommand(chatID, LibraryFilteredActive) b.setLibraryState(command.chatID, command) return b.showLibraryMenuFiltered(command) case LibraryFilteredGoBack: command.filter = "" - b.setActiveCommand(userID, LibraryMenuActive) + b.setActiveCommand(chatID, LibraryMenuActive) b.setLibraryState(command.chatID, command) return b.showLibraryMenu(command) case LibraryMovieMonitor: diff --git a/pkg/bot/librarymenu.go b/pkg/bot/librarymenu.go index 6da94e2..73be363 100644 --- a/pkg/bot/librarymenu.go +++ b/pkg/bot/librarymenu.go @@ -81,7 +81,7 @@ func (b *Bot) processLibraryCommand(update tgbotapi.Update, userID int64, r *rad } func (b *Bot) libraryMenu(update tgbotapi.Update) bool { - userID, err := b.getUserID(update) + userID, err := b.getChatID(update) if err != nil { fmt.Printf("Cannot manage library: %v", err) return false diff --git a/pkg/bot/librarymovieedit.go b/pkg/bot/librarymovieedit.go index 5ca040b..532c11e 100644 --- a/pkg/bot/librarymovieedit.go +++ b/pkg/bot/librarymovieedit.go @@ -20,13 +20,13 @@ const ( ) func (b *Bot) libraryMovieEdit(update tgbotapi.Update) bool { - userID, err := b.getUserID(update) + chatID, err := b.getChatID(update) if err != nil { fmt.Printf("Cannot manage library: %v", err) return false } - command, exists := b.getLibraryState(userID) + command, exists := b.getLibraryState(chatID) if !exists { return false } @@ -38,7 +38,7 @@ func (b *Bot) libraryMovieEdit(update tgbotapi.Update) bool { case LibraryMovieEditSubmitChanges: return b.handleLibraryMovieEditSubmitChanges(update, command) case LibraryMovieEditGoBack: - b.setActiveCommand(userID, LibraryFilteredActive) + b.setActiveCommand(chatID, LibraryFilteredActive) b.setLibraryState(command.chatID, command) return b.showLibraryMovieDetail(update, command) case LibraryMovieEditCancel: diff --git a/pkg/config/config.go b/pkg/config/config.go index 7865cad..5ad5c78 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -11,7 +11,7 @@ import ( // BotConfig ... type Config struct { TelegramBotToken string - AllowedUserIDs map[int64]bool + AllowedChatIDs map[int64]bool MaxItems int RadarrProtocol string RadarrHostname string @@ -72,7 +72,7 @@ func LoadConfig() (Config, error) { } parsedUserIDs[parsedID] = true } - config.AllowedUserIDs = parsedUserIDs + config.AllowedChatIDs = parsedUserIDs // Parsing RBOT_RADARR_PORT as a number port, err := strconv.Atoi(radarrPort)