Skip to content

Commit

Permalink
support groups (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
woiza authored Mar 19, 2024
1 parent ef9f0a6 commit aa3c066
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 66 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions pkg/bot/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
}
Expand All @@ -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)
Expand Down
60 changes: 30 additions & 30 deletions pkg/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down
21 changes: 10 additions & 11 deletions pkg/bot/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 8 additions & 8 deletions pkg/bot/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/bot/libraryfiltered.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pkg/bot/librarymenu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/bot/librarymovieedit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit aa3c066

Please sign in to comment.