From d68335345228edc2452d63f9759d7769cb048dc1 Mon Sep 17 00:00:00 2001 From: woiza Date: Mon, 18 Mar 2024 16:33:12 +0100 Subject: [PATCH 1/2] reply to user instead of chat --- pkg/bot/command.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/bot/command.go b/pkg/bot/command.go index 806072b..993a667 100644 --- a/pkg/bot/command.go +++ b/pkg/bot/command.go @@ -18,7 +18,7 @@ func (b *Bot) handleCommand(update tgbotapi.Update, s *sonarr.Sonarr) { return } - msg := tgbotapi.NewMessage(update.Message.Chat.ID, "") + msg := tgbotapi.NewMessage(update.Message.From.ID, "") switch update.Message.Command() { From e76dbbe9e82cf3e9c037d008bb609f4e70202543 Mon Sep 17 00:00:00 2001 From: woiza Date: Wed, 20 Mar 2024 19:35:54 +0100 Subject: [PATCH 2/2] group support --- 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 | 20 ++++++------ pkg/bot/libraryseasonedit.go | 6 ++-- pkg/bot/libraryseriesedit.go | 6 ++-- pkg/config/config.go | 4 +-- 9 files changed, 76 insertions(+), 77 deletions(-) diff --git a/pkg/bot/add.go b/pkg/bot/add.go index 2adb81b..18f1fc6 100644 --- a/pkg/bot/add.go +++ b/pkg/bot/add.go @@ -29,8 +29,8 @@ const ( AddSeriesCutOff = "ADDSERIES_CUTOFF" ) -func (b *Bot) processAddCommand(update tgbotapi.Update, userID int64, s *sonarr.Sonarr) { - msg := tgbotapi.NewMessage(userID, "Handling add series ommand... please wait") +func (b *Bot) processAddCommand(update tgbotapi.Update, chatID int64, s *sonarr.Sonarr) { + msg := tgbotapi.NewMessage(chatID, "Handling add series ommand... please wait") message, _ := b.sendMessage(msg) command := userAddSeries{ chatID: message.Chat.ID, @@ -44,7 +44,7 @@ func (b *Bot) processAddCommand(update tgbotapi.Update, userID int64, s *sonarr. } searchResults, err := s.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, s *sonarr. } func (b *Bot) addSeries(update tgbotapi.Update) bool { - userID, err := b.getUserID(update) + chatID, err := b.getChatID(update) if err != nil { fmt.Printf("Cannot add series: %v", err) return false } - command, exists := b.getAddSeriesState(userID) + command, exists := b.getAddSeriesState(chatID) if !exists { return false } switch update.CallbackQuery.Data { case AddSeriesYes: - b.setActiveCommand(userID, AddSeriesCommand) + b.setActiveCommand(chatID, AddSeriesCommand) return b.handleAddSeriesYes(update, command) case AddSeriesGoBack: b.setAddSeriesState(command.chatID, command) diff --git a/pkg/bot/bot.go b/pkg/bot/bot.go index 3db1ef4..d640b92 100644 --- a/pkg/bot/bot.go +++ b/pkg/bot/bot.go @@ -138,19 +138,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 { @@ -203,7 +203,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 @@ -213,88 +213,88 @@ func (b *Bot) clearState(update tgbotapi.Update) { b.muActiveCommand.Lock() defer b.muActiveCommand.Unlock() - delete(b.ActiveCommand, userID) + delete(b.ActiveCommand, chatID) b.muAddSeriesStates.Lock() defer b.muAddSeriesStates.Unlock() - delete(b.AddSeriesStates, userID) + delete(b.AddSeriesStates, chatID) b.muDeleteSeriesStates.Lock() defer b.muDeleteSeriesStates.Unlock() - delete(b.DeleteSeriesStates, userID) + delete(b.DeleteSeriesStates, 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) getAddSeriesState(userID int64) (*userAddSeries, bool) { +func (b *Bot) getAddSeriesState(chatID int64) (*userAddSeries, bool) { b.muAddSeriesStates.Lock() defer b.muAddSeriesStates.Unlock() - state, exists := b.AddSeriesStates[userID] + state, exists := b.AddSeriesStates[chatID] return state, exists } -func (b *Bot) setAddSeriesState(userID int64, state *userAddSeries) { +func (b *Bot) setAddSeriesState(chatID int64, state *userAddSeries) { b.muAddSeriesStates.Lock() defer b.muAddSeriesStates.Unlock() - b.AddSeriesStates[userID] = state + b.AddSeriesStates[chatID] = state } -func (b *Bot) getDeleteSeriesState(userID int64) (*userDeleteSeries, bool) { +func (b *Bot) getDeleteSeriesState(chatID int64) (*userDeleteSeries, bool) { b.muDeleteSeriesStates.Lock() defer b.muDeleteSeriesStates.Unlock() - state, exists := b.DeleteSeriesStates[userID] + state, exists := b.DeleteSeriesStates[chatID] return state, exists } -func (b *Bot) setDeleteSeriesState(userID int64, state *userDeleteSeries) { +func (b *Bot) setDeleteSeriesState(chatID int64, state *userDeleteSeries) { b.muDeleteSeriesStates.Lock() defer b.muDeleteSeriesStates.Unlock() - b.DeleteSeriesStates[userID] = state + b.DeleteSeriesStates[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 993a667..9e6e44f 100644 --- a/pkg/bot/command.go +++ b/pkg/bot/command.go @@ -12,27 +12,27 @@ import ( func (b *Bot) handleCommand(update tgbotapi.Update, s *sonarr.Sonarr) { - 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, AddSeriesCommand) - b.processAddCommand(update, userID, s) + b.setActiveCommand(chatID, AddSeriesCommand) + b.processAddCommand(update, chatID, s) case "series", "library", "l": - b.setActiveCommand(userID, LibraryMenuCommand) - b.processLibraryCommand(update, userID, s) + b.setActiveCommand(chatID, LibraryMenuCommand) + b.processLibraryCommand(update, chatID, s) case "delete", "remove", "Delete", "Remove", "d": - b.setActiveCommand(userID, DeleteSeriesCommand) - b.processDeleteCommand(update, userID, s) + b.setActiveCommand(chatID, DeleteSeriesCommand) + b.processDeleteCommand(update, chatID, s) case "clear", "cancel", "stop": b.clearState(update) @@ -151,12 +151,11 @@ func (b *Bot) handleCommand(update tgbotapi.Update, s *sonarr.Sonarr) { 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 613930a..0c55e67 100644 --- a/pkg/bot/delete.go +++ b/pkg/bot/delete.go @@ -24,13 +24,13 @@ const ( DeleteSeriesLastPage = "DELETE_SERIES_LAST_PAGE" ) -func (b *Bot) processDeleteCommand(update tgbotapi.Update, userID int64, s *sonarr.Sonarr) { - msg := tgbotapi.NewMessage(userID, "Handling delete command... please wait") +func (b *Bot) processDeleteCommand(update tgbotapi.Update, chatID int64, s *sonarr.Sonarr) { + msg := tgbotapi.NewMessage(chatID, "Handling delete command... please wait") message, _ := b.sendMessage(msg) series, err := s.GetSeries(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, s *sona command.seriesForSelection = series command.chatID = message.Chat.ID command.messageID = message.MessageID - b.setDeleteSeriesState(userID, &command) + b.setDeleteSeriesState(chatID, &command) criteria := update.Message.CommandArguments() // no search criteria --> show complete library and return @@ -61,23 +61,23 @@ func (b *Bot) processDeleteCommand(update tgbotapi.Update, userID int64, s *sona //update.Message.Text = fmt.Sprintf("/q \"%s\"", update.Message.Text) searchResults, err := s.Lookup("\"" + criteria + "\"") if err != nil { - msg := tgbotapi.NewMessage(userID, err.Error()) + msg := tgbotapi.NewMessage(chatID, err.Error()) b.sendMessage(msg) return } - b.setDeleteSeriesState(userID, &command) + b.setDeleteSeriesState(chatID, &command) b.handleDeleteSearchResults(searchResults, &command) } func (b *Bot) deleteSeries(update tgbotapi.Update) bool { - userID, err := b.getUserID(update) + chatID, err := b.getChatID(update) if err != nil { fmt.Printf("Cannot delete Series: %v", err) return false } - command, exists := b.getDeleteSeriesState(userID) + command, exists := b.getDeleteSeriesState(chatID) if !exists { return false } diff --git a/pkg/bot/libraryfiltered.go b/pkg/bot/libraryfiltered.go index 6876339..529eb84 100644 --- a/pkg/bot/libraryfiltered.go +++ b/pkg/bot/libraryfiltered.go @@ -34,13 +34,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 } @@ -65,12 +65,12 @@ func (b *Bot) libraryFiltered(update tgbotapi.Update) bool { return b.showLibraryMenuFiltered(command) case LibrarySeriesGoBack: command.series = 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 LibrarySeriesMonitor: diff --git a/pkg/bot/librarymenu.go b/pkg/bot/librarymenu.go index 9cd903c..fb0cd99 100644 --- a/pkg/bot/librarymenu.go +++ b/pkg/bot/librarymenu.go @@ -35,25 +35,25 @@ const ( FilterSearchResults = "FILTER_SEARCHRESULTS" ) -func (b *Bot) processLibraryCommand(update tgbotapi.Update, userID int64, s *sonarr.Sonarr) { - msg := tgbotapi.NewMessage(userID, "Handling library command... please wait") +func (b *Bot) processLibraryCommand(update tgbotapi.Update, chatID int64, s *sonarr.Sonarr) { + msg := tgbotapi.NewMessage(chatID, "Handling library command... please wait") message, _ := b.sendMessage(msg) qualityProfiles, err := s.GetQualityProfiles() if err != nil { - msg := tgbotapi.NewMessage(userID, err.Error()) + msg := tgbotapi.NewMessage(chatID, err.Error()) b.sendMessage(msg) return } tags, err := s.GetTags() if err != nil { - msg := tgbotapi.NewMessage(userID, err.Error()) + msg := tgbotapi.NewMessage(chatID, err.Error()) b.sendMessage(msg) return } series, err := s.GetSeries(0) if err != nil { - msg := tgbotapi.NewMessage(userID, err.Error()) + msg := tgbotapi.NewMessage(chatID, err.Error()) b.sendMessage(msg) return } @@ -69,14 +69,14 @@ func (b *Bot) processLibraryCommand(update tgbotapi.Update, userID int64, s *son criteria := update.Message.CommandArguments() // no search criteria --> show menu and return if len(criteria) < 1 { - b.setLibraryState(userID, &command) + b.setLibraryState(chatID, &command) b.showLibraryMenu(&command) return } searchResults, err := s.Lookup("\"" + criteria + "\"") if err != nil { - msg := tgbotapi.NewMessage(userID, err.Error()) + msg := tgbotapi.NewMessage(chatID, err.Error()) b.sendMessage(msg) return } @@ -86,20 +86,20 @@ func (b *Bot) processLibraryCommand(update tgbotapi.Update, userID int64, s *son } func (b *Bot) libraryMenu(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 } switch update.CallbackQuery.Data { case LibraryFilteredGoBack: command.filter = "" - b.setActiveCommand(userID, LibraryMenuActive) + b.setActiveCommand(chatID, LibraryMenuActive) b.setLibraryState(command.chatID, command) return b.showLibraryMenu(command) case LibraryMenu: diff --git a/pkg/bot/libraryseasonedit.go b/pkg/bot/libraryseasonedit.go index d951781..ec79a34 100644 --- a/pkg/bot/libraryseasonedit.go +++ b/pkg/bot/libraryseasonedit.go @@ -28,13 +28,13 @@ const ( ) func (b *Bot) librarySeasonEdit(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 } @@ -44,7 +44,7 @@ func (b *Bot) librarySeasonEdit(update tgbotapi.Update) bool { case LibrarySeasonEditSubmitChanges: return b.handleLibrarySeasonEditSubmitChanges(update, command) case LibrarySeasonEditGoBack: - b.setActiveCommand(userID, LibraryFilteredActive) + b.setActiveCommand(chatID, LibraryFilteredActive) b.setLibraryState(command.chatID, command) return b.showLibrarySeriesDetail(update, command) case LibrarySeasonEditCancel: diff --git a/pkg/bot/libraryseriesedit.go b/pkg/bot/libraryseriesedit.go index a174117..9081fed 100644 --- a/pkg/bot/libraryseriesedit.go +++ b/pkg/bot/libraryseriesedit.go @@ -19,13 +19,13 @@ const ( ) func (b *Bot) librarySeriesEdit(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 } @@ -37,7 +37,7 @@ func (b *Bot) librarySeriesEdit(update tgbotapi.Update) bool { case LibrarySeriesEditSubmitChanges: return b.handleLibrarySeriesEditSubmitChanges(update, command) case LibrarySeriesEditGoBack: - b.setActiveCommand(userID, LibraryFilteredActive) + b.setActiveCommand(chatID, LibraryFilteredActive) b.setLibraryState(command.chatID, command) return b.showLibrarySeriesDetail(update, command) case LibrarySeriesEditCancel: diff --git a/pkg/config/config.go b/pkg/config/config.go index 7961124..44d136c 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 SonarrProtocol string SonarrHostname string @@ -72,7 +72,7 @@ func LoadConfig() (Config, error) { } parsedUserIDs[parsedID] = true } - config.AllowedUserIDs = parsedUserIDs + config.AllowedChatIDs = parsedUserIDs // Parsing SBOT_SONARR_PORT as a number port, err := strconv.Atoi(sonarrPort)